Android在对话框自定义视图中创建意图

时间:2013-02-11 06:45:10

标签: android dialog

我已经使用自定义xml视图创建了一个对话框,现在我有一些按钮,我想在点击监听器上添加并使用它们启动新活动。但每当我尝试创建一个intent时,它都会崩溃我的应用程序。

         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();

错误报告

                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)

1 个答案:

答案 0 :(得分:6)

更改

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

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

因为要从Dialog布局访问UI元素,您需要使用dialog