2014-04-04 59 views
0

我想在我的应用程序中打开警告对话框的URL的onclick但它开始activity.please的送礼者错误告诉我的解决方案警告对话框的onclick应用程序崩溃

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

错误:

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

后Communicator.java在列146 – Blackbelt

+0

使用右键在方法 – Saqib

+0

的说法,而活动是你的问题解决了吗? –

回答

0

Uri你想从""开始。

对于示例 -

Intent marketIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
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(); 

    }