Java – null pointer exception on actionbar on fragmentactivity
•
Java
I received this error while running
Stack trace:
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); } }
styles. 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>
manifest. XML file:
<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>
I don't know how to solve the problem Anyone can help me solve this problem thank you.
Solution
You should change
public class InnerPageActivity extends FragmentActivity
to
public class InnerPageActivity extends ActionBarActivity
In addition, it also changed mcactionbar = getsupportactionbar();
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码