我正在尝试扩展ActionBarActivity。但它每次都会崩溃我的应用程序。我知道这个问题已被问到很多。但我在互联网上找到的解决方案无效。我没有导入 android-support-v7-appcompat.jar 文件,因为我导入了库项目 android-support-v7-appcompat 。
MainActivity.java:
package com.example.actionbartry;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.actionbartry"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".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>
</application>
</manifest>
Style.xml:
<resources>
<!--
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>
<color name="mycolor">#E4E4E4</color>
</resources>
日志:
12-04 00:34:59.314: I/art(2009): Rejecting re-init on previously-failed class java.lang.Class<android.support.v7.app.ActionBarActivity>
12-04 00:34:59.316: I/art(2009): Rejecting re-init on previously-failed class java.lang.Class<android.support.v7.app.ActionBarActivity>
12-04 00:34:59.325: I/art(2009): Rejecting re-init on previously-failed class java.lang.Class<com.example.actionbartry.MainActivity>
12-04 00:34:59.328: D/AndroidRuntime(2009): Shutting down VM
12-04 00:34:59.401: E/AndroidRuntime(2009): FATAL EXCEPTION: main
12-04 00:34:59.401: E/AndroidRuntime(2009): Process: com.example.actionbartry, PID: 2009
12-04 00:34:59.401: E/AndroidRuntime(2009): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.actionbartry/com.example.actionbartry.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.actionbartry.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.actionbartry-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread.access$800(ActivityThread.java:144)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.os.Handler.dispatchMessage(Handler.java:102)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.os.Looper.loop(Looper.java:135)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread.main(ActivityThread.java:5221)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.reflect.Method.invoke(Native Method)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.reflect.Method.invoke(Method.java:372)
12-04 00:34:59.401: E/AndroidRuntime(2009): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
12-04 00:34:59.401: E/AndroidRuntime(2009): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
12-04 00:34:59.401: E/AndroidRuntime(2009): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.actionbartry.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.actionbartry-1/base.apk"],nativeLibraryDirectories=[/vendor/lib, /system/lib]]
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.ClassLoader.loadClass(ClassLoader.java:469)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.Instrumentation.newActivity(Instrumentation.java:1065)
12-04 00:34:59.401: E/AndroidRuntime(2009): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)
12-04 00:34:59.401: E/AndroidRuntime(2009): ... 10 more
12-04 00:34:59.401: E/AndroidRuntime(2009): Suppressed: java.lang.NoClassDefFoundError: com.example.actionbartry.MainActivity
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.DexFile.defineClassNative(Native Method)
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.DexFile.defineClass(DexFile.java:222)
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.DexFile.loadClassBinaryName(DexFile.java:215)
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.DexPathList.findClass(DexPathList.java:321)
12-04 00:34:59.401: E/AndroidRuntime(2009): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:54)
12-04 00:34:59.401: E/AndroidRuntime(2009): ... 14 more
12-04 00:34:59.401: E/AndroidRuntime(2009): Suppressed: java.lang.ClassNotFoundException: com.example.actionbartry.MainActivity
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.Class.classForName(Native Method)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.BootClassLoader.findClass(ClassLoader.java:781)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.BootClassLoader.loadClass(ClassLoader.java:841)
12-04 00:34:59.401: E/AndroidRuntime(2009): at java.lang.ClassLoader.loadClass(ClassLoader.java:504)
12-04 00:34:59.401: E/AndroidRuntime(2009): ... 13 more
12-04 00:34:59.401: E/AndroidRuntime(2009): Caused by: java.lang.NoClassDefFoundError: Class not found using the boot class loader; no stack available
12-04 00:38:51.281: I/Process(2009): Sending signal. PID: 2009 SIG: 9