【问题标题】:setOnEditorActionListener gives NullPointerExceptionsetOnEditorActionListener 给出 NullPointerException
【发布时间】:2015-03-28 01:59:41
【问题描述】:

我对@9​​87654321@ 有疑问,我不明白为什么它会导致应用程序崩溃。我按照 Android 开发者网站上的指南搜索了大约 2 个小时,但我找不到解决方案......代码对我来说看起来不错,我认为唯一可能是问题是我创建了 Activity在 Android Studio 中使用 Navigation Drawer 预设;但我仍然看不到问题所在。有人能帮我吗?提前致谢。

活动

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)

【问题讨论】:

  • 代码太多了。
  • noteText = (EditText) findViewById(R.id.write_note); 应该在setContentView(...) 调用之后。您正在尝试定位 a View,然后再给 Activity 一个 View 进行内部搜索。
  • @Vikram 没有任何改变,同样的错误......
  • @Jagannath 我减少了代码!!

标签: java android nullpointerexception crash android-edittext


【解决方案1】:

好的,我意识到 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);

【讨论】:

    猜你喜欢
    • 2012-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多