更改样式时Android应用程序崩溃

时间:2014-06-19 09:42:10

标签: android android-styles

我希望我的程序具有背景颜色#222222。我在styles.xml文件夹中的values创建了一个新样式。

styles.xml:

<resources xmlns:android="http://schemas.android.com/apk/res/android">
    <!--
        Base application theme, dependent on API level. This theme is replaced
        by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
    -->
    <style name="AppBaseTheme" parent="android:Theme.Light">
        <!--
            Theme customizations available in newer API levels can go in
            res/values-vXX/styles.xml, while customizations related to
            backward-compatibility can go here.
        -->
    </style>

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

    <style name="MyTheme" parent="android:Theme">
        <item name="android:textViewStyle">@style/MyTextViewStyle</item>
        <item name="android:background">#222222</item>
    </style>

    <style name="MyTextViewStyle" parent="android:Widget.TextView">
        <item name="android:textColor">#F00</item>
    </style>
</resources>

在我的清单文件中,它看起来像这样:

[...]

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/MyTheme" >

[...]

我收到这些错误:

06-19 11:41:13.745: E/AndroidRuntime(28370): FATAL EXCEPTION: main
06-19 11:41:13.745: E/AndroidRuntime(28370): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.davlog.dmusicplayer/com.davlog.dmusicplayer.MainActivity}: java.lang.NullPointerException
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2249)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2299)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread.access$700(ActivityThread.java:154)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1284)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.os.Looper.loop(Looper.java:137)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread.main(ActivityThread.java:5306)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at dalvik.system.NativeStart.main(Native Method)
06-19 11:41:13.745: E/AndroidRuntime(28370): Caused by: java.lang.NullPointerException
06-19 11:41:13.745: E/AndroidRuntime(28370):     at com.davlog.dmusicplayer.MainActivity.onCreate(MainActivity.java:37)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.Activity.performCreate(Activity.java:5255)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
06-19 11:41:13.745: E/AndroidRuntime(28370):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2213)
06-19 11:41:13.745: E/AndroidRuntime(28370):     ... 11 more

MainActivity Code:

public class MainActivity extends FragmentActivity implements ActionBar.TabListener {

    @Override
    public void onTabReselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabSelected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onTabUnselected(Tab arg0, FragmentTransaction arg1) {
        // TODO Auto-generated method stub
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Set up the action bar.
        final ActionBar actionBar = getActionBar();
        actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);

        // For each of the sections in the app, add a tab to the action bar.
         actionBar.addTab(actionBar.newTab().setText("Songs").setTabListener(this));
    }
}

0 个答案:

没有答案