我有一个问题,我很困惑。我有一个对话框,它包含一个按钮。我用这段代码在主 Activity 中显示对话框,非常简单:
InfoDialog id=new InfoDialog(this);
id.show();
我想当我点击按钮时,它会启动第二个活动。你看这很容易,我使用这个代码:
public class InfoDialog extends Dialog implements android.view.View.OnClickListener{
Context context;
public InfoDialog(Context context) {
super(context);
setContentView(R.layout.infodialog);
this.context=context;
setupViews();
}
private void setupViews() {
Button button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Intent newintent= new Intent(context,DestActivity.class);
context.startActivity(newintent);
break;
}
}
我也在 Manifest 中声明了 destAct。当我在 Gingerbread Android 上运行旧 sdk 版本时,它运行良好。但是当我在冰淇淋三明治(sdk 11)或更高版本上运行此代码时,它会给我力量关闭!
这是我的日志猫:
08-27 09:38:54.540: W/dalvikvm(10945): threadid=1: thread exiting with uncaught exception (group=0x40a3d1f8)
08-27 09:38:54.544: E/AndroidRuntime(10945): FATAL EXCEPTION: main
08-27 09:38:54.544: E/AndroidRuntime(10945): java.lang.NullPointerException
08-27 09:38:54.544: E/AndroidRuntime(10945): at my.app.InfoDialog.onClick(InfoDialog.java:131)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.view.View.performClick(View.java:3511)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.view.View$PerformClick.run(View.java:14105)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Handler.handleCallback(Handler.java:605)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Handler.dispatchMessage(Handler.java:92)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.os.Looper.loop(Looper.java:137)
08-27 09:38:54.544: E/AndroidRuntime(10945): at android.app.ActivityThread.main(ActivityThread.java:4575)
08-27 09:38:54.544: E/AndroidRuntime(10945): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 09:38:54.544: E/AndroidRuntime(10945): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 09:38:54.544: E/AndroidRuntime(10945): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-27 09:38:54.544: E/AndroidRuntime(10945): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-27 09:38:54.544: E/AndroidRuntime(10945): at dalvik.system.NativeStart.main(Native Method)
这到底是什么?请帮帮我