扩展ActionBarActivity时应用程序崩溃

时间:2013-09-21 14:32:23

标签: android illegalstateexception android-appcompat android-actionbaractivity

我延长ActionBarActivity时遇到错误。如果我延长Activity则没有错误。

事情在我的其他活动中完美运作。起初,我认为这是因为我之前的活动在意图中加入了额外的内容。但即使没有它,应用程序也会崩溃。

这是我的代码:

import android.support.v7.app.ActionBarActivity;

import android.os.Bundle;

public class FullscreenActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fullscreen);
    }
}

编辑:以及日志结果:

09-21 18:21:39.413: E/AndroidRuntime(26944): FATAL EXCEPTION: main
09-21 18:21:39.413: E/AndroidRuntime(26944): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testlfm/com.example.testlfm.FullscreenActivity}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread.access$600(ActivityThread.java:153)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.os.Looper.loop(Looper.java:137)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread.main(ActivityThread.java:5227)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at java.lang.reflect.Method.invoke(Method.java:511)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at dalvik.system.NativeStart.main(Native Method)
09-21 18:21:39.413: E/AndroidRuntime(26944): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:102)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.support.v7.app.ActionBarActivityDelegateICS.onCreate(ActionBarActivityDelegateICS.java:58)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:98)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at com.example.testlfm.FullscreenActivity.onCreate(FullscreenActivity.java:36)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.Activity.performCreate(Activity.java:5104)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
09-21 18:21:39.413: E/AndroidRuntime(26944):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
09-21 18:21:39.413: E/AndroidRuntime(26944):    ... 11 more

4 个答案:

答案 0 :(得分:9)

根据您发布的错误:“您需要在此活动中使用Theme.AppCompat主题(或后代)。”您应该将appcompat库用作库项目,在这种情况下,您将看到该项目中res / values下的可用主题

答案 1 :(得分:3)

<?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>

答案 2 :(得分:0)

如果项目中没有assets文件夹,请在src / main下创建一个。将您的字体文件复制到assets文件夹中。

你可以在你的java文件中使用

       //字体路径         String fontPath =“MyCustomFOnt.TTF”;

    EditText txtCustom = (EditText) findViewById(R.id.editor1);
    // Loading Font Face
    Typeface tf = Typeface.createFromAsset(getAssets(), fontPath);
    // Applying font
    txtCustom.setTypeface(tf);

答案 3 :(得分:0)

我遇到了同样的问题并且尝试了很多东西(主题,导入外部appcompat jar等) 唯一一个对我有帮助:
1)手动将android-support-v4.jar和android-support-v7-appcompat.jar复制到项目的lib文件夹中;
2)右键单击项目 - &gt;属性 - &gt;图书馆 - &gt;添加JAR(不是外部罐子) - &gt;从lib文件夹中选择jar(我们在步骤1中复制) - &gt;订单和出口点按 - &gt;取消选中旧的支持库并选中我们刚刚添加的new。 - &GT;行。