我正在使用从main扩展到设置某些按钮的子类。我可以更改在main中创建的变量,例如子项内的TotalMoney。FindViewById在子类中使用时为空
问题是findViewById给出了一个空指针异常。 buildthem()中的代码在主类中使用时可以正常工作。
我正在使用setContentView(R.layout.main);从OnCreate中主要。 Child类实例化并从主类中的OnResume调用。
我是否需要在子节点中设置内容视图,或者以某种方式传递主类中的内容视图?
package com.chewyapps.markettrader;
class child extends main {
void buildthem(){
TotalMoney = TotalMoney + 9999;
Button MenuButton = (Button) findViewById(R.id.Menu);
//etc
}
}
我可以在OnCreate中没有findViewById因为完整的代码将使用
for(i=0; i<buttonIDs.length; i++) {
Button b = (Button) findViewById(buttonIDs[i]);
//do other stuff
}
的对是需要与每个按钮其他的事情循环。如果我能够得到基本示例,但我认为完整的代码将起作用。
它没有发生在我说这之前,但在子类是所谓的child.java一个单独的文件,从我main.java文件中的onResume我用:
child childObject = new child();
childObject.buildthem();
关于命名约定好一点。 – trgraglia 2011-03-17 15:53:07
请参阅编辑为什么我不能把它放在创造 – 2011-03-17 16:17:19
@汤姆汉弗里斯更新了我的答案 – 2011-03-17 17:36:29