我是Android开发的初学者,所以我正在观看教程并按照步骤操作,但程序进展不顺利。
我在项目中导入了v7 appcompat
。 (但是v21文件存在问题,但是我在project.properties中从19更改为21,因此修复了。)
然而,当我在我的Java文件中扩展Activity类时,AVM工作正常,但是当我扩展ActionBarActivity时,AVM继续说"不幸的是程序停止了。"
这是java代码:
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.widget.ListView;
public class Calculator extends ActionBarActivity {
private DrawerLayout drawerLayout;
private ListView listView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calculator);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
listView = (ListView) findViewById(R.id.drawerList);
}
}
这是xml代码:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="@+id/mainContent"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="@+id/drawerList"
android:entries="@array/left_navigation"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</ListView>
</android.support.v4.widget.DrawerLayout>
谁能告诉我出了什么问题?
其他 LogCat信息在这里:
02-18 02:38:12.125: D/AndroidRuntime(3041): Shutting down VM
02-18 02:38:12.131: E/AndroidRuntime(3041): FATAL EXCEPTION: main
02-18 02:38:12.131: E/AndroidRuntime(3041): Process: example, PID: 3041
02-18 02:38:12.131: E/AndroidRuntime(3041): java.lang.NoClassDefFoundError: Failed resolution of: Landroid/support/v7/appcompat/R$styleable;
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:138)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
02-18 02:38:12.131: E/AndroidRuntime(3041): at example.Calculator.onCreate(Calculator.java:15)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.Activity.performCreate(Activity.java:5933)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.ActivityThread.access$800(ActivityThread.java:144)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.os.Handler.dispatchMessage(Handler.java:102)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.os.Looper.loop(Looper.java:135)
02-18 02:38:12.131: E/AndroidRuntime(3041): at android.app.ActivityThread.main(ActivityThread.java:5221)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.reflect.Method.invoke(Native Method)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.reflect.Method.invoke(Method.java:372)
02-18 02:38:12.131: E/AndroidRuntime(3041): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
02-18 02:38:12.131: E/AndroidRuntime(3041): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
02-18 02:38:12.131: E/AndroidRuntime(3041): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.support.v7.appcompat.R$styleable" on path: DexPathList[[zip file "/data/app/example-2/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
02-18 02:38:12.131: E/AndroidRuntime(3041): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
02-18 02:38:12.131: E/AndroidRuntime(3041): ... 17 more
02-18 02:38:12.131: E/AndroidRuntime(3041): Suppressed: java.lang.ClassNotFoundException: android.support.v7.appcompat.R$styleable
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.Class.classForName(Native Method)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
02-18 02:38:12.131: E/AndroidRuntime(3041): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
02-18 02:38:12.131: E/AndroidRuntime(3041): ... 18 more
02-18 02:38:12.131: E/AndroidRuntime(3041): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
答案 0 :(得分:0)
“您需要在此活动中使用Theme.AppCompat
主题(或后代)。”您正在将appcompat库用作库项目,在这种情况下,您将在该项目中的res/values
下看到可用的主题。你需要在清单中使用合适的风格。
要测试,您可以在清单的application
节点中添加它
android:theme="@style/Theme.AppCompat.Light"
答案 1 :(得分:0)
如果您要在低于11的api上使用ActionBar以支持api&gt; = 7签出http://developer.android.com/guide/topics/ui/actionbar.html
,则可以扩展ActionBarActivity此外,它可能会引起混淆,但是当您使用ActionBarActivity时,您的主题必须是Theme.AppCompact后代之一的子代,例如
<style name="MyTheme" parent="Theme.AppCompact">
<item name="android:colorPrimary">@color/accent_1</item>
<item name="android:colorPrimaryDark">@color/accent_dark</item>
<item name="android:colorAccent">#f00</item>
<item name="colorPrimary">@color/accent_1</item>
<item name="colorPrimaryDark">@color/accent_dark</item>
<item name="colorAccent">#f00</item>
</style>