簡體   English   中英

由於button.setOnClickListener(this),Android應用程序關閉

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

我正在閱讀一個教程,該教程教會我有關Android App開發的信息。 我一直在進行所有操作,但是由於某種原因,當我運行此活動時,該應用程序關閉並說它已停止工作。 據我所知,我已正確復制了該人的代碼,但復制了他的作品。 我發現問題出在onCreate內部,上面寫着tryCmd.setOnClickListener(this);

如果我注釋掉那一行,活動就可以正常工作(但是,當我單擊按鈕時,顯然什么也沒做)。 該應用程序可以與切換按鈕的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;
        }
    }

}

您將tryCmd初始化為局部變量。 並在全局變量tryCmd設置tryCmd

從此行移除Button

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

並更改它

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

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

這條線有問題。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM