如何在我的应用程序中纠正强制关闭错误

时间:2012-03-31 10:45:54

标签: java android android-layout android-intent

我可以在我的应用中找出错误。当我尝试执行我的应用程序时,会出现关闭错误。

public class Main extends Activity {
Button btnAdd;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnAdd.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent listIntent = new Intent (v.getContext(),Newbook.class) ;
            startActivity(listIntent);
        }
    });
}

}

和log cat错误

03-31 16:05:57.493: D/AndroidRuntime(387): Shutting down VM
03-31 16:05:57.493: W/dalvikvm(387): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
03-31 16:05:57.503: E/AndroidRuntime(387): Uncaught handler: thread main exiting due to uncaught exception
03-31 16:05:57.597: E/AndroidRuntime(387): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.sam.daybook/com.sam.daybook.DayBookActivity}: java.lang.ClassNotFoundException: com.sam.daybook.DayBookActivity in loader dalvik.system.PathClassLoader@44e8d908
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread.access$2200(ActivityThread.java:119)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.os.Looper.loop(Looper.java:123)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread.main(ActivityThread.java:4363)
03-31 16:05:57.597: E/AndroidRuntime(387):  at java.lang.reflect.Method.invokeNative(Native Method)
03-31 16:05:57.597: E/AndroidRuntime(387):  at java.lang.reflect.Method.invoke(Method.java:521)
03-31 16:05:57.597: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
03-31 16:05:57.597: E/AndroidRuntime(387):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
03-31 16:05:57.597: E/AndroidRuntime(387):  at dalvik.system.NativeStart.main(Native Method)
03-31 16:05:57.597: E/AndroidRuntime(387): Caused by: java.lang.ClassNotFoundException: com.sam.daybook.DayBookActivity in loader dalvik.system.PathClassLoader@44e8d908
03-31 16:05:57.597: E/AndroidRuntime(387):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
03-31 16:05:57.597: E/AndroidRuntime(387):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
03-31 16:05:57.597: E/AndroidRuntime(387):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
03-31 16:05:57.597: E/AndroidRuntime(387):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
03-31 16:05:57.597: E/AndroidRuntime(387):  ... 11 more
03-31 16:05:57.684: I/dalvikvm(387): threadid=7: reacting to signal 3
03-31 16:05:57.863: I/dalvikvm(387): Wrote stack trace to '/data/anr/traces.txt'

4 个答案:

答案 0 :(得分:0)

在缺少错误的情况下,您可以找到错误,该错误会使您的应用程序崩溃,例如:

com.sam.daybook

和类或活动如:

com.sam.daybook.DayBookActivity

,正确的错误是:

 java.lang.ClassNotFoundException: com.sam.daybook.DayBookActivity in loader dalvik.system.PathClassLoader@44e8d908 03-31 16:05:57.597:

我希望这可能对你有帮助......

答案 1 :(得分:0)

看起来Android无法找到您的活动类: 这告诉您无法从代码中实例化活动。

Unable to instantiate activity ComponentInfo{com.sam.daybook/com.sam.daybook.DayBookActivity}: 

然后,这告诉您无法找到班级com.sam.daybook.DayBookActivity

java.lang.ClassNotFoundException: com.sam.daybook.DayBookActivity in loader

班级DayBookActivity是否存在于src/com/sam/daybook/内?

您发布的代码是针对名为Main的类,如果这是您希望加载的活动,我认为您可能需要将该类重命名为DayBookActivity。您还需要将Main.java文件重命名为DayBookActivity.java

答案 2 :(得分:0)

试试这个  Intent listIntent = new Intent(Main.this,Newbook.class);

答案 3 :(得分:0)

尝试将以下行添加到项目的Android清单中

<activity android:name="NAME_OF_YOUR_CLASS">
  <intent-filter>
     <action android:name="NAME_OF_YOUR_INTENT"/>
     <category android:name="android.intent.category.DEFAULT"/>
  </intent-filter>
</activity>

然后使用以下行

开始您的活动

startActivity(new Intent("NAME_OF_YOUR_INTENT"));

确保您在上述行和清单中的意图名称完全相同。