Android:无法实例化活动/ClassNotFoundException [英] Android: Unable to instantiate activity / ClassNotFoundException
问题描述
我最近向市场发布了一个应用程序,但我现在收到了一些用户的错误消息,该应用程序可能在启动时就崩溃了.不幸的是,我无法直接与他联系,并且该应用程序在模拟器和我的手机(以及一些朋友的手机)上运行良好.
我想在我收到评论时,这种情况发生在不止一个用户身上在市场上,例如启动时崩溃"或不起作用".我只收到了这一个堆栈跟踪,但没有关于配置、设备、Android 版本等的信息.
I recently published an app to the market and I'm now getting an error by some user, the app presumably crashes right when it starts. Unfortunately I can't contact him directly and the app works fine in the emulator as well as on my phone (and some friends' phones).
I guess that this happens to more than one user as I received comments in the market like "crashes on start" or "doesn't work". I only received this one stacktrace but there's no info about the configuration, device, Android version, etc.
该应用程序是一个简单的音板,所以真的没有任何魔法,但我不明白为什么它在某些手机上会失败.这是我得到的堆栈跟踪,我希望有人能帮助我:
The app is a simple soundboard, so there's really no magic involved, but I can't get why it fails on some phones. Here's the stack trace I'm getting, I hope anybody can help me out:
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.my.app/com.my.app.SoundMachine}: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
at android.app.ActivityThread.access$2300(ActivityThread.java:125)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:123)
at android.app.ActivityThread.main(ActivityThread.java:4627)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:876)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:634)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassNotFoundException: com.my.app.SoundMachine in loader dalvik.system.PathClassLoader[/mnt/asec/com.my.app-1/pkg.apk]
at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
... 11 more
这是我活动的前几行:
public class SoundMachine extends Activity {
private SoundManager mSoundManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
这是(几乎)完整的 onCreate:
This is the (almost) complete onCreate:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
int counter = 0;
for (Integer soundFile : soundFiles) {
counter++;
mSoundManager.addSound(counter, soundFile);
}
ImageButton SoundButton1 = (ImageButton) findViewById(R.id.sound1);
SoundButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.playSound(1);
}
});
SoundButton1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
saveSoundChoice(soundFiles[0], soundNames[0]);
return true;
}
});
(...more of this...)
Button StopButton = (Button) findViewById(R.id.stopbutton);
StopButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mSoundManager.stopAll();
}
});
}
这是我的清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.my.app" android:installLocation="preferExternal"
android:versionCode="9" android:versionName="1.2">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".SoundMachine" android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-sdk android:minSdkVersion="3" android:targetSdkVersion="8" />
</manifest>
因此,我已经在此处和某些论坛中读到的所有可能的错误都不适用于我的应用.
So all possible errors I already read about here and in some forums don't apply to my app.
- 活动存在于清单中.
- 在覆盖方法中调用超级方法.
- 在访问视图中的元素之前设置 ContentView.
我知道在无法重现错误的情况下很难查明错误的根源,但也许有人有一个好主意可以帮助我.
I know it's hard to pinpoint the source of an error without being able to reproduce it, but maybe somebody has a bright idea and can help me out.
一些问题:
我需要清单中的意图"部分吗?Eclipse 在我创建项目时创建了它.(是的,根据 Mayra)应该在它所在的地方调用超级方法吗?(是的,根据 Mayra 的说法)
Do I need the "intent"-part in the manifest? Eclipse created it when I created the project.(Yes, according to Mayra)Should the super-method be called where it is?(Yes, according to Mayra)
现在剩下的主要问题是:为什么 PathClassLoader 中的路径与我的包名不同?John J Smith 发布的页面似乎处理了同样的问题,但我不明白在那里应用的修复.
The main question that remains now is: How come the path in PathClassLoader is different from my package-name? The page John J Smith posted seems to deal with the same problem, but I don't understand the fix that was applied there.
谢谢,Select0r
Thanks, Select0r
推荐答案
自 ADT 更新到修订版 22(2013 年 5 月)以来,您必须在 Project -> Properties -> Java Build 中选中Android Private Libraries"复选框Path -> Order and Export 在 Eclipse 中为你的旧项目摆脱这个异常......
Since ADT update to revision 22 (May 2013) you have to check "Android Private Libraries" check box in Project -> Properties -> Java Build Path -> Order and Export in Eclipse for your older projects to get rid of this exception ...
这篇关于Android:无法实例化活动/ClassNotFoundException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!