由于button.setOnClickListener(this)Android应用关闭

时间:2014-09-13 06:25:25

标签: java android onclicklistener

我正在阅读一本教我学习Android App开发的教程。我一直都很好,但出于某种原因,当我运行此活动时,应用程序关闭并说它已停止工作。据我所知,我已正确复制了人的代码,但他的作品。 我发现问题出在onCreate里面的行tryCmd.setOnClickListener(this);

如果我注释掉那一行,活动就可以了(但是当我点击按钮时,显然没有做任何事情)。该应用程序适用于togglebutton的onClickListener语句。谁能告诉我有什么问题?感谢。

以下是我的activity.java中的所有代码:

package com.example.testapp;

import java.util.Random;

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ToggleButton;

public class Text extends Activity implements View.OnClickListener{

    EditText input;
    Button tryCmd;
    ToggleButton passTog;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text);
        //Call the method to assign variables to the elements.
        assignVariables(); 
        //Initialize the button and ToggleButton to work with the onClick method.
        passTog.setOnClickListener(this);
        tryCmd.setOnClickListener(this);        

    }

    private void assignVariables() {
        // TODO Auto-generated method stub
        input = (EditText) findViewById(R.id.etCommands);
        Button tryCmd = (Button) findViewById(R.id.bResults);
        passTog = (ToggleButton) findViewById(R.id.tbPassword);
        display = (TextView) findViewById(R.id.tvResults);
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch(v.getId()){
        case R.id.bResults:
            String check = input.getText().toString();
            if (check.contentEquals("left")){
                display.setGravity(Gravity.LEFT);
            }else if(check.contentEquals("center")){
                display.setGravity(Gravity.CENTER);
            }else if(check.contentEquals("right")){
                display.setGravity(Gravity.RIGHT);
            }else if(check.contentEquals("blue")){
                display.setTextColor(Color.BLUE);
            }else if(check.contains("WTF")){
                Random crazy = new Random();
                display.setText("WTF!?!?");
                display.setTextSize(crazy.nextInt(75));
                display.setTextColor(Color.rgb(crazy.nextInt(255), crazy.nextInt(255), crazy.nextInt(255)));
                switch(crazy.nextInt(3)){
                case 0:
                    display.setGravity(Gravity.LEFT);
                    break;
                case 1:
                    display.setGravity(Gravity.CENTER);                     
                    break;
                case 2:
                    display.setGravity(Gravity.RIGHT);                      
                    break;
                }
            }else{
                display.setText("invalid");
                display.setTextColor(Color.BLACK);
                display.setGravity(Gravity.CENTER);
            }

            //Clear the input box if it doesn't contain WTF.
            if(!check.contains("WTF")){
                input.setText("");
            }
            break;

        case R.id.tbPassword:
            if (passTog.isChecked()){
                input.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            } else{
                input.setInputType(InputType.TYPE_CLASS_TEXT);
            }
            break;
        }
    }

}

3 个答案:

答案 0 :(得分:1)

您将tryCmd初始化为本地变量。 并在全局变量tryCmd

上设置onClickListener

从此行中删除按钮

Button tryCmd = (Button) findViewById(R.id.bResults);

并改变它

tryCmd = (Button) findViewById(R.id.bResults);

答案 1 :(得分:1)

您将tryCmd定义为局部变量,因此它仅在assignVariables()方法中检测。您应该将其更改为全局变量。(正如您定义但不使用)

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    Button tryCmd = (Button) findViewById(R.id.bResults);//error in this line you define it as local variable delete Button before it
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}  

编辑:

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    tryCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}  

答案 2 :(得分:0)

这条线有问题..

Button tryCmd = (Button) findViewById(R.id.bResults);

您正在为新的局部变量创建局部变量和赋值...所以全局变量仍然是 NULL ..

所以从 assignVariables()函数中删除该行..

尝试此代码......

private void assignVariables() {
    // TODO Auto-generated method stub
    input = (EditText) findViewById(R.id.etCommands);
    tryCmd = (Button) findViewById(R.id.bResults);
    passTog = (ToggleButton) findViewById(R.id.tbPassword);
    display = (TextView) findViewById(R.id.tvResults);
}