为什么我会java.lang.NullPointerException
我尝试了很多方法,但仍然没有工作......
private class LoadAllQoutes extends AsyncTask<String, Void, List<Quotescategory>> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(QuotesActivity2.this);
pDialog.setMessage("Loading qoutes. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected List<Quotescategory> doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url_all_qoutes, "GET", params);
Log.d("All Qoutes: ", json.toString());
try {
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
QOUTES =json.getJSONArray(TAG_QOUTES);
for (int i = 0; i < QOUTES.length(); i++) {
JSONObject c =QOUTES.getJSONObject(i);
quotescat.setId(c.getString(TAG_QID));
quotescat.setName(c.getString(TAG_NAME));
quotescat.set_By(c.getString(TAG_BY));
listcat.add(quotescat);
Log.d("All Qoutes: ", "Add on list");
Log.d("All Qoutes2: ", quotescat.getName().toString());
}
} else {
Intent i = new Intent(getApplicationContext(),MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(i);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
@SuppressWarnings("unused")
protected void onPostExecute(List<Quotescategory> result) {
ListView Items = (ListView) findViewById(R.id.quoteslist);
QuotesSimpleAdapter adapter=new QuotesSimpleAdapter(local, R.layout.list_item, result);
Items.setAdapter(adapter);
}
}
public class QuotesSimpleAdapter extends ArrayAdapter
{
Context context;
int layoutResourceId;
List<Quotescategory> data = null;
public QuotesSimpleAdapter(Context context,int layoutResourceId, List<Quotescategory> result) {
super(context,layoutResourceId, result);
this.context = context;
this.data = result;
this.layoutResourceId = layoutResourceId;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return data.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return data.get(position);
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
RssItemHolder holder = null;
if (row == null) {
convertView = super.getView(position, convertView, parent);
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new RssItemHolder();
holder.txt_id = (TextView) row.findViewById(R.id.txt_qid);
holder.txt_by=(TextView) row.findViewById(R.id.txt_by);
holder.txt_name=(TextView) row.findViewById(R.id.txt_name);
row.setTag(holder);
} else {
holder = (RssItemHolder) row.getTag();
}
Quotescategory QItem = data.get(position);
holder.txt_id.setText(QItem.getId());
holder.txt_name.setText(QItem.getName());
holder.txt_by.setText(QItem.get_By());
return row;
}
static class RssItemHolder {
TextView txt_id;
TextView txt_name;
TextView txt_by;
}
}
log cat:
11-08 11:30:48.558: E/AndroidRuntime(1147): FATAL EXCEPTION: main
11-08 11:30:48.558: E/AndroidRuntime(1147): java.lang.NullPointerException
11-08 11:30:48.558: E/AndroidRuntime(1147): at com.example.guajratipride2.QuotesSimpleAdapter.getCount(QuotesSimpleAdapter.java:31)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.widget.ListView.setAdapter(ListView.java:454)
11-08 11:30:48.558: E/AndroidRuntime(1147): at com.example.pride.QuotesActivity2$LoadAllQoutes.onPostExecute(QuotesActivity.java:193)
11-08 11:30:48.558: E/AndroidRuntime(1147): at com.example.pride.QuotesActivity2$LoadAllQoutes.onPostExecute(QuotesActivity.java:1)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.os.AsyncTask.finish(AsyncTask.java:417)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.os.AsyncTask.access$300(AsyncTask.java:127)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.os.Handler.dispatchMessage(Handler.java:99)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.os.Looper.loop(Looper.java:123)
11-08 11:30:48.558: E/AndroidRuntime(1147): at android.app.ActivityThread.main(ActivityThread.java:3683)
11-08 11:30:48.558: E/AndroidRuntime(1147): at java.lang.reflect.Method.invokeNative(Native Method)
11-08 11:30:48.558: E/AndroidRuntime(1147): at java.lang.reflect.Method.invoke(Method.java:507)
11-08 11:30:48.558: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
11-08 11:30:48.558: E/AndroidRuntime(1147): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
11-08 11:30:48.558: E/AndroidRuntime(1147): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
返回一些东西(listcat); //而不是null int doInbackground
答案 1 :(得分:1)
你要回来&#34; null&#34;来自doInBackground方法:
请将此声明更改为:
return result;
(我想你需要在这里传递listCat。)
此结果将传递给您的
onPostExecute(List<Quotescategory> result)
希望这是你的问题
答案 2 :(得分:1)
NPE是由适配器没有正确数据引起的。在getCount()
:
@Override
public int getCount() { // list should be empty if data is null
return data == null ? 0 : data.size();
}
另外,更改您的AsyncTask
doInBackground以返回列表中的数据:
protected List<Quotescategory> doInBackground(String... args) {
//.... your code
return result; // not null
}
答案 3 :(得分:-1)
更改此代码,
@Override
public View getView(int position, View convertView, ViewGroup parent) {
RssItemHolder holder = null;
if (convertView == null) {
LayoutInflater inflater = LayoutInflater.from(context);
convertView = inflater.inflate(layoutResourceId, null, false);
holder = new RssItemHolder();
holder.txt_id = (TextView) row.findViewById(R.id.txt_qid);
holder.txt_by=(TextView) row.findViewById(R.id.txt_by);
holder.txt_name=(TextView) row.findViewById(R.id.txt_name);
convertView.setTag(holder);
} else {
holder = (RssItemHolder) convertView.getTag();
}
Quotescategory QItem = data.get(position);
holder.txt_id.setText(QItem.getId());
holder.txt_name.setText(QItem.getName());
holder.txt_by.setText(QItem.get_By());
return convertView;
}
我认为这可能会对你有所帮助