Android:Logcat显示错误(.setOnClickListener)

时间:2014-03-27 23:23:28

标签: java android onclicklistener logcat android-logcat

当我运行此代码时,它不起作用,catlog将我发送到该行: “add.setOnClickListener(new View.OnClickListener(){”

这是我的代码:

public class MainActivity extends ActionBarActivity {

    int counter;
    Button add, sub;
    TextView display; 

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        counter =0;
        add = (Button) findViewById(R.id.Badd);
        sub = (Button) findViewById(R.id.Bsub);
        display = (TextView) findViewById(R.id.TVmain);



        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Your Total is: " + counter);
            }
        });

activity_main.xml中:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.pluseonecalculator.MainActivity"
    tools:ignore="MergeRootFrame" />

这是logcat:

  

03-27 23:14:27.150:I / Process(408):发送信号。 PID:408 SIG:9       03-27 23:27:01.070:D / AndroidRuntime(420):关闭VM       03-27 23:27:01.070:W / dalvikvm(420):threadid = 1:线程退出未捕获异常(组= 0x40015560)       03-27 23:27:01.090:E / AndroidRuntime(420):致命异常:主要       03-27 23:27:01.090:E / AndroidRuntime(420):java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.pluseonecalculator / com.example.pluseonecalculator.MainActivity}:   显示java.lang.NullPointerException       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.os.Handler.dispatchMessage(Handler.java:99)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.os.Looper.loop(Looper.java:123)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread.main(ActivityThread.java:3683)       03-27 23:27:01.090:E / AndroidRuntime(420):at java.lang.reflect.Method.invokeNative(Native Method)       03-27 23:27:01.090:E / AndroidRuntime(420):at java.lang.reflect.Method.invoke(Method.java:507)       03-27 23:27:01.090:E / AndroidRuntime(420):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)       03-27 23:27:01.090:E / AndroidRuntime(420):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)       03-27 23:27:01.090:E / AndroidRuntime(420):at dalvik.system.NativeStart.main(Native Method)       03-27 23:27:01.090:E / AndroidRuntime(420):引起:java.lang.NullPointerException       03-27 23:27:01.090:E / AndroidRuntime(420):at com.example.pluseonecalculator.MainActivity.onCreate(MainActivity.java:34)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)       03-27 23:27:01.090:E / AndroidRuntime(420):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)       03-27 23:27:01.090:E / AndroidRuntime(420):... 11 more

3 个答案:

答案 0 :(得分:0)

当您尝试在此处设置侦听器时,看起来您的Button指向null

add.setOnClickListener(new View.OnClickListener() {

确保activity_main.xml中存在ID为Badd的Button,否则findViewById将返回null

add = (Button) findViewById(R.id.Badd);

答案 1 :(得分:0)

您在activity_main中缺少按钮的xml代码。要使用其id从java代码实例化按钮,必须将其添加到activity_main.xml:

<Button
    android:id="@+id/Badd"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="My Button"/>

答案 2 :(得分:0)

activity_main.xml内没有任何按钮。如果您使用findViewById,则必须将<Button>(或任何您想要查找的内容)元素作为主要布局的子项(在本例中为FrameLayout)。

<FrameLayout
    android:stuff = "stuffystuff"
    ... />
    <Button
        android:id = "ThisIsTheIdThatWillBeFound    // by findViewById(R.id.ThisIsTheIdThatWillBeFound)
        .../>
<FrameLayout>

目前正在发生的事情是没有R.id.Badd,因为android无法找到android:id = "@+id/Badd"的任何内容来生成它。因此,NullPointerException

在您的情况下,将ID为BaddBsub的按钮添加到您的布局中,您应该没有任何问题。您的按钮也必须具有其他属性。见here

此外,如果您计划以类似的方式实现第二个按钮,我会考虑将两个按钮(而不是创建两个)用作主要类OnClickListener,并在OnClick时使用方法传递视图使用其id通过switch (view.getId())控制程序流。示例here