-2

所以我正在运行一个教程应用程序,除了我尝试运行波纹管类外,它都可以正常工作,但是我没有在代码中遇到错误,所以我想知道如果代码可能有问题,是否有人可以提供帮助。

另外一方面不是在编程时,例如你输入说:android:在xml或其他东西中。在 java 中,为什么我没有得到包含所有选项的下拉菜单?

代码:

package com.example.learn.tam;

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 textplay extends Activity implements View.OnClickListener{

Button chkCommand;
ToggleButton passToggle;
EditText input;
TextView display;

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

    holder();

    passToggle.setOnClickListener(this);

    chkCommand.setOnClickListener(this);
}

private void holder() {

    Button chkCommand = (Button) findViewById(R.id.bResults);
    passToggle = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);

}

@Override
public void onClick(View v) {
    switch(v.getId()){
    case R.id.bResults:

        String check = input.getText().toString();

        display.setText(check);

        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.contentEquals("WTF")){
            Random crazy = new Random();
            display.setText("WTF!!!!");
            display.setTextSize(crazy.nextInt(75));
            display.setTextColor(Color.rgb(crazy.nextInt(265),crazy.nextInt(265),crazy.nextInt(265)));

            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.setGravity(Gravity.CENTER);
            display.setTextColor(Color.WHITE);
        }


        break;
        case R.id.tbPassword:

        if (passToggle.isChecked() == true){
            input.setInputType(InputType.TYPE_CLASS_TEXT  | InputType.TYPE_TEXT_VARIATION_PASSWORD);
        }else{
            input.setInputType(InputType.TYPE_CLASS_TEXT);
        }
        break;
    }
}
}
4

1 回答 1

3

改变:

private void holder() {

    Button chkCommand = (Button) findViewById(R.id.bResults);
    passToggle = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);

}

private void holder() {

    chkCommand = (Button) findViewById(R.id.bResults);
    passToggle = (ToggleButton) findViewById(R.id.tbPassword);
    input = (EditText) findViewById(R.id.etCommands);
    display = (TextView) findViewById(R.id.tvResults);

}

通过声明一个具有相同名称的新变量,您隐藏了类字段。因此,类级别chkCommand保持为空,并在您尝试使用chkCommand.setOnClickListener(this);.

于 2013-03-02T15:53:10.963 回答