gpt4 book ai didi

java - FragmentActivity 的 ActionBar 出现空指针异常

转载 作者:太空宇宙 更新时间:2023-11-03 11:37:04 26 4
gpt4 key购买 nike

我在运行时遇到这个错误。

java.lang.RuntimeException: Unable to resume activity java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference

堆栈跟踪:

01-22 06:32:55.509: E/AndroidRuntime(3728): FATAL EXCEPTION: main
01-22 06:32:55.509: E/AndroidRuntime(3728): Process: com.qrme.quranmadeeasy, PID: 3728
01-22 06:32:55.509: E/AndroidRuntime(3728): java.lang.RuntimeException: Unable to resume activity {com.qrme.quranmadeeasy/com.qrme.quranmadeeasy.InnerPageActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2951)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2982)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.access$800(ActivityThread.java:144)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.os.Handler.dispatchMessage(Handler.java:102)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.os.Looper.loop(Looper.java:135)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.main(ActivityThread.java:5221)
01-22 06:32:55.509: E/AndroidRuntime(3728): at java.lang.reflect.Method.invoke(Native Method)
01-22 06:32:55.509: E/AndroidRuntime(3728): at java.lang.reflect.Method.invoke(Method.java:372)
01-22 06:32:55.509: E/AndroidRuntime(3728): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
01-22 06:32:55.509: E/AndroidRuntime(3728): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
01-22 06:32:55.509: E/AndroidRuntime(3728): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayShowHomeEnabled(boolean)' on a null object reference
01-22 06:32:55.509: E/AndroidRuntime(3728): at com.qrme.quranmadeeasy.InnerPageActivity.ActionBar(InnerPageActivity.java:203)
01-22 06:32:55.509: E/AndroidRuntime(3728): at com.qrme.quranmadeeasy.InnerPageActivity.onResume(InnerPageActivity.java:71)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1241)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.Activity.performResume(Activity.java:6019)
01-22 06:32:55.509: E/AndroidRuntime(3728): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2940)
01-22 06:32:55.509: E/AndroidRuntime(3728): ... 11 more

InnerPageActivity.java:

public class InnerPageActivity extends FragmentActivity {

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

setContentView(R.layout.copyactivity_single_page);

}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
initialize();
ActionBar(); --->71 st line
}


public void ActionBar() {
ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false); -->203rd line
mActionBar.setDisplayShowTitleEnabled(false);
mActionBar.setBackgroundDrawable(new ColorDrawable(Color
.parseColor("#356AA0")));

mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
}

样式.xml:

<resources>

<style name="AppBaseTheme" parent="@style/Theme.AppCompat"></style>

<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>

list .xml:

    <uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="21" />

<application
android:name="com.qrme.quranmadeeasy.Application"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
........
<application>

我不知道如何解决这个问题。任何人都可以帮助我解决这个问题。谢谢。

最佳答案

因为您使用的是支持主题 (AppCompat) 和支持 Activity (FragmentActivity),而不是

getActionBar()

打电话

getSupportActionBar()

关于java - FragmentActivity 的 ActionBar 出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28082784/

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