按钮单击时应用程序崩溃

时间:2013-09-02 09:51:54

标签: java android

当我按下按钮应用程序崩溃时有问题。 崩溃的文字 包com.leftas.vidurkioskaiciavimas;

 24367-24367/com.leftas.vidurkioskaiciavimas E/AndroidRuntime: FATAL EXCEPTION: main
            java.lang.IllegalStateException: Could not execute method of the activity
            at android.view.View$1.onClick(View.java:3704)
            at android.view.View.performClick(View.java:4232)
            at android.view.View$PerformClick.run(View.java:17298)
            at android.os.Handler.handleCallback(Handler.java:615)
            at android.os.Handler.dispatchMessage(Handler.java:92)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4921)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
            at dalvik.system.NativeStart.main(Native Method)
            Caused by: java.lang.reflect.InvocationTargetException
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at android.view.View$1.onClick(View.java:3699)
            ... 11 more
            Caused by: java.lang.NullPointerException
            at com.leftas.vidurkioskaiciavimas.vidurkioskaiciuokle.buttonEventHandler(vidurkioskaiciuokle.java:85)
        ... 14 more

我认为文本为空,因为所有时间都崩溃了 “mUserOutput.setText(”“);或”mUserInput.setText(mUserInput.getText()+“10”);“

所以我添加了

        if (mUserOutput == null)
        Log.e("output", null);
    if (mUserInput == null)
        Log.e("input", null);

但没有输出......
所以这是我的主要活动。

    import android.content.Intent;
    import android.os.Bundle;
    import android.app.Activity;
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuInflater;
    import android.view.MenuItem;
    import android.view.View;
    import android.widget.TextView;



public class vidurkioskaiciuokle extends Activity {
int c;
TextView mUserOutput,mUserInput;


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

    TextView mUserOutput = (TextView) findViewById(R.id.textView2);
    TextView mUserInput = (TextView) findViewById(R.id.textView);

    if (mUserOutput == null)
        Log.e("output", null);
    if (mUserInput == null)
        Log.e("input", null);
}
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.vidurkioskaiciuokle, menu);
    return true;
}
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.about:
            startActivity(new Intent(this, About.class));
            return true;
        case R.id.exit:
            finish();
            return true;
        default:
            return super.onOptionsItemSelected(item);
    }
}

public void buttonEventHandler(View v) {
    switch(v.getId()) {

        case R.id.button1 :
            mUserInput.setText(mUserInput.getText() + "1");
            c = +1;
            break;

        case R.id.button2 :
            mUserInput.setText(mUserInput.getText() + "2");
            c = +2;
            break;

        case R.id.button3 :
            mUserInput.setText(mUserInput.getText() + "3");
            c = +3;
            break;

        case R.id.button4 :
            mUserInput.setText(mUserInput.getText() + "4");
            c = +4;
            break;

        case R.id.button5 :
            mUserInput.setText(mUserInput.getText() + "5");
            c = +5;
            break;

        case R.id.button6 :
            mUserInput.setText(mUserInput.getText() + "6");
            c = +6;
            break;

        case R.id.button7 :
            mUserInput.setText(mUserInput.getText() + "7");
            c = +7;
            break;

        case R.id.button8 :
            mUserInput.setText(mUserInput.getText() + "8");
            c = +8;
            break;

        case R.id.button9 :
            mUserInput.setText(mUserInput.getText() + "9");
            c = +9;
            break;

        case R.id.button10 :
            mUserInput.setText(mUserInput.getText() + "10");
            c = +10;
            break;

        case R.id.buttonclear :
            mUserOutput.setText("");
            mUserInput.setText("");
            c = 0;
            double average = 0;
            break;
        }
}

public void computeAverage(int [] scores)
{
    double average = 0;
    for (int c : scores) {
        average += c;
    }

    average /= scores.length;

    mUserOutput.setText(Double.toString(average));
}

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

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="1"
    android:id="@+id/button1"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="2"
    android:id="@+id/button2"
    android:layout_alignTop="@+id/button1"
    android:layout_toRightOf="@+id/button1"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="3"
    android:id="@+id/button3"
    android:layout_alignBottom="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="4"
    android:id="@+id/button4"
    android:layout_alignTop="@+id/button3"
    android:layout_toRightOf="@+id/button3"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="5"
    android:id="@+id/button5"
    android:layout_alignTop="@+id/button4"
    android:layout_toRightOf="@+id/button4"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="6"
    android:id="@+id/button6"
    android:layout_alignTop="@+id/button5"
    android:layout_toRightOf="@+id/button5"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="7"
    android:id="@+id/button7"
    android:layout_above="@+id/button2"
    android:layout_toRightOf="@+id/button2"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="8"
    android:id="@+id/button8"
    android:layout_alignTop="@+id/button9"
    android:layout_toLeftOf="@+id/button9"
    android:onClick="buttonEventHandler" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="9"
    android:id="@+id/button9"
    android:onClick="buttonEventHandler"
    android:layout_alignTop="@+id/button7"
    android:layout_toRightOf="@+id/button1" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="10"
    android:id="@+id/button10"
    android:layout_above="@+id/button4"
    android:layout_toLeftOf="@+id/button5"
    android:onClick="buttonEventHandler" />

<TextView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/textView"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/button8"
    android:layout_above="@+id/textView2" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/textView2"
    android:layout_alignParentStart="false"
    android:layout_alignParentEnd="false"
    android:layout_alignWithParentIfMissing="false"
    android:layout_alignLeft="@+id/button8"
    android:layout_alignRight="@+id/textView"
    android:layout_above="@+id/button7" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Clear"
    android:id="@+id/buttonclear"
    android:onClick="buttonEventHandler"
    android:layout_above="@+id/button5"
    android:layout_toRightOf="@+id/button4"
    android:layout_alignRight="@+id/textView2" />

如果你能帮助我,我将感激不尽:)
如果有需要,我会编辑我的问题。 :)
谢谢 :)
如果出现问题,请说明在哪里以及如何更改;)

2 个答案:

答案 0 :(得分:6)

试试这个

  mUserOutput = (TextView) findViewById(R.id.textView2);
  mUserInput = (TextView) findViewById(R.id.textView);

您已经将textview声明为类成员。所以如上所述初始化它们。

  TextView mUserOutput = (TextView) findViewById(R.id.textView2); 
  // makes mUserOutput local 

答案 1 :(得分:0)

findviewbyid和mUserOutput.setOnClickListener(this);要求。

还实现onLCicklistener,如:    公共类MainActivity扩展Activity实现OnClickListener {