gpt4 book ai didi

android - 如果没有互联网连接,则关闭应用程序

转载 作者:行者123 更新时间:2023-12-03 16:18:56 24 4
gpt4 key购买 nike

如果没有可用的互联网连接,我正在尝试关闭应用程序。我知道有一个问题已经存在,但是我做的完全一样,而且看来我的应用在注册前可以正常工作,但是如果我在编辑文本后再次打开时在昵称字段中编辑文本,则会抛出空指针错误。看一看 :

我已经在我的应用程序上注册,并且在抽屉中有一个可编辑的昵称,所以我这样做是这样的:

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(layout)之后的开始处检查连接的可用性,并且该函数仅在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

最佳答案

您需要检查json变量是否为null
仅当json变量不为null时,才继续执行其他步骤

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

}
});

关于android - 如果没有互联网连接,则关闭应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41438692/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com