警报对话框onclick应用程序崩溃

时间:2014-04-04 10:15:28

标签: android

我想在我的应用程序中打开url onclick of alert对话框但是它会给出启动活动的错误。请告诉我解决方案

public void AlertUpgrade(Activity activity)
{
    Log.e("AlertUpgrade", "Communicator.");
    AlertDialog.Builder builder = new AlertDialog.Builder(activity);
    builder.setMessage("  Click OK to Upgrade Now ? ")
    .setPositiveButton("OK" +
    "", new DialogInterface.OnClickListener() 
        {
     public void onClick(DialogInterface dialog, int id)   
     {

       Log.e("onClick", "AlertUpgrade");

       Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("  "));
       marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
        startActivity(marketIntent);
    }
     }
    ).setOnKeyListener(new OnKeyListener() {
        @Override
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
            {
                return false;
            }

            return true;
        }
    });
    builder.create().show();  
}

ERROR:

04-04 15:40:10.549: E/AndroidRuntime(5142): FATAL EXCEPTION: main
04-04 15:40:10.549: E/AndroidRuntime(5142): java.lang.NullPointerException
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.app.Activity.startActivityForResult(Activity.java:3464)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.app.Activity.startActivityForResult(Activity.java:3425)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.app.Activity.startActivity(Activity.java:3661)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.app.Activity.startActivity(Activity.java:3629)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at gsip.webgalaxy.ui.Communicator$2.onClick(Communicator.java:146)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.os.Looper.loop(Looper.java:176)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at android.app.ActivityThread.main(ActivityThread.java:5419)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at java.lang.reflect.Method.invokeNative(Native Method)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at java.lang.reflect.Method.invoke(Method.java:525)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1046)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:862)
04-04 15:40:10.549: E/AndroidRuntime(5142):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

将您想要的Uri放在""中。

例如 -

Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));

答案 1 :(得分:0)

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AlertUpgrade(MainActivity.this);
}
    public void AlertUpgrade(Activity activity)
    {

        AlertDialog.Builder builder = new AlertDialog.Builder(activity);
        builder.setMessage("  Click OK to Upgrade Now ? ")
        .setPositiveButton("OK" +
        "", new DialogInterface.OnClickListener() 
            {
         public void onClick(DialogInterface dialog, int id)   
         {

           Log.e("onClick", "AlertUpgrade");

           Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
           marketIntent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY | Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET|Intent.FLAG_ACTIVITY_MULTIPLE_TASK);
            startActivity(marketIntent);
        }
         }
        ).setOnKeyListener(new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                if(keyCode == KeyEvent.KEYCODE_BACK && event.getAction() == KeyEvent.ACTION_UP)
                {
                    return false;
                }

                return true;
            }
        });
        builder.create().show();  

    }