【发布时间】:2012-08-06 19:20:19
【问题描述】:
在我的应用程序中,我有一个打开和关闭蓝牙的按钮,我注意到当我将切换按钮定义移到类的顶部并移出onResume(); 时,应用程序崩溃并出现 nullpointerexception,这里是崩溃代码的简短版本:
public class MainActivity extends Activity {
static final BluetoothAdapter myBluetooth = BluetoothAdapter.getDefaultAdapter();
final ToggleButton tglbtn = (ToggleButton)findViewById(R.id.ToggleButton01);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(myBluetooth == null){
setContentView(R.layout.notsupported);
}
else{
setContentView(R.layout.supported);
}
}
@Override
public void onResume()
{
super.onResume();
tglbtn.setChecked(myBluetooth.isEnabled());
tglbtn.setOnClickListener(new OnClickListener(){
public void onClick(View v) { .......}
}
}
}
当我移动时final ToggleButton tglbtn = (ToggleButton)findViewById(R.id.ToggleButton01);
回到onResume(),应用运行良好,没有崩溃,有人能解释一下为什么会这样吗?
【问题讨论】:
标签: android android-widget adt