gpt4 book ai didi

互联网连接关闭时 Android 应用程序崩溃

转载 作者:搜寻专家 更新时间:2023-11-01 08:52:13 24 4
gpt4 key购买 nike

我正在开发一个从互联网获取数据的安卓应用。当互联网连接保持打开时,应用程序可以正常工作。但是,当互联网连接保持关闭状态时,它就会崩溃。我在谷歌上搜索过,但找不到我的解决方案。我还在 Android Menifest 文件中添加了这些权限

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我的全部代码如下,

public class NoticeBoard extends Activity {


ArrayList<String> title_array = new ArrayList<String>();
ArrayList<String> notice_array = new ArrayList<String>();

ListView list;
base_adapter3 adapter;
TextView f;
TextView msg;



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_notice);
list = (ListView) findViewById(R.id.list_notice2);
msg=(TextView)findViewById(R.id.error_msg);

new test_ays().execute();

}




class test_ays extends AsyncTask<Void, Void, String> {

@Override
protected String doInBackground(Void... params) {
String str = null ;
try {
HttpClient httpclient = new DefaultHttpClient();
//HttpPost httppost = new HttpPost("http://10.0.2.2/BSDI/show.php");
HttpPost httppost = new HttpPost("http://logger.net46.net/android_bsdi_tuhin/show.php");
HttpResponse response = httpclient.execute(httppost);
str = EntityUtils.toString(response.getEntity());
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();



}
return str;


}



@Override
protected void onPostExecute(String result) {

super.onPostExecute(result);

String response = result.toString();
msg.setText(response);

try {

JSONArray new_array = new JSONArray(response);

for (int i = 0, count = new_array.length(); i < count; i++) {
try {
JSONObject jsonObject = new_array.getJSONObject(i);
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("notice").toString());

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

}
}

adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array);
list.setAdapter(adapter);
// f=(TextView) findViewById(R.id.textTuh);
// f.setText(title_array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

}
}

}

我的 logcat 错误如下,

03-02 06:55:28.570: E/AndroidRuntime(788): FATAL EXCEPTION: main
03-02 06:55:28.570: E/AndroidRuntime(788): Process: com.example.bsdiassistant, PID: 788
03-02 06:55:28.570: E/AndroidRuntime(788): java.lang.NullPointerException
03-02 06:55:28.570: E/AndroidRuntime(788): at com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:80)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.example.bsdiassistant.NoticeBoard$test_ays.onPostExecute(NoticeBoard.java:1)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.finish(AsyncTask.java:632)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask.access$600(AsyncTask.java:177)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Handler.dispatchMessage(Handler.java:102)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.os.Looper.loop(Looper.java:137)
03-02 06:55:28.570: E/AndroidRuntime(788): at android.app.ActivityThread.main(ActivityThread.java:4998)
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invokeNative(Native Method)
03-02 06:55:28.570: E/AndroidRuntime(788): at java.lang.reflect.Method.invoke(Method.java:515)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
03-02 06:55:28.570: E/AndroidRuntime(788): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
03-02 06:55:28.570: E/AndroidRuntime(788): at dalvik.system.NativeStart.main(Native Method)

最佳答案

试试这段代码:

@Override
protected void onPostExecute(String result) {

super.onPostExecute(result);

if(result!=null) // add this
{
String response = result.toString();
msg.setText(response);

try {

JSONArray new_array = new JSONArray(response);

for (int i = 0, count = new_array.length(); i < count; i++) {
try {
JSONObject jsonObject = new_array.getJSONObject(i);
title_array.add(jsonObject.getString("title").toString());
notice_array.add(jsonObject.getString("notice").toString());

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

}
}

adapter = new base_adapter3(NoticeBoard.this, title_array, notice_array);
list.setAdapter(adapter);
// f=(TextView) findViewById(R.id.textTuh);
// f.setText(title_array);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();

}
}
}

关于互联网连接关闭时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22127238/

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