我试图使用Simple Framework从nasa rss(我正在关注Android HeadFirst书)中获取一些数据。 我面临的问题是,在加载主活动时,应用程序会自动关闭。
以下是代码:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
try {
print();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这是打印方法:
public void print() throws Exception
{
Rss rss = new Rss();
Serializer serializer = new Persister();
File source = new File("http://www.nasa.gov/rss/dyn/image_of_the_day.rss");
serializer.read(rss, source);
TextView text = (TextView) this.findViewById(R.id.imageTitle);
text.setText(rss.channel.title);
}
logcat的:
> 12-11 18:27:03.459: D/AndroidRuntime(20621): Shutting down VM
12-11 18:27:03.459: W/dalvikvm(20621): threadid=1: thread exiting with uncaught exception (group=0x400205a0)
12-11 18:27:03.469: E/AndroidRuntime(20621): FATAL EXCEPTION: main
12-11 18:27:03.469: E/AndroidRuntime(20621): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.nasadailyimage/com.example.nasadailyimage.MainActivity}: java.lang.ClassNotFoundException: com.example.nasadailyimage.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.nasadailyimage-1.apk]
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1743)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.os.Handler.dispatchMessage(Handler.java:99)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.os.Looper.loop(Looper.java:150)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread.main(ActivityThread.java:4277)
12-11 18:27:03.469: E/AndroidRuntime(20621): at java.lang.reflect.Method.invokeNative(Native Method)
12-11 18:27:03.469: E/AndroidRuntime(20621): at java.lang.reflect.Method.invoke(Method.java:507)
12-11 18:27:03.469: E/AndroidRuntime(20621): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-11 18:27:03.469: E/AndroidRuntime(20621): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-11 18:27:03.469: E/AndroidRuntime(20621): at dalvik.system.NativeStart.main(Native Method)
12-11 18:27:03.469: E/AndroidRuntime(20621): Caused by: java.lang.ClassNotFoundException: com.example.nasadailyimage.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.nasadailyimage-1.apk]
12-11 18:27:03.469: E/AndroidRuntime(20621): at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-11 18:27:03.469: E/AndroidRuntime(20621): at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-11 18:27:03.469: E/AndroidRuntime(20621): at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.Instrumentation.newActivity(Instrumentation.java:1040)
12-11 18:27:03.469: E/AndroidRuntime(20621): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1735)
12-11 18:27:03.469: E/AndroidRuntime(20621): ... 11 more
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.nasadailyimage"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
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.nasadailyimage.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>
我创建了一个全新的Android项目。它的工作原理
然而,即使我删除了我添加的所有库(simple.org),如果我注释掉我在MainActivity.java中添加的所有内容仍然会使应用程序关闭
我能够观察到这一行:
Serializer serialezer = new Persister();
导致错误。框架是否简单用于Android应用程序?它通常会导致这类错误吗?
更新
我也尝试过不同版本的jar。 似乎没有工作
关闭Eclipse无济于事
程序仅在序列化程序行注释 out
时运行答案 0 :(得分:0)
该行
Serializer ser = new Persister();
是导致错误的原因。我不知道简单的框架会以某种方式造成这种情况的最小天气。