onClick导致崩溃

时间:2014-04-16 21:25:59

标签: java android eclipse

当我按下我添加的两个按钮中的任何一个时,我的应用程序崩溃,即bAdd和bSubtract。这就像我通过以下几个教程编写的第二个应用程序,所以这对我来说是全新的。 " XXX"和" yyy"在按下bSubtract和bAdd按钮时调用,这会导致应用程序在我的手机上崩溃。 xml,代码和logcat如下所示。

xml文件:

<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.myapp1.CalculateActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="@string/hello_world"
        android:textSize="25sp" />

    <Button
        android:id="@+id/bSubtract"
        android:onClick="xxx"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/bAdd"
        android:layout_alignBottom="@+id/bAdd"
        android:layout_marginLeft="28dp"
        android:layout_toRightOf="@+id/bAdd"
        android:text="@string/bSubtract" />

    <Button
        android:id="@+id/bAdd"
        android:onClick="yyy"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginLeft="52dp"
        android:layout_marginTop="25dp"
        android:text="@string/bAdd" />

</RelativeLayout>

代码:

public class CalculateActivity extends ActionBarActivity {

    int counter;
    TextView display;
    Button add, sub;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_calculate);

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }

        counter = 0;
        display = (TextView) findViewById(R.id.textView1);

    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }


    /**
    * A placeholder fragment containing a simple view.
    */

    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() { }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                  Bundle savedInstanceState) {
              View rootView = inflater.inflate(R.layout.fragment_calculate,
                      container, false);
              return rootView;
        }
    }


    public void xxx(View view) {
        // Do something in response to button
        counter--;
        display.setText(""+ counter);

    }


    public void yyy(View view) {
        // Do something in response to button
        counter++;
        display.setText(""+ counter);

    }
}

logcat的:

04-16 23:07:04.409: D/AndroidRuntime(22562): Shutting down VM
04-16 23:07:04.409: W/dalvikvm(22562): threadid=1: thread exiting with uncaught exception (group=0x418db700)
04-16 23:07:04.414: E/AndroidRuntime(22562): FATAL EXCEPTION: main
04-16 23:07:04.414: E/AndroidRuntime(22562): java.lang.IllegalStateException: Could not execute method of the activity
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.view.View$1.onClick(View.java:3838)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.view.View.performClick(View.java:4475)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.view.View$PerformClick.run(View.java:18786)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.os.Handler.handleCallback(Handler.java:730)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.os.Looper.loop(Looper.java:137)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.app.ActivityThread.main(ActivityThread.java:5419)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at java.lang.reflect.Method.invoke(Method.java:525)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at dalvik.system.NativeStart.main(Native Method)
04-16 23:07:04.414: E/AndroidRuntime(22562): Caused by: java.lang.reflect.InvocationTargetException
04-16 23:07:04.414: E/AndroidRuntime(22562):    at java.lang.reflect.Method.invokeNative(Native Method)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at java.lang.reflect.Method.invoke(Method.java:525)
04-16 23:07:04.414: E/AndroidRuntime(22562):    at android.view.View$1.onClick(View.java:3833)
04-16 23:07:04.414: E/AndroidRuntime(22562):    ... 11 more
04-16 23:07:04.414: E/AndroidRuntime(22562): Caused by: java.lang.NullPointerException
04-16 23:07:04.414: E/AndroidRuntime(22562):    at com.example.myapp1.CalculateActivity.yyy(CalculateActivity.java:83)
04-16 23:07:04.414: E/AndroidRuntime(22562):    ... 14 more
04-16 23:12:14.494: I/Process(22562): Sending signal. PID: 22562 SIG: 9

2 个答案:

答案 0 :(得分:-1)

按钮add和sub为null。请尝试以下代码(添加onCreate()):

add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub); 

答案 1 :(得分:-2)

您已经在xml创建了按钮,但尚未添加引用,这应该可以帮到您。在开始使用之前添加它。

add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub); 

您也可以在xml中添加来自侦听器的引用

 android:onClick="xxx"
 android:onClick="yyy"

您可以通过编程方式添加它,就像这样。

add.setOnClickListener(xxx);

 View.OnClickListener xxx = new View.OnClickListener() {
        public void onClick(View v) {
            // Do something in response to button
            counter--;
            display.setText(""+ counter);
        }
    };

我相信这完全取决于你。 另外,检查你的日志,它说你的按钮为空;它几乎说出了什么问题。

04-16 23:07:04.414: E/AndroidRuntime(22562): Caused by: java.lang.NullPointerException
04-16 23:07:04.414: E/AndroidRuntime(22562):    at com.example.myapp1.CalculateActivity.yyy(CalculateActivity.java:83)
04-16 23:07:04.414: E/AndroidRuntime(22562):    ... 14 more

无论如何,只需记住在使用then之前添加对视图的引用。这应该避免将来的空指针异常。