我在我的应用程序中使用Action Bar,扩展了ActionBarActivity,但它在Logcat中给出了以下错误,
02-12 21:59:53.252: E/AndroidRuntime(7062): FATAL EXCEPTION: main
02-12 21:59:53.252: E/AndroidRuntime(7062): Process: com.example.a_bar, PID: 7062
02-12 21:59:53.252: E/AndroidRuntime(7062): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.a_bar/com.example.a_bar.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "com.example.a_bar.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.a_bar-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.a_bar-1, /system/lib, /data/downloads]]
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.os.Handler.dispatchMessage(Handler.java:102)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.os.Looper.loop(Looper.java:136)
02-12 21:59:53.252: E/AndroidRuntime(7062): at android.app.ActivityThread.main(ActivityThread.java:5021)
02-12 21:59:53.252: E/AndroidRuntime(7062): at java.lang.reflect.Method.invokeNative(Native Method)
02-12 21:59:53.252: E/AndroidRuntime(7062): at java.lang.reflect.Method.invoke(Method.java:515)
02-12 21:59:53.252: E/AndroidRuntime(7062): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
02-12 21:59:53.252: E/AndroidRuntime(7062): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
02-12 21:59:53.252: E/AndroidRuntime(7062): at dalvik.system.NativeStart.main(Native Method)
02-12 21:59:53.252: E/AndroidRuntime(7062): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.a_bar.MainActivity" on path: DexPathList[[zip file "/data/app/com.example.a_bar-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.example.a_bar-1, /system/lib, /data/downloads]]
清单如下,
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a_bar"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.a_bar.MainActivity"
android:label="@string/app_name"
android:theme="@style/Theme.AppCompat.Light" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
style.xml如下,
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"> </style>
<style name="AppTheme" parent="AppBaseTheme"> </style>
</resources>
MainActivity如下,
package com.example.a_bar;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
我在我的应用程序中添加了作为库项目android-support-v7-appcompat。 并将主题添加到Manifest中,
android:theme="@style/Theme.AppCompat.Light"
我的应用程序在声明时崩溃了。 为什么生成ClassNotFoundException?但是当我扩展Activity而不是ActionBarActivity时,它正在运行。
答案 0 :(得分:2)
无需导入android.support.v7.app.ActionBarActivity
,因为项目的minSdkVersion
为11,因此即使您不扩展ActionBarActivity
,ActionBar也已包含在您的应用中。仅当您的应用程序的ActionBar
小于11 时,才需要为minSdkVersion
导入支持库。
只需导入android.app.Activity
并将您的课程扩展到Activity
。
答案 1 :(得分:0)
存在依赖性问题。添加support-v4库并希望它能解决您的问题。