英文:
How can I use findviewbyid in static class?
问题
我有一个静态类:
public static void culculateFprice(){
TextView FinalBuy = (TextView) findViewById(R.id.buyText);
int Pprice = MainActivity.pepperoni.getFinalPrice();
int Cprice = MainActivity.calzone.getFinalPrice();
int QCprice = MainActivity.quattrostagioni.getFinalPrice();
int QFprice = MainActivity.quattroformaggi.getFinalPrice();
int Mprice = MainActivity.mexican.getFinalPrice();
int FinalPrice = Pprice + Cprice + QCprice + QFprice + Mprice;
FinalBuy.setText("Стоимось вашего заказа: " + FinalPrice + " руб.");
}
我如何在这个类中使用 findViewById
方法?
我从这个方法中调用这个方法:
public static void onPlus(int i){
ArrayList<String> list = listok();
switch (list.get(i)){
// ...
}
adapteR.refreshData(listokadd());
culculateFprice();
}
并且出现了问题 "非静态方法 "findViewById(int)" 无法从静态上下文中引用"。
英文:
I have a static class:
public static void culculateFprice(){
TextView FinalBuy = (TextView) findViewById(R.id.buyText);
int Pprice = MainActivity.pepperoni.getFinalPrice();
int Cprice = MainActivity.calzone.getFinalPrice();
int QCprice = MainActivity.quattrostagioni.getFinalPrice();
int QFprice = MainActivity.quattroformaggi.getFinalPrice();
int Mprice = MainActivity.mexican.getFinalPrice();
int FinalPrice = Pprice + Cprice + QCprice + QFprice + Mprice;
FinalBuy.setText("Стоимось вашего заказа: " + FinalPrice + " руб.");
}
How can I use find findViewById in this class?
I call this method from this method
public static void onPlus(int i){
ArrayList<String> list = listok();
switch (list.get(i)){
...
}
adapteR.refreshData(listokadd());
culculateFprice();
}
And have problem "Non-static method "findViewById(int)" cannot be referenced from a static context"
答案1
得分: 0
我假设这个函数存在于你的 Activity
类中,你想要根据 View
处理一些信息。你有两种方法可以解决这个问题:
- 去除静态修饰符,让实例来处理这个问题。
- 创建一个持有
View
引用的字段,该引用在布局设置完成后立即被创建。
所以你的 Activity
将会持有这个字段:
class MyActivity extends Activity {
static View FinalBuy; // 需要是静态的,否则会出现相同的错误
}
@Override
void onCreate(Bundle savedInstance) {
// 在 setLayout 之后
FinalBuy = (TextView) findViewById(R.id.buyText);
}
但为了避免潜在的运行时错误,你可以在静态方法中使用 if...else
代码块:
public static void culculateFprice(){
if(FinalBuy != null) {
// 在这里编写你的代码。
}
}
这个问题是关于基本的 Java,而不是 Android。这是 Java 语言设计的一部分,用于为程序提供实例方法和静态方法。
这个 static View
的声明可能会导致潜在的设计缺陷,因为这个 View
会比 Activity 存在更长的时间。与此不同的是,你应该考虑使用我在选项1中提出的方法,并查看为什么在你的程序中无法使用实例方法。你应该在这个讨论串上阅读更多信息。
这很可能需要你将 MainActivity
的其他字段也声明为非静态的!
英文:
I assume this function exists in your Activity
class and you would like to process some information based on the View
. There are two ways you can solve this:
- Remove the static modifier and let the instance take care of this.
- Create a field that holds a
View
reference that gets created as soon as your layout is set.
So your Activity
will hold the field:
class MyActivity extends Activity {
static View FinalBuy; // needs to be static, otherwise would give same error
}
@Override
void onCreate(Bundle savedInstance) {
// after setLayout
FinalBuy = (TextView) findViewById(R.id.buyText);
}
But to save your code from a potential runtime error, use an if...else
block in your static method:
public static void culculateFprice(){
if(FinalBuy != null) {
// your code here.
}
}
This problem is basic Java and not Android. That is how Java language is designed to provide instance vs. static methods for the programs.
This static View
declaration leads to a potential design flaw because this View
will outlive the Activity. Instead of this, you should consider using the approach I suggested in the list as option 1 and see why you are unable to use an instance method in your program. You should read more on this thread.
This will likely need you to change your other fields of MainActivity
to be declared as non-static as well!
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论