Android开发 - 意图

时间:2012-09-26 15:01:12

标签: java android android-layout android-intent android-emulator

我最近在API版本16(4.1)上开始了Android开发。根据developer.android.com上提供的教程,我开始开发应用程序,它教我使用Intents。问题是,只要我的应用程序在模拟器上加载,它就会显示应用程序必须关闭的错误。我正在给我的xml代码。有人可以告诉我哪里出错了吗?感谢。

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" 
android:orientation="horizontal">


<EditText
    android:id="@+id/et1"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:hint="Edit text"
    android:layout_weight="1"
    tools:ignore="HardcodedText" />

<Button
    android:id="@+id/b1"
    android:layout_width="wrap_content"
    android:layout_height="40dp"
    android:text="Press"
    android:onClick="sendMessage"
    tools:ignore="HardcodedText" />

</LinearLayout>

logcat如下所示。

09-26 19:35:03.880: E/Trace(615): error opening trace file: No such file or directory     (2)
09-26 19:35:04.430: D/AndroidRuntime(615): Shutting down VM
09-26 19:35:04.430: W/dalvikvm(615): threadid=1: thread exiting with uncaught exception  (group=0x40a13300)
09-26 19:35:04.471: E/AndroidRuntime(615): FATAL EXCEPTION: main
09-26 19:35:04.471: E/AndroidRuntime(615): java.lang.RuntimeException: Unable to     instantiate activity ComponentInfo{com.learn.training1/com.learn.training1.Training1}:     java.lang.NullPointerException
 09-26 19:35:04.471: E/AndroidRuntime(615):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.os.Looper.loop(Looper.java:137)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 19:35:04.471: E/AndroidRuntime(615):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 19:35:04.471: E/AndroidRuntime(615):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 19:35:04.471: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-26 19:35:04.471: E/AndroidRuntime(615):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 19:35:04.471: E/AndroidRuntime(615):  at dalvik.system.NativeStart.main(Native Method)
09-26 19:35:04.471: E/AndroidRuntime(615): Caused by: java.lang.NullPointerException
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.Activity.findViewById(Activity.java:1825)
09-26 19:35:04.471: E/AndroidRuntime(615):  at com.learn.training1.Training1.<init>(Training1.java:15)
09-26 19:35:04.471: E/AndroidRuntime(615):  at java.lang.Class.newInstanceImpl(Native Method)
09-26 19:35:04.471: E/AndroidRuntime(615):  at java.lang.Class.newInstance(Class.java:1319)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-26 19:35:04.471: E/AndroidRuntime(615):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
09-26 19:35:04.471: E/AndroidRuntime(615):  ... 11 more
09-26 19:35:32.352: I/Process(615): Sending signal. PID: 615 SIG: 9
09-26 19:39:22.460: E/Trace(664): error opening trace file: No such file or directory (2)
09-26 19:39:22.820: D/AndroidRuntime(664): Shutting down VM
09-26 19:39:22.820: W/dalvikvm(664): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-26 19:39:22.846: E/AndroidRuntime(664): FATAL EXCEPTION: main
09-26 19:39:22.846: E/AndroidRuntime(664): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.learn.training1/com.learn.training1.Training1}: java.lang.NullPointerException
 09-26 19:39:22.846: E/AndroidRuntime(664):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.os.Looper.loop(Looper.java:137)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 19:39:22.846: E/AndroidRuntime(664):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 19:39:22.846: E/AndroidRuntime(664):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 19:39:22.846: E/AndroidRuntime(664):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-26 19:39:22.846: E/AndroidRuntime(664):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 19:39:22.846: E/AndroidRuntime(664):  at dalvik.system.NativeStart.main(Native Method)
