我正在開發一個從互聯網獲取數據的android應用程序。當互聯網連接仍然在,然後該應用程序正常工作。但是,當互聯網連接連接保持關閉,然後崩潰。我在谷歌搜索,但我不能解決我的問題。我還添加了在Android的Menifest這些權限文件當互聯網連接關閉時,Android應用程序崩潰
<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)
檢查'onPOstExecute' – Raghunandan
請註明線'80' @'NoticeBoard'做Raghunandan說的 –
什麼是'告示欄線80。 java'? – Raghunandan