Null Pointer Exception when finding button on dialog
This is in my onCreate()
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.go_pro);
Button free = (Button) dialog.findViewById(R.id.still_free);
free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE!
@Override
public void onClick(View v) {
dialog.cancel();
}
});
this is int the layout go_pro:
<LinearLayout android:layout_height="wrap_content" android:layout_gravity="center_horizontal" android:layout_width="wrap_content">
<Button android:id="@+id/still_free" android:text="Keep the ads" android:layout_width="wrap_content" android:layout_height="wrap_content" ></Button>
<Button android:id="@+id/full_version" android:text="Go PRO" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
LogCat:
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): FATAL EXCEPTION: main
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.convert/com.convert.MainWindowYuval}: java.lang.NullPointerException
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.os.Handler.dispatchMessage(Handler.java:99)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.os.Looper.loop(Looper.java:123)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at java.lang.reflect.Method.invokeNative(Native Method)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at java.lang.reflect.Method.invoke(Method.java:521)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com开发者_JAVA技巧.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at dalvik.system.NativeStart.main(Native Method)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): Caused by: java.lang.NullPointerException
**07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at com.convert.MainWindowYuval.onCreate(MainWindowYuval.java:93)**MY CLASS!!@#!@#!@#!@#!@#
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-09 16:23:40.102: ERROR/AndroidRuntime(11435): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
EDIT: this is so wierd! i have two same buttons in my xml with different id and two buttons in the on create all the same but only one of them makes a null pointer exception
Button button1 = (Button) dialog.findViewById(R.id.full_version);
button1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("market://details?id=com.paulmaidment.games.flagsoftheworld"));
startActivity(intent);
dialog.cancel();//TODO OPEN PAID APP HERE!!!
}
});
Button free = (Button)findViewById(R.id.stay_free);
free.setOnClickListener(new OnClickListener() { //EXCEPTION ONLY HERE!
@Override
public void onClick(View v) {
dialog.cancel();
}
});
You are calling findViewById on Dialog when it should be called on the Activity. Changing
Button free = (Button) dialog.findViewById(R.id.still_free);
to
Button free = (Button) findViewById(R.id.still_free);
should fix it.
Try moving your code
dialog.setContentView(R.layout.go_pro);
Button free = (Button) dialog.findViewById(R.id.still_free);
free.setOnClickListener(new OnClickListener() { //EXCEPTION HERE!
@Override
public void onClick(View v) {
dialog.cancel();
}
});
to onCreate method of your Dialog class
In your xml file the id is still_free and in your code you access stay_free... That is it
edit: hmm in your edit you have stay_free but in the original post it is still_free so I assume you just changed the id to test if that caused it?
精彩评论