我想创建一个Andriod 2.1兼容的操作栏并导入android.support.v7.app.ActionBarActivity包。我将MainActivity类扩展到ActionBarActivity,现在运行应用程序后,我不断得到“遗憾的应用程序已经停止”。 我附上了主类代码。 谢谢您的帮助, Nitzan
public class MainActivity extends ActionBarActivity {
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
和logcat:
01-02 16:21:15.900: D/AndroidRuntime(792): Shutting down VM
01-02 16:21:15.900: W/dalvikvm(792): threadid=1: thread exiting with uncaught exception (group=0xb3abbb90)
01-02 16:21:15.960: E/AndroidRuntime(792): FATAL EXCEPTION: main
01-02 16:21:15.960: E/AndroidRuntime(792): Process: com.theredpill.myfirstapp, PID: 792
01-02 16:21:15.960: E/AndroidRuntime(792): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.theredpill.myfirstapp/com.theredpill.myfirstapp.MainActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.os.Handler.dispatchMessage(Handler.java:102)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.os.Looper.loop(Looper.java:137)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-02 16:21:15.960: E/AndroidRuntime(792): at java.lang.reflect.Method.invokeNative(Native Method)
01-02 16:21:15.960: E/AndroidRuntime(792): at java.lang.reflect.Method.invoke(Method.java:515)
01-02 16:21:15.960: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-02 16:21:15.960: E/AndroidRuntime(792): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-02 16:21:15.960: E/AndroidRuntime(792): at dalvik.system.NativeStart.main(Native Method)
01-02 16:21:15.960: E/AndroidRuntime(792): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
01-02 16:21:15.960: E/AndroidRuntime(792): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:111)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
01-02 16:21:15.960: E/AndroidRuntime(792): at com.theredpill.myfirstapp.MainActivity.onCreate(MainActivity.java:17)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.Activity.performCreate(Activity.java:5243)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-02 16:21:15.960: E/AndroidRuntime(792): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-02 16:21:15.960: E/AndroidRuntime(792): ... 11 more
01-02 16:20:01.225: I/Process(792): Sending signal. PID: 792 SIG: 9
并且AndroidManifest.xml中的xml包含以下主题标记:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.theredpill.myfirstapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<activity
android:name="com.theredpill.myfirstapp.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.theredpill.myfirstapp.DisplayMessageActivity"
android:label="@string/title_activity_display_message"
android:parentActivityName="com.example.myfirstapp.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.myfirstapp.MainActivity" />
</activity>
</application>
</manifest>