addTextChangedListener时的java.lang.NullPointerException

时间:2014-02-14 16:01:06

标签: java android android-4.4-kitkat

将addTextChangedListener添加到onCreate时出现以下错误 Os Android 4.4 API19:

错误:

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)

这是我的代码:

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位于fragment_main中,而不在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>

1 个答案:

答案 0 :(得分:0)

尝试清洁&amp;在eclipse中构建项目以重新生成R文件。

并确保您的activiy_main布局包含id为editText2

的元素

更新:我看到你的id为editText2的视图实际上是在片段布局中。因此,我建议您将处理此编辑文本视图的代码移动到Fragment类中。例如,在Fragment类的这个方法中:

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

       //retrieve your fragment views here