0

I took care of placing setContentView before using findViewbyId method but still it is giving me error. can any one please explain why this is happeneing. Here is my code.

public class DispActivity extends ActionBarActivity {

public String message;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_disp);

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new              PlaceholderFragment()).commit();
    }

    Intent intent = getIntent();
    message = intent.getStringExtra(MainActivity.Extra);
    EditText txt = (EditText)findViewById(R.id.edit_text23);
    System.out.println("check point 1");
    txt.setText("Having problem here");
    System.out.println("check point 2");


}

fragment_disp.xml lookss like this.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.wifi.DispActivity$PlaceholderFragment" >

<EditText 
    android:id="@+id/edit_text23"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
/>
<TextView
    android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />
<Button android:id="@+id/but"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="click"/>
</LinearLayout>

Error that is occuring is

05-13 23:50:46.716: I/System.out(5713): check point 1
05-13 23:50:46.716: D/AndroidRuntime(5713): Shutting down VM
05-13 23:50:46.716: W/dalvikvm(5713): threadid=1: thread exiting with uncaught     exception (group=0x419c9ba8)
05-13 23:50:46.726: E/AndroidRuntime(5713): FATAL EXCEPTION: main
05-13 23:50:46.726: E/AndroidRuntime(5713): Process: com.example.wifi, PID: 5713
05-13 23:50:46.726: E/AndroidRuntime(5713): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.wifi/com.example.wifi.DispActivity}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
05-13 23:50:46.726: E/AndroidRuntime(5713):     at  android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.os.Looper.loop(Looper.java:136)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invokeNative(Native Method)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at java.lang.reflect.Method.invoke(Method.java:515)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at dalvik.system.NativeStart.main(Native Method)
05-13 23:50:46.726: E/AndroidRuntime(5713): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
05-13 23:50:46.726: E/AndroidRuntime(5713):     at com.example.wifi.DispActivity$PlaceholderFragment.onCreateView(DispActivity.java:74)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:938)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1115)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1478)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1171)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.Activity.performStart(Activity.java:5241)
05-13 23:50:46.726: E/AndroidRuntime(5713):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2168)
05-13 23:50:46.726: E/AndroidRuntime(5713):     ... 11 more

2 Answers 2

2

You are trying to cast a TextView to an EditText and that's causing the error. Are you sure you have given the entire relevant code? The stack trace is pretty clear with line numbers.

0
0

This code should be put in the fragment PlaceholderFragment.

EditText txt = (EditText)findViewById(R.id.edit_text23);
System.out.println("check point 1");
txt.setText("Having problem here");
System.out.println("check point 2");

The EditText is located in fragment_disp.xml and you are referencing that in your layout R.layout.activity_disp which is why you are getting a NPE. The activity_disp.xml doesn't have that EditText.

EDITED

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_disp, container, false);

    EditText txt = (EditText) view.findViewById(R.id.edit_text23);
    System.out.println("check point 1");
    txt.setText("Having problem here");
    System.out.println("check point 2");
}
2
  • Yes you are correct that view objects registered in activity_disp.xml can be used in onCreate method. I tried it and it works. But now I'm wondering when are view objects registered in fragment_disp.xml intiated, and why do we have two xml if both of them are doing the same job(handling the layout)? Itried to put my code in onCreateView method in PlaceholderFragment class but I'm getting the same error there also.
    – Mercurial
    May 14, 2014 at 9:20
  • @Mercurial please see my edited answer. Did you inflate your layout like so? The above code will work. Fragments are the standard way of showing layouts due to it's flexibility and reusability. According to Android Developers, even if you only have 1 fragment in an activity it is still better practice to do it that way versus putting the layout in the activity.
    – The Nomad
    May 15, 2014 at 18:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.