我在设备中运行当前的android项目时遇到了一些麻烦。该项目没有构建错误(使用maven),但是当我运行时会发生此错误:
E/AndroidRuntime(12097): FATAL EXCEPTION: main
E/AndroidRuntime(12097): Process: org.hello, PID: 12097
E/AndroidRuntime(12097): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.hello/org.hello.MainActivity}: java.lang.ClassNotFoundException: Didn't find class "org.hello.MainActivity" on path: DexPathList[[zip file "/data/app/org.hello-1.apk"],nativeLibraryDirectories=[/data/app-lib/org.hello-1, /vendor/lib, /system/lib]]
E/AndroidRuntime(12097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
E/AndroidRuntime(12097): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
E/AndroidRuntime(12097): at android.app.ActivityThread.access$800(ActivityThread.java:139)
E/AndroidRuntime(12097): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
E/AndroidRuntime(12097): at android.os.Handler.dispatchMessage(Handler.java:102)
E/AndroidRuntime(12097): at android.os.Looper.loop(Looper.java:136)
E/AndroidRuntime(12097): at android.app.ActivityThread.main(ActivityThread.java:5086)
E/AndroidRuntime(12097): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(12097): at java.lang.reflect.Method.invoke(Method.java:515)
E/AndroidRuntime(12097): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
E/AndroidRuntime(12097): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
E/AndroidRuntime(12097): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(12097): Caused by: java.lang.ClassNotFoundException: Didn't find class "org.hello.MainActivity" on path: DexPathList[[zip file "/data/app/org.hello-1.apk"],nativeLibraryDirectories=[/data/app-lib/org.hello-1, /vendor/lib, /system/lib]]
E/AndroidRuntime(12097): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
E/AndroidRuntime(12097): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
E/AndroidRuntime(12097): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
E/AndroidRuntime(12097): at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
E/AndroidRuntime(12097): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
E/AndroidRuntime(12097): ... 11 more
W/ActivityManager( 916): Force finishing activity org.hello/.MainActivity
我的MainActivity类是:
package org.hello;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTabHost;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MenuInflater;
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab1"), Tab1Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab2"), Tab2Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab3"), Tab3Fragment.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab4").setIndicator("Tab4"), Tab4Fragment.class, null);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle presses on the action bar items
switch (item.getItemId()) {
case R.id.action_search:
openSearch();
return true;
case R.id.action_settings:
openSettings();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
public void openSearch() {
//
}
public void openSettings() {
//
}
}
我的pom.xml是:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.hello</groupId>
<artifactId>basic-tabs</artifactId>
<version>0.1.0</version>
<packaging>apk</packaging>
<properties>
<!-- use UTF-8 for everything -->
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<android.sdk.path>/home/kleber/android-sdk-linux/</android.sdk.path>
</properties>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>4.1.1.4</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>support-v4</artifactId>
<version>20.0.0</version>
<scope>system</scope>
<systemPath>/home/kleber/android-sdk-linux/extras/android/support/v4/android-support-v4.jar</systemPath>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>android-maven-plugin</artifactId>
<version>3.9.0-rc.1</version>
<configuration>
<sdk>
<platform>19</platform>
</sdk>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
<showDeprecation>true</showDeprecation>
<showWarnings>true</showWarnings>
<executable>/home/kleber/jdk1.7.0_55/bin/javac</executable>
<fork>true</fork>
</configuration>
</plugin>
</plugins>
</build>
</project>
类MainActivity被放置在我的项目内的正确目录中(与其他项目一样,运行没有问题)。
如果我删除了依赖项support-v4
(以及我的代码中从该库中引用的所有内容),运行此项目没有问题。
任何人都可以看到我在这里缺少的东西吗?
ps:有问题的项目是:https://github.com/klebermo/basic_tab2
答案 0 :(得分:2)
我记不清楚但是我使用了这个support-v4库时遇到了同样的错误,我甚至没有使用Maven。但是既然你真的非常渴望得到帮助,我认为这可以解决我的问题。如果您有Eclipse(我不了解其他IDE),请转到构建路径并转到Order and Export并在support-v4库中删除或设置Export。
答案 1 :(得分:1)
清理项目并删除项目目录下的/gen
和/bin
文件夹,然后重建它!
答案 2 :(得分:1)
您是否尝试过直接在项目中包含jar,看它是否有所作为?