关闭应用程序如果没有Internet连接

时间:2017-01-03 07:46:56

标签: android nullpointerexception crash

如果没有可用的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

4 个答案:

答案 0 :(得分:2)

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

因此,

int success = json.getInt(TAG_SUCCESS);

正在提供java.lang.NullPointerException:

答案 1 :(得分:1)

看起来你的json对象是空的

使用前检查它是否为空

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

答案 2 :(得分: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();
            }

        }
    });

答案 3 :(得分: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;
}

之前检查互联网:

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