Java 如何从oncreate方法内部调用同一类中的方法?
大家好,我是android&java新手,我尝试在每次单击按钮时创建一个三按钮菜单,另外两个按钮更改其颜色或单击一个按钮时高亮显示以显示其已选中,但我无法在单击时从内部调用onCreate方法来执行特定任务。救命,谢谢Java 如何从oncreate方法内部调用同一类中的方法?,java,android,oncreate,Java,Android,Oncreate,大家好,我是android&java新手,我尝试在每次单击按钮时创建一个三按钮菜单,另外两个按钮更改其颜色或单击一个按钮时高亮显示以显示其已选中,但我无法在单击时从内部调用onCreate方法来执行特定任务。救命,谢谢 public class MainActivity extends AppCompatActivity { int PriceList; @Override public void onCreate(Bundle savedInstanceState)
public class MainActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView)findViewById(R.id.Counters);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
PriceList = 1;
return PriceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int IdNum = param1.getId()
TotalValue(IdNum);
}
});
}
}
}
您不能在方法中定义方法:
public class Example {
void foo() {
void bar() { }
}
不行!追求:
public class Example {
void foo() {
}
void bar() { }
相反
除此之外,需要了解的另一个基本问题是,为了让多个方法在同一个变量上工作,这些方法必须是类的字段,如:
public class Example {
private Button button1
void onCreate() {
...
button1 = (Button) findViewById(R.id.button1);
...
}
void foo() {
do something with button1
}
旧答案
您的问题与按钮的初始化有关:
您正在调用MyMethod(mybutton1、mybutton2、mybutton3)代码>
但您从未初始化按钮
,因此它们将始终为null
文本视图
也是如此
在Android中,您必须调用init,如下所示:
public void onCreate(...){
Button button1 = (Button)findViewById(R.id.Coffee);
Button button2 = (Button)findViewById(R.id.Mocha);
Button button3 = (Button)findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
mClick(button1,button2,button3);
mClick(button2,button1,button3);
mClick(button3,button2,button1);
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
,它们是android初学者的最佳选择imho
祝你好运!我在这里需要任何帮助
改进:
这就是您的代码应该是什么样子。请记住设置代码格式,以便更好地理解:
public class MainActivity extends AppCompatActivity {
private int priceList;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button)findViewById(R.id.button1);
Button button2 = (Button)findViewById(R.id.button2);
Button button3 = (Button)findViewById(R.id.button3);
TextView counter = (TextView)findViewById(R.id.Counter);
mClick(button1,button2,button3)
mClick(button2,button1,button3)
mClick(button3,button2,button1)
counter.setText(Item()); //Dont' use capital letters for variable's name
}
**/*------------METHODS----------------*/**
public int TotalValue(int param5){
if(param5 == 2131427416){ //just Trying to compare with id value
Item();
Log.d("Item Value " , "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else{
// some other code here
}
}
public int Item(){
priceList = 1;
return priceList;
}
public void mClick(final Button param1,final Button param2,final Button param3){
param1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view){
param2.setBackgroundColor(Color.rgb(192,110,99));
param3.setBackgroundColor(Color.rgb(192,110,99));
int idNum = param1.getId()
TotalValue(idNum);
}
});
}
}
有几点:
- 在末端增加一个闭合撑杆
- (约定)不要使用大写选择器调用变量
我没有改变你的方法的名字和变量的名字,但是考虑使用更具解释性的东西(仍然是惯例,但是为了更好的理解)
- 你没有
方法调用后的代码>
最新情况:
您丢失了在mClick(..)
调用后进行code>。根据我对您所需内容的理解,您可以使用此选项
public class LoginActivity extends AppCompatActivity {
int PriceList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button button1 = (Button) findViewById(R.id.Coffee);
final Button button2 = (Button) findViewById(R.id.Mocha);
final Button button3 = (Button) findViewById(R.id.Lattee);
TextView Counter = (TextView) findViewById(R.id.Counters);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
button2.setBackgroundColor(Color.rgb(192, 110, 99));
button3.setBackgroundColor(Color.rgb(192, 110, 99));
int IdNum = button1.getId();
TotalValue(IdNum);
}
});
Counter.setText(Item()); //Counter is a TextVew, This code doesn't work
}
public int TotalValue(int param5) {
if (param5 == 2131427416) { //just Trying to compare with id value
Item();
Log.d("Item Value ", "onCreate: The value is " + Item()); // works upto here i.e Item()=1
} else {
// some other code here
}
return 0;
}
public int Item() {
PriceList = 1;
return PriceList;
}
}
如果没有,请说明具体需要,以便我可以提供帮助。您的代码无法编译。2.您在哪里定义按钮1、按钮2和按钮3。即使代码可以编译,你也会得到一个NullPointerException,因为你的按钮没有在代码中定义。我还是不明白。请解释一下?@Misterrai我更新了我的答案,另外,请仔细阅读另一个答案。谢谢,我得到了答案。事实上,我认为我在mclick方法中定义了onclick方法,这导致了问题。很抱歉,我之前没有添加完整的代码,再次编辑,我已经定义了所有按钮和文本视图对象,但仍然不起作用,请查看again@Misterrai检查编辑的代码并尝试运行它。如果它给你任何错误,请张贴感谢你的帮助,但我认为这可能不工作,因为这是有效的,如果我点击按钮1,但不会为其他按钮(按钮2和按钮3)。使用参数背后的主要思想是随时更改button1的值。谢谢