2013-09-25 116 views
0

我通過意圖傳遞一個包並啓動一個活動,但它未執行。日誌中的例外是無法啓動活動

09-25 21:11:44.514: E/AndroidRuntime(426): FATAL EXCEPTION: main 
09-25 21:11:44.514: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.y.httpcommunicationhelper2/com.x.y..httpcommunicationhelper2.logout}: java.lang.NullPointerException. 

您能指出錯誤嗎? ?你可以幫忙的代碼是:

//Intent part 
if (result != null) { 
    Intent tokenIntent = new Intent(mContext, tokenActivity.class); 
    Bundle bundle = new Bundle(); 
    bundle.putString("responsedata", result.substring(result.indexOf("=") + 1, result.length())); 

    tokenIntent.putExtras(bundle); 
    startActivity(tokenIntent); 


    //logout class 

    public class logout extends Activity { 

     TextView logouttext; 
     Bundle bundle2; 
     Context mContext; 

     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_main3); 

      mContext = this; 
      logouttext = (TextView) findViewById(R.id.tvlogout); 
      bundle2 = getIntent().getExtras(); 

      Log.i("logout - ats token", bundle2.getString("responsedata")); 

      URLconnector ss = new URLconnector(); 
      ss.execute("url"); 

     } 


     private class URLconnector extends AsyncTask < String, Void, String > { 

      @Override 
      protected String doInBackground(String...urls) { 
       String response = ""; 
       for (String url: urls) { 

        DefaultHttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost(url); 
        try { 

         List <NameValuePair> postParameters = new ArrayList <NameValuePair>(); 

         postParameters.add(new BasicNameValuePair("appID", "hjbhjbh")); 
         postParameters.add(new BasicNameValuePair("atsToken", bundle2.getString("responsedata"))); 

         Log.i("logout - ats token", bundle2.getString("responsedata")); 


         UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
          postParameters); 

         httppost.setEntity(formEntity); 


         HttpResponse logoutresp = httpclient.execute(httppost); 
         Log.i("Logout response ", logoutresp.toString()); 

錯誤日誌:

09-25 21:11:44.514: E/AndroidRuntime(426): FATAL EXCEPTION: main 
09-25 21:11:44.514: E/AndroidRuntime(426): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.x.y.httpcommunicationhelper2/com.x.y..httpcommunicationhelper2.logout}: java.lang.NullPointerException 

09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.os.Looper.loop(Looper.java:123) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-25 21:11:44.514: E/AndroidRuntime(426): at java.lang.reflect.Method.invokeNative(Native Method) 
09-25 21:11:44.514: E/AndroidRuntime(426): at java.lang.reflect.Method.invoke(Method.java:507) 
09-25 21:11:44.514: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
09-25 21:11:44.514: E/AndroidRuntime(426): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
09-25 21:11:44.514: E/AndroidRuntime(426): at dalvik.system.NativeStart.main(Native Method) 
09-25 21:11:44.514: E/AndroidRuntime(426): Caused by: java.lang.NullPointerException 
09-25 21:11:44.514: E/AndroidRuntime(426): at com.x.y.httpcommunicationhelper2.logout.onCreate(logout.java:52) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
09-25 21:11:44.514: E/AndroidRuntime(426): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
+2

什麼是第52行? – Ahmad

+0

你能告訴你如何調用你的註銷活動嗎?是否有可能你的'bundle2'是空的,如果我是正確的看看這裏http://stackoverflow.com/questions/5944503/android-getintent-getextras-returns-null這可能是相關的 – user902383

+0

@Ahmad Log.i (「logout - ats token」,bundle2.getString(「responsedata」)); – androidnoob

回答

0

更改線路:

bundle2 = getIntent().getExtras(); 

Log.i("logout - ats token", bundle2.getString("responsedata")); 

bundle2 = getIntent().getExtras(); 
if (bundle2 != null) { 
     Log.i("logout - ats token", bundle2.getString("responsedata")); 
} 

你也需要更改的代碼。

+0

現在錯誤消失了。 URLconnector應該做什麼改變?現在實際上沒有登錄事件。非常感謝。 – androidnoob

+0

類似的事情,檢查'bundle2'是否爲空,因爲它可能爲空。 – Szymon

+0

現在實際上沒有註銷登錄,也沒有網絡響應 – androidnoob