简体   繁体   English

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

[英]Android app closes due because of button.setOnClickListener(this)

I'm going through a tutorial that's teaching me about Android App development. 我正在阅读一个教程,该教程教会我有关Android App开发的信息。 I've been going through it all just fine but for some reason when I run this activity the app closes and says that it has stopped working. 我一直在进行所有操作,但是由于某种原因,当我运行此活动时,该应用程序关闭并说它已停止工作。 As far as I can tell I have copied the person's code correctly, but his works. 据我所知,我已正确复制了该人的代码,但复制了他的作品。 I found that the problem is with the line inside onCreate that says tryCmd.setOnClickListener(this); 我发现问题出在onCreate内部,上面写着tryCmd.setOnClickListener(this);

If I comment out that single line, the activity works fine(but just doesn't do anything when I click the button, obviously). 如果我注释掉那一行,活动就可以正常工作(但是,当我单击按钮时,显然什么也没做)。 The app works fine with the togglebutton's onClickListener statement. 该应用程序可以与切换按钮的onClickListener语句配合使用。 Can anyone tell me what is wrong? 谁能告诉我这是怎么回事? Thanks. 谢谢。

Here is all the code from my activity.java: 这是我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;
        }
    }

}

you are initialize tryCmd as local variable. 您将tryCmd初始化为局部变量。 and set onClickListner onglobal variable tryCmd 并在全局变量tryCmd设置tryCmd

remove Button from this line 从此行移除Button

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

and change it 并更改它

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

you define tryCmd as local variable so it detect only in assignVariables() method.you should change it to global variable.(as you define but not use) 您将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);
}  

edited: 编辑:

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);
}  

There is Problem with this line.. 这条线有问题。

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

You Are creating local Variable and Assigning value to new local Variable... So the global variable is still NULL .. 您正在创建局部变量并将值分配给新的局部变量...因此,全局变量仍为NULL ..

So Remove that line from assignVariables() function.. 因此,从assignVariables()函数中删除该行。

Try This code... 试试这个代码...

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);
}  

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM