2017-01-03 55 views
0

我想關閉應用程序,如果沒有Internet連接可用。我知道有一個問題,我已經存在,但我做的完全一樣,似乎我的應用程序在註冊前工作正常,但在編輯暱稱字段中的文本後,它會拋出空指針錯誤,如果我在編輯文本後再次打開此處時執行此操作一起來看看:關閉應用程序如果沒有Internet連接

我已經註冊了我的應用程序,並在抽屜裏我的綽號是可編輯的,所以我做這樣的:

pname = (EditText) findViewById(R.id.pname); 

     profimg.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       show(); 
      } 
     }); 

     pname.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       pname.setInputType(0x0000006); 
       pname.setCursorVisible(true); 

      } 
     }); 
     pname.setOnFocusChangeListener(new View.OnFocusChangeListener() { 
      @Override 
      public void onFocusChange(View v, boolean hasFocus) { 
       if(hasFocus){ 
        pname.setCursorVisible(false); 
       } 
       else{ 
        pname.setCursorVisible(false); 
       } 
      } 
     }); 

     pname.setOnEditorActionListener(new EditText.OnEditorActionListener() { 
      @Override 
      public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { 
       if (event!=null && event.getKeyCode() != KeyEvent.KEYCODE_ENTER || actionId != EditorInfo.IME_ACTION_DONE) { 
        return false; 
       } 
       else if(actionId==EditorInfo.IME_ACTION_DONE || event==null || event.getKeyCode() == KeyEvent.KEYCODE_ENTER){ 

        pname.setCursorVisible(false); 
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(pname.getWindowToken(), 0); 
        return false; 
       } 
       return false; 
      } 
     }); 

pname.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 
       sp.edit().remove("personName").apply(); 
       name= s.toString(); 
       sp.edit().putString("personName",name).apply(); 
       try { 
        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("nickname", name)); 
        params.add(new BasicNameValuePair("mobile", callNo)); 
        JSONObject json = jsonParser.makeHttpRequest(Config.URL_Info, "POST", 
          params); 
        int success = json.getInt(TAG_SUCCESS); 
        if (success == 1) { 
         Log.d("Status Updated", json.getString(TAG_MESSAGE)); 
        } 
       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 
     if(path1) { 
      String value = sp.getString("personName", name); 
      pname.setText(value); 
     } 

的錯誤是我在哪裏設置文本。我正在檢查setcontentview(佈局)後的起始連接可用性,並且該函數剛好在onCreate方法之後。

錯誤日誌:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int org.json.JSONObject.getInt(java.lang.String)' on a null object reference                    
                      at .afterTextChanged(PlaceCallActivity.java:206) 
                      at android.widget.TextView.sendAfterTextChanged(TextView.java:7942) 
                      at android.widget.TextView.setText(TextView.java:4079) 
                      at android.widget.TextView.setText(TextView.java:3928) 
                      at 
+0

這個文本設置是通過使用共享偏好 – SamH67

+1

這樣做的哪裏是你的錯誤日誌? – rafsanahmad007

+0

@ rafsanahmad007看看我知道發生了什麼,但無法弄清楚如何解決它:( – SamH67

回答

1

你需要檢查是否json變量是null或不

只有json變量不爲空繼續進一步的步驟

@Override 
     public void afterTextChanged(Editable s) { 
      sp.edit().remove("personName").apply(); 
      name= s.toString(); 
      sp.edit().putString("personName",name).apply(); 
      try { 
       List<NameValuePair> params = new ArrayList<NameValuePair>(); 
       params.add(new BasicNameValuePair("nickname", name)); 
       params.add(new BasicNameValuePair("mobile", callNo)); 
       JSONObject json = jsonParser.makeHttpRequest(Config.URL_Info, "POST", 
         params); 
       if(json!=null){ 
       int success = json.getInt(TAG_SUCCESS); 
       if (success == 1) { 
        Log.d("Status Updated", json.getString(TAG_MESSAGE)); 
       } 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 
    }); 
+0

完美我不知道,但Vygintas B首先給出了正確的答案你的我這就是爲什麼我upvoting謝謝BTW :) – SamH67

+0

快樂,如果它幫助你:) – Jayanth

+0

對不起你的回答是1分鐘提前,他的答案是一分鐘前。我會接受你的我的壞:/ – SamH67

2

這是因爲jsonParser.makeHttpRequest(Config.URL_Info, "POST",params);是返回null!

因此,

int success = json.getInt(TAG_SUCCESS); 

是給java.lang.NullPointerException:

+0

更多代碼添加看看是的,但如何解決它:( – SamH67

+0

如果它給你null這意味着沒有互聯網連接,因此你可以關閉應用程序,但不json.getInt(TAG_SUCCESS)爲json爲空! – Tulsi

1

看起來你的JSON對象爲空

檢查,用前不爲空它

if(json != null){ 
    int success = json.getInt(TAG_SUCCESS); 
} 
else{ 
    Log.d("JSON", "NULL"); 
} 
+0

感謝它工作這些小我會在3分鐘內接受你的答案:) – SamH67

+0

感謝您的幫助:) – SamH67

+0

高興地幫助:)檢查此問題以獲得有關此問題的更多見解[什麼是NullPointerException,以及如何解決此問題?] (http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) –

1

你應該檢查網絡是否可用撥打電話到httprequest

嘗試在此之前,

public static boolean isNetworkAvailable(Activity activity) { 
    int[] networkTypes = {ConnectivityManager.TYPE_MOBILE, 
      ConnectivityManager.TYPE_WIFI}; 
    try { 
     ConnectivityManager connectivityManager = 
       (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); 
     for (int networkType : networkTypes) { 
      NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo(); 
      if (activeNetworkInfo != null && 
        activeNetworkInfo.getType() == networkType) 
       return true; 
     } 
    } catch (Exception e) { 
     return false; 
    } 
    return false; 
} 

檢查Internet之前:

if(isNetworkAvailable(Mainactivity.this) 
    JSONObject json = jParser.makeHttpRequest(Config.URL_Info, "POST", 
         params); 

       try { 
        // Checking for SUCCESS TAG 
        if(json!=null){ 
        Log.d("RESPONSE ", json.toString()); 
        int success = json.getInt(TAG_SUCCESS); 

        if (success == 1) { 

        } else { 

        } 
       }else{ 


       } catch (JSONException e) { 
        e.printStackTrace(); 
       } 
+0

是的,這也是正確的,但json檢查會做的工作。謝謝大家發佈正確的答案:)o – SamH67

+0

如果沒有網絡連接...沒有任何要求致電httprequest ..雖然 – rafsanahmad007

+0

是的我知道,但沒有互聯網連接,所以不會打電話,雖然你的答案看起來更多適當的我會使用你的,但我已經接受了一個答案。非常感謝更好的方法:) – SamH67

相關問題