setOnEditorActionListener给出NullPointerException

时间:2015-03-28 01:59:42

标签: java android nullpointerexception crash android-edittext

我遇到setOnEditorActionListener的问题,我无法理解为什么它会导致应用程序崩溃。我按照Android开发者网站上的指南进行了大约2个小时的搜索,但我找不到解决方案......代码看起来很好,我认为唯一可能是问题就是我创建了这个问题使用导航抽屉预设的Android Studio活动;但我仍然无法看到问题。有人能帮我吗?提前谢谢。

活动

package com.ran3000.notefication;

(...)

public class MainActivity extends ActionBarActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks {

    (...)

    EditText noteText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        noteText = (EditText) findViewById(R.id.write_note);
        getSupportActionBar().hide();
        Window window = getWindow();
        notificationBarColor.changeStatusBarColor(window, getResources().getColor(R.color.darken_green));
        setContentView(R.layout.activity_main);


        (...)


        noteText.setOnEditorActionListener(new OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) {
                return false;
            }
        });
    }

    (...)

}

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=".MainActivity$PlaceholderFragment"
android:background="#ff00f045">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff"
    android:textSize="64sp"
    android:text="@string/main_header"
    android:textStyle="bold" />

<EditText
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/write_note"
    android:background="@drawable/edit_text_outline"
    android:layout_below="@+id/textView"
    android:textSize="22sp"
    android:textColor="#ffffffff"
    android:textColorHint="#c8ffffff"
    android:layout_marginTop="42dp"
    android:inputType="textAutoCorrect|textCapSentences"
    android:imeOptions="actionSend"
    android:hint="@string/edit_text_hint" />

logcat的

Process: com.ran3000.notefication, PID: 3636
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ran3000.notefication/com.ran3000.notefication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnEditorActionListener(android.widget.TextView$OnEditorActionListener)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setOnEditorActionListener(android.widget.TextView$OnEditorActionListener)' on a null object reference
            at com.ran3000.notefication.MainActivity.onCreate(MainActivity.java:88)
            at android.app.Activity.performCreate(Activity.java:5937)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

1 个答案:

答案 0 :(得分:0)

好的,我意识到EditText不在activity_main布局中,而是在Android Studio创建的fragment_main布局中。我刚刚添加了这行代码,现在它完美无缺:

View inflatedView = getLayoutInflater().inflate(R.layout.fragment_main, null);
EditText noteText = (EditText) inflatedView.findViewById(R.id.write_note);