每当有调用从我的 MySQL 数据库中获取数据时,我的应用程序中都有几个堆栈跟踪,如下面的那些。如果连接中断并且超时,我会得到以下堆栈跟踪:
例子:
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.---.---.profile.MyReviewAdapter.<init>(MyReviewAdapter.java:20)
at com.---.---.profile.UserFragmentActivity$CommentFragment$MyCommentTask.onPostExecute(UserFragmentActivity.java:559)
at com.---.---.profile.UserFragmentActivity$CommentFragment$MyCommentTask.onPostExecute(UserFragmentActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
例子:
java.lang.NullPointerException
at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
at com.---.---.master.TopItemAdapter.<init>(TopItemAdapter.java:18)
at com.---.---.master.MainFragmentActivity$TopFrag$TopTask.onPostExecute(MainFragmentActivity.java:961)
at com.---.---.master.MainFragmentActivity$TopFrag$TopTask.onPostExecute(MainFragmentActivity.java:1)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
这是第二个 LogCat 调用的示例代码:
class TopTask extends AsyncTask<String, String, Void> {
private MyProgressDialog progressDialog = new MyProgressDialog(
getActivity());
InputStream is = null;
String result = "";
protected void onPreExecute() {
progressDialog.setMessage("Loading...");
progressDialog.show();
progressDialog.setOnCancelListener(new OnCancelListener() {
public void onCancel(DialogInterface dialog) {
TopTask.this.cancel(true);
}
});
}
@Override
protected Void doInBackground(String... params) {
String url_select = "http://www.---.com/---/master_cat_items.php";
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url_select);
ArrayList<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("Sort", Sort));
try {
httpPost.setEntity(new UrlEncodedFormEntity(param));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
// read content
is = httpEntity.getContent();
} catch (Exception e) {
Log.e("log_tag", "Error in http connection " + e.toString());
}
try {
BufferedReader br = new BufferedReader(
new InputStreamReader(is));
StringBuilder sb = new StringBuilder();
String line = "";
while ((line = br.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
} catch (Exception e) {
// TODO: handle exception
Log.e("log_tag", "Error converting result " + e.toString());
}
return null;
}
protected void onPostExecute(Void v) {
String item, cat;
try {
JSONArray jArray = new JSONArray(result);
JSONObject json_data = null;
for (int i = 0; i < jArray.length(); i++) {
json_data = jArray.getJSONObject(i);
item = json_data.getString("item");
cat = json_data.getString("category");
items.add(item);
cats.add(cat);
}
} catch (JSONException e1) {
} catch (ParseException e1) {
e1.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
// toast to notify user to reload
}
TopItemAndTrendingObject[] mco = new TopItemAndTrendingObject[items
.size()];
int index = 0;
for (@SuppressWarnings("unused")
String i : items) {
mco[index] = new TopItemAndTrendingObject(items.get(index),
cats.get(index));
index++;
}
adapter = new TopItemAdapter(getActivity(), mco); // this is what is referenced in LogCat
setListAdapter(adapter);
progressDialog.dismiss();
}
}
所以我的问题是:
- 这个异常是什么意思(是的,我知道 NullPointerException 是什么)?
- 更重要的是,我该如何防止这种情况发生? 即使它就像连接断开时举杯一样基本(任何东西而不是强制关闭?
编辑:TopItemAdapter
按要求提供代码
public class TopItemAdapter extends ArrayAdapter<TopItemAndTrendingObject> {
private final Context context;
public TopItemAdapter(Context context, TopItemAndTrendingObject[] items) {
super(context, 0, items);
this.context = context;
}
static class ViewHolder {
public TextView tv1;
public TextView tv2;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
rowView = inflater
.inflate(R.layout.master_cat_all_time, null, true);
holder = new ViewHolder();
holder.tv1 = (TextView) rowView.findViewById(R.id.myMastCat_Cat);
holder.tv2 = (TextView) rowView.findViewById(R.id.myMasterCat_Item);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
TopItemAndTrendingObject mco = getItem(position);
String item = mco.item;
String cat = mco.cat;
holder.tv2.setText(String.valueOf(position + 1) + ". " + item);
holder.tv1.setText(cat);
return rowView;
}
}