gpt4 book ai didi

java - setOnEditorActionListener 给出 NullPointerException

转载 作者:行者123 更新时间:2023-12-03 17:47:10 27 4
gpt4 key购买 nike

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

Activity

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" />

日志猫
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)

最佳答案

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

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

关于java - setOnEditorActionListener 给出 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29312824/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com