gpt4 book ai didi

java - 向上导航键 : NullPointerException Java Android

转载 作者:行者123 更新时间:2023-12-02 03:41:56 24 4
gpt4 key购买 nike

我有一个 Android 应用程序,我想启用向上导航键,但我的布局的向上键不显示,当我运行时,我从中得到以下 NullPointerException:

04-20 16:06:48.722 2420-2420/spmrs.mari.com.marispmrs_api19 E/AndroidRuntime: FATAL EXCEPTION: main
Process: spmrs.mari.com.marispmrs_api19, PID: 2420
java.lang.RuntimeException: Unable to start activity ComponentInfo{spmrs.mari.com.marispmrs_api19/spmrs.mari.com.marispmrs_api19.SmsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference
at spmrs.mari.com.marispmrs_api19.SmsActivity.onCreate(SmsActivity.java:54)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
at android.app.ActivityThread.-wrap11(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:148) 
at android.app.ActivityThread.main(ActivityThread.java:5417) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 

这是我的 AndroidManifest.xml 的 SmsActivity

<activity
android:name=".SmsActivity"
android:label="@string/title_activity_sms"
android:theme="@style/AppTheme.NoActionBar"
android:parentActivityName=".SoilData">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".SoilData" />
</activity>

这是 SmsActivity 代码块,它给了我一个空指针异常:

//        Enabling up navigation
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);

有人可以帮助我找出错误的根本原因以及如何解决它吗?

最佳答案

您设置了支持操作栏吗?

Activity 主要 onCreate:

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
}

Activity 主布局

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" />

关于java - 向上导航键 : NullPointerException Java Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36744883/

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