使用我正在制作的随机数发生器应用程序的Android Studio出现问题,似乎无法解析setOnClickListener。下面是应用程序的代码我似乎无法确定问题,我们将非常感谢任何帮助。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
TextView textOne = (TextView) findViewById(R.id.textView);
TextView textTwo = (TextView) findViewById(R.id.textView2);
TextView textThree = (TextView) findViewById(R.id.textView3);
TextView textFour = (TextView) findViewById(R.id.textView4);
TextView textFive = (TextView) findViewById(R.id.textView5);
TextView textSix = (TextView) findViewById(R.id.textView6);
TextView textSeven = (TextView) findViewById(R.id.textView7);
TextView textEight = (TextView) findViewById(R.id.textView8);
Button button = (Button) findViewById(R.id.button);
String[] myStamina = {"Stamina", "300%"};
String[] mySize = {"Off", "Mega", "Mini"};
String[] myHead = {"Off", "Flower", "Bunny"};
String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
String[] myStatus = {"Off", "Curry", "Reflect"};
String[] myGravity = {"Off", "Light", "Heavy"};
String[] mySpeed = {"Off", "Fast", "Slow"};
String[] myCamera = {"Off", "Fixed", "Angled"};
int random1 = (int) ((Math.random() * 1));
int random2 = (int) ((Math.random() * 2));
int random3 = (int) ((Math.random() * 6));
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
textOne.setText(myStamina[random1]);
textTwo.setText(mySize[random2]);
textThree.setText(myHead[random2]);
textFour.setText(myBody[random3]);
textFive.setText(myStatus[random2]);
textSix.setText(myGravity[random2]);
textSeven.setText(mySpeed[random2]);
textEight.setText(myCamera[random2]);
}
});
}
答案 0 :(得分:2)
将代码移动到onCreate()
方法,如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView textOne = (TextView) findViewById(R.id.textView);
TextView textTwo = (TextView) findViewById(R.id.textView2);
TextView textThree = (TextView) findViewById(R.id.textView3);
TextView textFour = (TextView) findViewById(R.id.textView4);
TextView textFive = (TextView) findViewById(R.id.textView5);
TextView textSix = (TextView) findViewById(R.id.textView6);
TextView textSeven = (TextView) findViewById(R.id.textView7);
TextView textEight = (TextView) findViewById(R.id.textView8);
Button button = (Button) findViewById(R.id.button);
String[] myStamina = {"Stamina", "300%"};
String[] mySize = {"Off", "Mega", "Mini"};
String[] myHead = {"Off", "Flower", "Bunny"};
String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
String[] myStatus = {"Off", "Curry", "Reflect"};
String[] myGravity = {"Off", "Light", "Heavy"};
String[] mySpeed = {"Off", "Fast", "Slow"};
String[] myCamera = {"Off", "Fixed", "Angled"};
int random1 = (int) ((Math.random() * 1));
int random2 = (int) ((Math.random() * 2));
int random3 = (int) ((Math.random() * 6));
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
textOne.setText(myStamina[random1]);
textTwo.setText(mySize[random2]);
textThree.setText(myHead[random2]);
textFour.setText(myBody[random3]);
textFive.setText(myStatus[random2]);
textSix.setText(myGravity[random2]);
textSeven.setText(mySpeed[random2]);
textEight.setText(myCamera[random2]);
}
});
}
答案 1 :(得分:1)
TextView
和Button
小部件声明为global
。onClick
方法设置按钮OnCreate()
。试试这个:
public class MainActivity extends AppCompatActivity {
TextView textOne, textTwo, textThree, textFour, textFive, textSix, textSeven, textEight;
Button button;
String[] myStamina = {"Stamina", "300%"};
String[] mySize = {"Off", "Mega", "Mini"};
String[] myHead = {"Off", "Flower", "Bunny"};
String[] myBody = {"Off", "Metal", "Clear", "Tail", "Rocket belt", "Screw", "Back shield"};
String[] myStatus = {"Off", "Curry", "Reflect"};
String[] myGravity = {"Off", "Light", "Heavy"};
String[] mySpeed = {"Off", "Fast", "Slow"};
String[] myCamera = {"Off", "Fixed", "Angled"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textOne = (TextView) findViewById(R.id.textView);
textTwo = (TextView) findViewById(R.id.textView2);
textThree = (TextView) findViewById(R.id.textView3);
textFour = (TextView) findViewById(R.id.textView4);
textFive = (TextView) findViewById(R.id.textView5);
textSix = (TextView) findViewById(R.id.textView6);
textSeven = (TextView) findViewById(R.id.textView7);
textEight = (TextView) findViewById(R.id.textView8);
button = (Button) findViewById(R.id.button);
int random1 = (int) ((Math.random() * 1));
int random2 = (int) ((Math.random() * 2));
int random3 = (int) ((Math.random() * 6));
button.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
textOne.setText(myStamina[random1]);
textTwo.setText(mySize[random2]);
textThree.setText(myHead[random2]);
textFour.setText(myBody[random3]);
textFive.setText(myStatus[random2]);
textSix.setText(myGravity[random2]);
textSeven.setText(mySpeed[random2]);
textEight.setText(myCamera[random2]);
}
});
}
答案 2 :(得分:0)
在OnCreate()方法中移动代码。
答案 3 :(得分:0)
解决方案已呈现给您,现在让我们深入了解一些理论: 虽然你已经将你的问题标记为与android有关,但我建议你稍微考虑一下object oriented programming principles。如果你想在你的类中拥有可执行代码(即在对象成员等上调用其他方法的代码),你应该把它放在一个方法中。这个想法是一个对象必须首先created(instantiated)才能使用它的所有字段而不用担心它的字段是否被实例化
请注意,方法外的所有代码都将执行when the class is loaded (static code blocks)或when an object of that class type is created(构造函数)。
回到原来的问题:当你尝试设置点击监听器时,android虚拟机甚至无法告诉谁是“按钮”,因为在执行的那一点,系统创建的MainActivity实例不是完全创建(实例化)。