getSupportActionBar()返回null

时间:2014-06-17 13:44:17

标签: android android-actionbar android-support-library android-actionbar-compat android-actionbaractivity

我正在使用Support Actionbar。它正在MessageTemplateActivity中工作,因为当我想以getSupportActionBar().setDisplayHomeAsUpEnabled(true);显示回启用时,它会启用后退按钮。但在SyncActivitygetSupportActionBar() is returning null . Can anyone please tell me why in SyncActivity`中,它返回null。

的NullPointerException

06-17 18:45:45.327: E/AndroidRuntime(25335): Caused by: java.lang.NullPointerException
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarImplICS.getDisplayOptions(ActionBarImplICS.java:234)
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarImplICS.<init>(ActionBarImplICS.java:57)
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarImplICS.<init>(ActionBarImplICS.java:46)
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarActivityDelegateICS.createSupportActionBar(ActionBarActivityDelegateICS.java:46)
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarActivityDelegate.getSupportActionBar(ActionBarActivityDelegate.java:83)
06-17 18:45:45.327: E/AndroidRuntime(25335):    at android.support.v7.app.ActionBarActivity.getSupportActionBar(ActionBarActivity.java:66)

SyncActivity.java

public class SyncActivity extends ActionBarActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_sync_activity);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

layout_sync_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainSyncScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</RelativeLayout>

MessageTemplateActivity.java

public class MessageTemplateActivity extends ActionBarActivity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_message_template);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

}

layout.layout_message_template.xml

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/mainScrollView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ScrollView>

谢谢,

1 个答案:

答案 0 :(得分:0)

在这里找到答案:https://stackoverflow.com/a/16027689/232727 问题出现在应用于此应用程序/活动的NoTitle主题中。