所以我正在运行一个教程应用程序,这一切都有效,除非我尝试运行bellow类,但是我没有在代码中出错,所以我想知道是否有人可以提供帮助,如果有可能出现问题代码。
另外,在编程方面,例如你在xml或类似的东西中输入说:android:在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;
}
}
}
答案 0 :(得分: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);
时给出了例外。