简体   繁体   中英

Android Creating Intent Inside Dialog Custom View

I have created a dialog using a custom xml view now i have some buttons that i want to add on click listeners and launch new activity using them. But everytime i try to create an intent it crashes my application.

         Dialog dialog = new Dialog(MainActivity.this);
     dialog.setContentView(R.layout.activity_dialog_tips);
     RelativeLayout AldikoLauncher;
     AldikoLauncher = (RelativeLayout) findViewById(R.id.launch_aldiko_tour);

        AldikoLauncher.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                pref.setGuidedActivity("MainActivity");
                Intent ShowCase1 = new Intent(MainActivity.this,ShowCaseStep1.class);
                ShowCase1.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                overridePendingTransition(R.anim.fadeout, R.anim.fadein);
                startActivity(ShowCase1);
            }
        }); 
     dialog.setTitle("Quick Tips");
     dialog.setCancelable(true);
     dialog.show();

ERROR REPORT

                02-11 06:44:04.355: E/AndroidRuntime(2202): FATAL EXCEPTION: main
            02-11 06:44:04.355: E/AndroidRuntime(2202): java.lang.NullPointerException
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.utext.unext.MainActivity.InvokeTipsLauncher(MainActivity.java:91)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.utext.unext.MainActivity.onOptionsItemSelected(MainActivity.java:80)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.app.Activity.onMenuItemSelected(Activity.java:2548)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:980)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.view.menu.ActionMenuView.invokeItem(ActionMenuView.java:547)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:115)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.view.View.performClick(View.java:4202)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.view.View$PerformClick.run(View.java:17340)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.os.Handler.handleCallback(Handler.java:725)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.os.Handler.dispatchMessage(Handler.java:92)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.os.Looper.loop(Looper.java:137)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at android.app.ActivityThread.main(ActivityThread.java:5039)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at java.lang.reflect.Method.invokeNative(Native Method)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at java.lang.reflect.Method.invoke(Method.java:511)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
            02-11 06:44:04.355: E/AndroidRuntime(2202):     at dalvik.system.NativeStart.main(Native Method)

Change

AldikoLauncher = (RelativeLayout) findViewById(R.id.launch_aldiko_tour);

to

 AldikoLauncher = (RelativeLayout)dialog.findViewById(R.id.launch_aldiko_tour);

because for accessing UI elements from Dialog layout you will need to use dialog

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM