0

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

}
4

2 回答 2

3

问题是初始化b

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

只有在设置好activity的内容后,即调用才能找到views 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);
}

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

于 2012-04-26T19:52:38.920 回答
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 );
于 2012-04-26T19:51:27.130 回答