当我通过manifest.xml进行全屏活动时,我的应用程序崩溃了

时间:2013-11-30 11:51:01

标签: android

我的主要活动是全屏模式,没问题。

但是当我试图去“教程”活动时,它会崩溃。

即时删除android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen 它工作得很好。

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="19" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.randomator.MainActivity"
        android:label="@string/app_name"
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
         >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.randomator.Tutorial"
        android:label="@string/title_activity_tutorial"
        android:parentActivityName="com.example.randomator.MainActivity"
        android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.randomator.MainActivity" />
    </activity>

</application>

可能是什么问题?

logcat
11-30 14:09:09.972: D/AndroidRuntime(10534): Shutting down VM
11-30 14:09:09.982: W/dalvikvm(10534): threadid=1: thread exiting with uncaught     exception (group=0x409dc1f8)
11-30 14:09:09.982: E/AndroidRuntime(10534): FATAL EXCEPTION: main
11-30 14:09:09.982: E/AndroidRuntime(10534): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.randomator/com.example.randomator.Tutorial}: java.lang.NullPointerException
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at  android.os.Handler.dispatchMessage(Handler.java:99)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at   android.os.Looper.loop(Looper.java:137)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread.main(ActivityThread.java:4424)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at java.lang.reflect.Method.invokeNative(Native Method)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at java.lang.reflect.Method.invoke(Method.java:511)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at dalvik.system.NativeStart.main(Native Method)
11-30 14:09:09.982: E/AndroidRuntime(10534): Caused by: java.lang.NullPointerException
11-30 14:09:09.982: E/AndroidRuntime(10534):    at com.example.randomator.Tutorial.setupActionBar(Tutorial.java:37)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at com.example.randomator.Tutorial.onCreate(Tutorial.java:22)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at   android.app.Activity.performCreate(Activity.java:4492)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
11-30 14:09:09.982: E/AndroidRuntime(10534):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
11-30 14:09:09.982: E/AndroidRuntime(10534):    ... 11 more
11-30 14:09:10.262: I/dalvikvm(10534): threadid=3: reacting to signal 3
11-30 14:09:10.262: I/dalvikvm(10534): Wrote stack traces to '/data/anr/traces.txt'
11-30 14:09:10.502: I/dalvikvm(10534): threadid=3: reacting to signal 3
11-30 14:09:10.502: I/dalvikvm(10534): Wrote stack traces to '/data/anr/traces.txt'
11-30 14:09:11.732: I/Process(10534): Sending signal. PID: 10534 SIG: 9

Tutorial.class     public class Tutorial扩展了Activity {

@SuppressWarnings("unused")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tutorial);
    // Show the Up button in the action bar.
    setupActionBar();
    Intent ReciveFromMain = getIntent();





}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.tutorial, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
    case android.R.id.home:
        // This ID represents the Home or Up button. In the case of this
        // activity, the Up button is shown. Use NavUtils to allow users
        // to navigate up one level in the application structure. For
        // more details, see the Navigation pattern on Android Design:
        //
        // http://developer.android.com/design/patterns/navigation.html#up-vs-back
        //
        NavUtils.navigateUpFromSameTask(this);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

2 个答案:

答案 0 :(得分:1)

设置

  android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen

您的活动的主题,然后Activity将占据整个屏幕,根本不会有ActionBar

在这种情况下,当你打电话时

 getActionBar();

setupActionBar()中,它会返回null,因为全屏Activity不会包含ActionBar

答案 1 :(得分:0)

不要将android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen"提供给每个单独的活动,只需尝试将其放在application tag内。希望它有效。

示例代码:

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@android:style/Theme.Light.NoTitleBar.Fullscreen" >
    <activity
        android:name="com.example.randomator.MainActivity"
        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="com.example.randomator.Tutorial"
        android:label="@string/title_activity_tutorial"
        android:parentActivityName="com.example.randomator.MainActivity"
        >
        <meta-data
            android:name="android.support.PARENT_ACTIVITY"
            android:value="com.example.randomator.MainActivity" />
    </activity>

</application>