为什么我的代码部队关闭?

时间:2014-07-14 16:37:02

标签: java android

我的代码从第一行到 setOnClickListener 正常运行,但是当我设置On Click Listener时,我的活动处于强制关闭状态。我试图纠正这个。

我认为我的错误出现在onclick侦听器标签上。

此代码有什么问题?

package com.Learning.math;

import java.util.Random;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class Plus extends Activity {
private TextView num11;
private TextView num22;

public Integer no1;
public Integer no2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plus_page);
    // Set Numbers To TextView's
    num11 = (TextView) findViewById(R.id.num1);
    num22 = (TextView) findViewById(R.id.num2);
    Intent iin = getIntent();
    Bundle b = iin.getExtras();
    if (b != null) {
        final String numb1 = (String) b.get("from");
        final String numb2 = (String) b.get("to");
        Integer min = Integer.valueOf(numb1);
        Integer max = Integer.valueOf(numb2);
        Random r1 = new Random();
        int random1 = r1.nextInt(max + -min) + min;
        Random r2 = new Random();
        int random2 = r2.nextInt(max - min) + min;
        String str1 = String.valueOf(random1);
        String str2 = String.valueOf(random2);
        num11.setText(str1 + "");
        num22.setText(str2 + "");
        no1 = Integer.valueOf(str1);
        no2 = Integer.valueOf(str2);

    }
 // Problem From this line

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {
        final EditText answerbox = (EditText) findViewById(R.id.answer_box);
        String Answr = answerbox.getText().toString();

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub

            Integer answer = Integer.valueOf(Answr);
            if (answer == (no2 + no1)) {
                num11.setText("OK OK OK ");
            } else {
                num11.setText("No No No");
            }
        }
    });

}

}

2 个答案:

答案 0 :(得分:1)

试试这个:

//declare Button btn
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.plus_page);
    //Your code here

    //Added code
    btn = (Button) findViewById(R.id.button1);

    btn.setOnClickListener(btnOnClick)
}


View.OnClickListener btnOnClick = new OnClickListener() 
{
    //Your code here

    @Override
    public void onClick(View arg0) {
        // TODO Auto-generated method stub

        //Your code here
    }
});

编辑:忽略此信息并发布错误日志。我没有正确阅读你的代码。我以为你正在初始化Button并在onClickListener之外调用onCreate

答案 1 :(得分:1)

setOnClickListener方法中EditText answerbox为空,因此请在EditText answerbox方法中定义onCreate。如

    final EditText answerbox = (EditText) findViewById(R.id.answer_box);

    Button btn = (Button) findViewById(R.id.button1);
    btn.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            String Answr = answerbox.getText().toString();

            Integer answer = Integer.valueOf(Answr);
            if (answer == (no2 + no1)) {
                num11.setText("OK OK OK ");
            } else {
                num11.setText("No No No");
            }
        }
    });