Java Android中操作栏setDisplayShowHomeEnabled()处的nullpointerexception
我创建自定义操作栏我真的不知道我的错误在哪里。我在创建自定义操作栏时遇到了一些问题Java Android中操作栏setDisplayShowHomeEnabled()处的nullpointerexception,java,android,nullpointerexception,Java,Android,Nullpointerexception,我创建自定义操作栏我真的不知道我的错误在哪里。我在创建自定义操作栏时遇到了一些问题 并在行=mActionBar.setDisplayShowHomeEnabled处显示空指针异常(false) 这是我的错误日志 07-29 16:19:20.180 32714-32714/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main java.lang.RuntimeException: Unable to s
并在行=mActionBar.setDisplayShowHomeEnabled处显示空指针异常(false)强> 这是我的错误日志
07-29 16:19:20.180 32714-32714/com.example.tazeen.classnkk E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tazeen.classnkk/com.example.tazeen.classnkk.AllPosts_Page}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.tazeen.classnkk.AllPosts_Page.CustomActionBar(AllPosts_Page.java:37)
at com.example.tazeen.classnkk.AllPosts_Page.onCreate(AllPosts_Page.java:28)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
我试了5到7个小时,但没有找到合适的解决方案。请帮助我。谢谢
这是我的活动代码
public class AllPosts_Page extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.all_post);
CustomActionBar();
}
public void CustomActionBar()
{
android.app.ActionBar mActionBar = getActionBar();
mActionBar.setDisplayShowHomeEnabled(false);
mActionBar.setDisplayShowTitleEnabled(false);
LayoutInflater mInflater = LayoutInflater.from(this);
View mCustomView = mInflater.inflate(R.layout.custom_action_bar, null);
TextView mTitleTextView = (TextView) mCustomView.findViewById(R.id.txtTitle);
mTitleTextView.setText("All Post");
ImageView imageButton = (ImageView) mCustomView
.findViewById(R.id.imgLeftMenu);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Refresh Clicked!",
Toast.LENGTH_LONG).show();
}
});
mActionBar.setCustomView(mCustomView);
mActionBar.setDisplayShowCustomEnabled(true);
}
梅尼菲斯特锉刀
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar">
<activity
android:name=".Splash_Screen"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login_Screen" />
<activity
android:name=".AllPosts_Page">
</activity>
<activity
android:name=".Filter_Page"
android:label="@string/title_activity_filter__page"
android:theme="@style/ListFont">
</activity>
</application>
</manifest>
style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="android:height">5dp</item>
</style>
</resources>
5dp
您使用的是Theme.AppCompat.Light.darkaActionBar
,因此您应该通过getSupportActionBar()
方法,而不是getActionBar()
如果扩展的活动
来自Android系统,则样式不正确(AppCompat),并且活动在没有ActionBar的情况下获得一些默认样式,因此getActionBar()
返回null
否则,如果您想使用此AppCompat样式,则它适用于AppCompatActivity
(然后使用shouldgetSupportActionBar()
)可能会显示一些代码?感谢您的回复。我在上面发布了一些代码,行=mActionBar.setDisplayShowHomeEnabled处出现空指针异常(false);您的mActionBar
为空<代码>getActionBar()
是查找问题的正确位置。我不明白你在说什么?意味着我必须更改编码的位置。你能解释一下吗。但是当我使用这个ActionBar时,mAct=getSupportActionBar();不兼容的类型需要android.app.ActionBar,找到了:android.support.v7.app.ActionBar。当导入android.app.ActionBar时,getSupportActionBar()无法获得支持。谢谢,它解决了我的问题。谢谢。但我的自定义操作栏设置不正确,它显示左侧和右侧的默认操作栏空间。