在Android上使用onClick()事件时出错

时间:2012-04-26 19:48:07

标签: android android-activity

我是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!");
        }
    };

}

2 个答案:

答案 0 :(得分:2)

问题是b的初始化:

Button b = ( Button )findViewById( R.id.buttonInfo );

您只能在设置活动内容后找到视图,即在致电setContentView()后。

试试这个:

private Button b;

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    b = (Button)findViewById(R.id.buttonInfo);
    b.setOnClickListener(mbuttonInfoListener);
}

另外,请确保您的布局中有一个ID设置为buttonInfo的按钮。

答案 1 :(得分:1)

新活动中有NullPointerException。它正确启动但崩溃了。错误显示在这里:

04-26 21:37:24.906: E/AndroidRuntime(637): Caused by: java.lang.NullPointerException
04-26 21:37:24.906: E/AndroidRuntime(637):  at ndroid.app.Activity.findViewById(Activity.java:1794)
04-26 21:37:24.906: E/AndroidRuntime(637):  at com.examples.fragmentos.TestFragmentosActivity.init>(TestFragmentosActivity.java:12)

检查TestFragmentosActivity第12行中的代码,因为那里存在问题。

编辑 -

我猜第12行是这样的:

Button b = ( Button )findViewById( R.id.buttonInfo );

您正在尝试在创建之前获取视图。使用setContentView设置视图后应调用此方法。一个简单的解决方案是声明这样的对象:

Button b;

并在onCreate函数的末尾添加:

b = ( Button )findViewById( R.id.buttonInfo );