09-26 19:39:22.846: E/AndroidRuntime(664): Caused by: java.lang.NullPointerException
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.Activity.findViewById(Activity.java:1825)
09-26 19:39:22.846: E/AndroidRuntime(664):  at com.learn.training1.Training1.<init>(Training1.java:15)
09-26 19:39:22.846: E/AndroidRuntime(664):  at java.lang.Class.newInstanceImpl(Native Method)
09-26 19:39:22.846: E/AndroidRuntime(664):  at java.lang.Class.newInstance(Class.java:1319)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-26 19:39:22.846: E/AndroidRuntime(664):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
09-26 19:39:22.846: E/AndroidRuntime(664):  ... 11 more
09-26 19:40:29.130: E/Trace(712): error opening trace file: No such file or directory (2)
09-26 19:40:29.431: D/dalvikvm(712): newInstance failed: Lcom/learn/training1/Training1; not accessible to Landroid/app/Instrumentation;
09-26 19:40:29.431: D/AndroidRuntime(712): Shutting down VM
09-26 19:40:29.431: W/dalvikvm(712): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-26 19:40:29.461: E/AndroidRuntime(712): FATAL EXCEPTION: main
09-26 19:40:29.461: E/AndroidRuntime(712): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.learn.training1/com.learn.training1.Training1}: java.lang.IllegalAccessException: access to class not allowed
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.os.Looper.loop(Looper.java:137)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 19:40:29.461: E/AndroidRuntime(712):  at java.lang.reflect.Method.invokeNative(Native Method)
09-26 19:40:29.461: E/AndroidRuntime(712):  at java.lang.reflect.Method.invoke(Method.java:511)
09-26 19:40:29.461: E/AndroidRuntime(712):  at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-26 19:40:29.461: E/AndroidRuntime(712):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 19:40:29.461: E/AndroidRuntime(712):  at dalvik.system.NativeStart.main(Native Method)
09-26 19:40:29.461: E/AndroidRuntime(712): Caused by: java.lang.IllegalAccessException: access to class not allowed
09-26 19:40:29.461: E/AndroidRuntime(712):  at java.lang.Class.newInstanceImpl(Native Method)
09-26 19:40:29.461: E/AndroidRuntime(712):  at java.lang.Class.newInstance(Class.java:1319)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-26 19:40:29.461: E/AndroidRuntime(712):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
09-26 19:40:29.461: E/AndroidRuntime(712):  ... 11 more
09-26 19:43:52.020: E/Trace(760): error opening trace file: No such file or directory (2)
09-26 19:43:52.110: D/dalvikvm(760): newInstance failed: Lcom/learn/training1/Training1; not accessible to Landroid/app/Instrumentation;
09-26 19:43:52.110: D/AndroidRuntime(760): Shutting down VM
09-26 19:43:52.110: W/dalvikvm(760): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
09-26 19:43:52.190: E/AndroidRuntime(760): FATAL EXCEPTION: main
09-26 19:43:52.190: E/AndroidRuntime(760): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.learn.training1/com.learn.training1.Training1}: java.lang.IllegalAccessException: access to class not allowed
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.os.Looper.loop(Looper.java:137)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread.main(ActivityThread.java:4745)
09-26 19:43:52.190: E/AndroidRuntime(760):  at java.lang.reflect.Method.invokeNative(Native Method)
 09-26 19:43:52.190: E/AndroidRuntime(760):     at java.lang.reflect.Method.invoke(Method.java:511)
09-26 19:43:52.190: E/AndroidRuntime(760):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-26 19:43:52.190: E/AndroidRuntime(760):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-26 19:43:52.190: E/AndroidRuntime(760):  at dalvik.system.NativeStart.main(Native Method)
09-26 19:43:52.190: E/AndroidRuntime(760): Caused by: java.lang.IllegalAccessException: access to class not allowed
09-26 19:43:52.190: E/AndroidRuntime(760):  at java.lang.Class.newInstanceImpl(Native Method)
09-26 19:43:52.190: E/AndroidRuntime(760):  at java.lang.Class.newInstance(Class.java:1319)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
09-26 19:43:52.190: E/AndroidRuntime(760):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1974)
09-26 19:43:52.190: E/AndroidRuntime(760):  ... 11 more
09-26 19:47:20.550: I/Process(810): Sending signal. PID: 810 SIG: 9

1 个答案:

答案 0 :(得分:1)

看起来你的活动没有正确宣布(也许你没有成功public?)。你应该有一个名为Training1.java的文件,在这个文件中你应该有类似

的文件
public class Training1 extends Activity {
    ...your activity code here...
}

请参阅https://groups.google.com/forum/?fromgroups=#!topic/android-developers/0Gw5WHc1J6g

编辑:添加备注

注意:此外,您不应该有任何构造函数。 Android框架会为您解决这个问题。