2015-09-14 141 views
-3

我使用OkHttp庫訪問URL ,我想返回時給出的消息顯示吐司。問題是,當我嘗試查看吐司時,顯示java.lang.NullPointerException錯誤。你可以幫我嗎?顯示java.lang.NullPointerException展出吐司

對不起,我的英語不好。

public void sendtows(String usr_e, String usr_p) throws Exception{ 
    String stResponse; 

    Request request = new Request.Builder() 
      .url("http://127.0.0.1/l.php?useremail=" + usr_e + "&userpassword=" + usr_p) 
      .build(); 
    Response response = client.newCall(request).execute(); 

    stResponse = response.body().string(); 

    if(response.isSuccessful()){ 
     if(stResponse.contains("OK")){ 
      Log.d("Login", "Ok"); 
     } else { 
      Toast toastErr = Toast.makeText(getApplicationContext(), "error", Toast.LENGTH_SHORT); 
      toastErr.show(); 
     } 
    } else { 
     Log.d("ERRO", "erro"); 
    } 
} 
+1

你能發佈stacktrace? – jaudo

+1

張貼你的日誌..... – Nithinlal

+1

把你的logcat – Rustam

回答

-1

您正在使用getApplicationContext()吐司,這是可能是給你NullPointerException異常。所以,

如果您正在使用片段使用:

Toast toastErr = Toast.makeText(getActivity(), "error", Toast.LENGTH_SHORT); 
toastErr.show(); 

如果您正在使用類,然後做出一個構造函數,可以通過語境和使用語境吐司像如下:

//Constructor to pass context 
public Your_ClassName(Context context){ 
     this.context = context; 
    } 

Toast toastErr = Toast.makeText(context, "error", Toast.LENGTH_SHORT); 
toastErr.show(); 
+0

不工作。幫我! – Yan

+0

getActivity()導致Fragment崩潰 – Manny265

0

不要工作!日誌:

09-14 16:47:49.802 4342-4374/com.android.vending d/Finsky:[222] AppStatesReplicator.handleContentSyncResponse:完成0帳戶 內容同步以0成功。 09-14 16:47:49.802
4342-4342/com.android.vending D/Finsky:[1] 5.onFinished:安裝 狀態複製成功。 09-14 16:48:08.112
2487-2532/system_process E/InputDispatcher:運動事件無效 指針計數0;值必須介於1和16之間。09-14 16:48:08.182
2487-2532/system_process E/InputDispatcher:運動事件無效 pointer count 0;值必須介於1和16之間。09-14 16:48:08.182
1289-1323 /? D/audio_hw_primary:找到/ dev/snd/pcmC0D0p 09-14 16:48:08.242 1289-1323 /? W/audio_hw_primary:out_write()限制 睡眠時間112539至46439 09-14 16:48:08.312 1289-1323 /? W/audio_hw_primary:out_write()限制睡眠時間65759至46439 09-14 16:48:09.652 4227-4227/com.lavie.conflife D /錯誤x: java.lang.NullPointerException 09-14 16:48:18.152
2657-3291/com.bluestacks.bstfolder d/dalvikvm:GC_FOR_ALLOC釋放 740K,29%免費2774K/3868K,暫停0毫秒,共0毫秒

的onClick調用sendtows

public void sendData(View view){ //btLogin click 
    Context context = getApplicationContext(); 
    int duration = Toast.LENGTH_SHORT; 

    if(checkInternetConnection()){ 
     try{ 
      usr_email = (EditText)findViewById(R.id.etEmail); 
      usr_password = (EditText)findViewById(R.id.etPassword); 

      final String email = usr_email.getText().toString(); 
      final String pass = usr_password.getText().toString(); 

      if (!isValidEmail(email)) { 
       usr_email.setError(getString(R.string.invalid_email)); 
      } else if (!isValidPassword(pass)) { 
       usr_password.setError(getString(R.string.invalid_password)); 
      } else { 
       new LoginActivity().sendtows(usr_email.getText().toString(), usr_password.getText().toString()); 
      } 
     } catch (Exception e){ 
      Log.d("Errorx", e.toString()); 
     } 
    } else { 
     CharSequence text = "Offline"; 
     Toast toast = Toast.makeText(context, text, duration); 
     toast.show(); 
    } 
}