我是 Android 新手,我已经启动了一个新手应用程序来玩事件侦听器,当我点击它时它会更改按钮的文本......遵循开发人员指南(http://developer.android.com /guide/topics/ui/ui-events.html#EventListeners),我创建了带有视图( TextView 和 Button )和主 Activity 的布局。但是当我编译 de 代码时,将 .apk ( Eclipse ) 发送到 ADV 并启动应用程序时,我得到了错误列表:
04-26 21:37:22.615: D/dalvikvm(637): Not late-enabling CheckJNI (already on)
04-26 21:37:23.845: I/dalvikvm(637): threadid=3: reacting to signal 3
04-26 21:37:24.205: I/dalvikvm(637): Wrote stack traces to '/data/anr/traces.txt'
04-26 21:37:24.805: D/AndroidRuntime(637): Shutting down VM
04-26 21:37:24.805: W/dalvikvm(637): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-26 21:37:24.906: E/AndroidRuntime(637): FATAL EXCEPTION: main
04-26 21:37:24.906: E/AndroidRuntime(637): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.examples.fragmentos/com.examples.fragmentos.TestFragmentosActivity}: java.lang.NullPointerException
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.os.Looper.loop(Looper.java:137)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-26 21:37:24.906: E/AndroidRuntime(637): at java.lang.reflect.Method.invokeNative(Native Method)
04-26 21:37:24.906: E/AndroidRuntime(637): at java.lang.reflect.Method.invoke(Method.java:511)
04-26 21:37:24.906: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-26 21:37:24.906: E/AndroidRuntime(637): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-26 21:37:24.906: E/AndroidRuntime(637): at dalvik.system.NativeStart.main(Native Method)
04-26 21:37:24.906: E/AndroidRuntime(637): Caused by: java.lang.NullPointerException
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.Activity.findViewById(Activity.java:1794)
04-26 21:37:24.906: E/AndroidRuntime(637): at com.examples.fragmentos.TestFragmentosActivity.<init>(TestFragmentosActivity.java:12)
04-26 21:37:24.906: E/AndroidRuntime(637): at java.lang.Class.newInstanceImpl(Native Method)
04-26 21:37:24.906: E/AndroidRuntime(637): at java.lang.Class.newInstance(Class.java:1319)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
04-26 21:37:24.906: E/AndroidRuntime(637): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871)
04-26 21:37:24.906: E/AndroidRuntime(637): ... 11 more
04-26 21:37:25.415: I/dalvikvm(637): threadid=3: reacting to signal 3
04-26 21:37:25.485: I/dalvikvm(637): Wrote stack traces to '/data/anr/traces.txt'
04-26 21:37:26.325: I/dalvikvm(637): threadid=3: reacting to signal 3
04-26 21:37:26.375: I/dalvikvm(637): Wrote stack traces to '/data/anr/traces.txt'
04-26 21:37:26.535: I/dalvikvm(637): threadid=3: reacting to signal 3
04-26 21:37:26.655: I/dalvikvm(637): Wrote stack traces to '/data/anr/traces.txt'
04-26 21:42:26.155: I/Process(637): Sending signal. PID: 637 SIG: 9
这是代码:
package com.examples.fragmentos;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class TestFragmentosActivity extends Activity {
Button b = ( Button )findViewById( R.id.buttonInfo );
/** Called when the activity is first created. */
@Override
public void onCreate( Bundle savedInstanceState ) {
super.onCreate( savedInstanceState );
b.setOnClickListener( mbuttonInfoListener );
setContentView(R.layout.main);
}
// Create an anonymous implementation of OnClickListener
private OnClickListener mbuttonInfoListener = new OnClickListener() {
public void onClick( View v ) {
b.setText("Ey, don't touch me!");
}
};
}