0

I have the following error when addTextChangedListener is added to onCreate Os Android 4.4 API19:

Error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.nes.android.emplacements/com.nes.android.emplacements.MainActivity}: java.lang.NullPointerException
Caused by: java.lang.NullPointerException
    at com.nes.android.emplacements.MainActivity.onCreate(MainActivity.java:32)
    at android.app.Activity.performCreate(Activity.java:5231)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
    at android.app.ActivityThread.access$800(ActivityThread.java:135)

Here's my code:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

        EditText valEmplacement = (EditText)findViewById(R.id.editText2);

        valEmplacement.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) { }
            public void beforeTextChanged(CharSequence s, int start, int count, int after){}
            public void onTextChanged(CharSequence s, int start, int before, int count){}
        });

}

editText2 is in fragment_main and not in activiy_main

activiy_main.xml :

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.nes.android.emplacements.MainActivity"
    tools:ignore="MergeRootFrame" />

fragment_main.xml :

<RelativeLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.nes.android.emplacements.MainActivity$PlaceholderFragment"
    android:focusable="true">


    <EditText
        android:layout_width="fill_parent"
        android:layout_height="80px"
        android:id="@+id/editText2"
        android:inputType="text"
        android:layout_below="@+id/textView"
        android:background="#cccccc"
        android:autoText="false"
        android:textSize="20px"
        android:textIsSelectable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="@string/text_emplacement"
        android:id="@+id/textView"
        android:layout_below="@+id/textView3"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginTop="46dp"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true" />    

</RelativeLayout>
  • NPE comes from 32 row, check this row. Probably your valEmplacement is null and NPE happens when you try add listener. – Georgy Gobozov Feb 14 '14 at 16:16
  • It seems `activity_main` doesn't have `editText2`.Can you poast the code for `activity_main`? – Abhishek V Feb 14 '14 at 16:17
  • i add if (valEmplacement.getText() != null), but dont work: if (valEmplacement.getText() != null){ valEmplacement.addTextChangedListener(new TextWatcher(){ public void afterTextChanged(Editable s) { } public void beforeTextChanged(CharSequence s, int start, int count, int after){} public void onTextChanged(CharSequence s, int start, int before, int count){} }); } – latrach.net Feb 14 '14 at 16:29
  • I think that you have to do it in your fragment code, because since the editText is in there, it would be null in the activity. – Alejandro Cumpa Feb 14 '14 at 16:48

1 Answers1

0

try clean & build project in eclipse to regenerate your R file.

and make sure that your activiy_main layout has the element with id editText2

Update: I see your view with id editText2 is in the fragment layout actually. So I suggest you move your code that handles this edit text view into the Fragment class. So for example in this method of your Fragment class:

 public void onActivityCreated(Bundle savedInstanceState) {
       super.onActivityCreated(savedInstanceState);

       //retrieve your fragment views here
donfuxx
  • 11,277
  • 6
  • 44
  • 76