I have a problem and I am very confiused. I have a Dialog that it contains a Button.
I show the dialog in main Activity with this code, so simple:
InfoDialog id=new InfoDialog(this);
id.show();
and I want when I click on Button, it starts the second activity. you see it is so easy, I use this code:
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;
}
}
and I declare the destAct in Manifest too. when I run it on Gingerbread Android an older sdk versions it works fine. but when I run this code on ice cream sandwich (sdk 11) or higher it gives me force close!!!
this is my log cat:
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)
what the hell is this? please help me guys
finally i fix it. i used a handler and write startActivity codes on handler message receive. and in button’s onClick method just i sent a message to my handler. and it works fine on sdk>11. but i still dont now why my old code didnt work 😉