2015-09-14 99 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(); 
    } 
}