在片段中,我正在执行AsyncTask,用于从 onCreateView()中的网络获取JSON数据,并在 ListView 中加载数据。
在 NavigationDrawer 菜单中,我有一些类别。点击后,我再次触发不同的网址并将数据加载到我用于片段的同一个ListView中。到这里工作流程很好。
问题来自:
我在显示数据之前从 NavigationDrawer 中选择了一个类别,当我选择另一个类别时,我的ListView得到 NullPointerException 。
这是片段的代码:
private ListView feedListView = null;
private ArrayList<FeedItem> listitem = null;
public class Category extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.fragment_category, container, false);
feedListView = (ListView) rootView.findViewById(R.id.list);
category_name = getArguments().getString("CATEGORY");
if(isConnection())
{
new GetData().execute("URL");
}
else
{
Intent in1 = new Intent(getActivity(),NetworkConn.class);
in1.putExtra("category", category_name);
startActivity(in1);
}
return rootView;
}
}
public class GetData extends AsyncTask<String,Void,String>
{
@Override
protected String doInBackground(String... params) {
JSONObject json = getJSONFromUrl(url);
parseJson(json);
}
@Override
protected void onPostExecute(String result) {
if (null != listitem) {
updateList();
}
}
}
public void updateList() {
// Here I am getting that exception
=> CustomListViewAdapter adapter = new CustomListViewAdapter(getActivity(), listitem);
feedListView.setVisibility(View.VISIBLE);
feedListView.setAdapter(adapter);
feedListView.setOnItemClickListener(new OnItemClickListener() {});
}
public void parseJson(JSONObject json) {
try {
// parsing json object
if (json.getString("status").equalsIgnoreCase("ok")) {
String pages = json.getString("pages");
System.out.println("No of pages: "+pages);
JSONArray posts = json.getJSONArray("posts");
listitem = new ArrayList<FeedItem>();
for (int i = 0; i < posts.length(); i++)
{
JSONObject post = (JSONObject) posts.getJSONObject(i);
JSONObject author = post.getJSONObject("author");
System.out.println(author.get("name"));
FeedItem item = new FeedItem();
String title = post.getString("title");
item.setTitle(Html.fromHtml(title).toString());
item.setDate(post.getString("date"));
item.setId(post.getString("id"));
item.setUrl(post.getString("url"));
item.setContent(post.getString("content"));
JSONArray attachments = post.getJSONArray("attachments");
if (null != attachments && attachments.length() > 0) {
JSONObject attachment = attachments.getJSONObject(0);
if (attachment != null)
item.setAttachmentUrl(attachment.getString("url"));
}
listitem.add(item);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
我的 CustomListViewAdapter :
public class CustomListViewAdapter extends BaseAdapter {
private ArrayList<FeedItem> listData;
private LayoutInflater layoutInflater;
public ImageLoader imageLoader;
DisplayImageOptions op;
private Context mcontext;
public CustomListViewAdapter(Context context, ArrayList<FeedItem> listData) {
this.listData = listData;
this.mcontext = context;
// Here I am getting the exception
=> layoutInflater = (LayoutInflater) this.mcontext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
imageLoader = ImageLoader.getInstance();
}
...
...
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = layoutInflater.inflate(R.layout.list_item,parent, false);
holder = new ViewHolder();
holder.headlineView = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
holder.reportedDateView = (TextView) convertView.findViewById(R.id.postdate);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
FeedItem newsItem = (FeedItem) listData.get(position);
holder.headlineView.setText(newsItem.getTitle());
holder.reportedDateView.setText(newsItem.getDate());
String url = newsItem.getAttachmentUrl();
if (holder.imageView != null) {
imageLoader.displayImage(url, holder.imageView,op);
}
return convertView;
}
}
R.layout.fragment_category :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ProgressBar
android:id="@+id/progressbar"
style="?android:progressBarStyleLargeInverse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center" />
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#fff"/>
</RelativeLayout>
请告诉我哪里出错了。
这是我的 LogCat :
06-18 12:03:18.392: E/AndroidRuntime(27092): FATAL EXCEPTION: main
06-18 12:03:18.392: E/AndroidRuntime(27092): java.lang.NullPointerException
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.rquad.theindianpanorama.CustomListViewAdapter.<init>(CustomListViewAdapter.java:31)
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.rquad.theindianpanorama.Category.updateList(Category.java:138)
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.rquad.theindianpanorama.Category$GetData.onPostExecute(Category.java:126)
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.rquad.theindianpanorama.Category$GetData.onPostExecute(Category.java:1)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.os.AsyncTask.finish(AsyncTask.java:631)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.os.AsyncTask.access$600(AsyncTask.java:177)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.os.Handler.dispatchMessage(Handler.java:99)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.os.Looper.loop(Looper.java:213)
06-18 12:03:18.392: E/AndroidRuntime(27092): at android.app.ActivityThread.main(ActivityThread.java:5225)
06-18 12:03:18.392: E/AndroidRuntime(27092): at java.lang.reflect.Method.invokeNative(Native Method)
06-18 12:03:18.392: E/AndroidRuntime(27092): at java.lang.reflect.Method.invoke(Method.java:525)
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
06-18 12:03:18.392: E/AndroidRuntime(27092): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
06-18 12:03:18.392: E/AndroidRuntime(27092): at dalvik.system.NativeStart.main(Native Method)
06-18 12:03:18.883: D/dalvikvm(27092): GC_FOR_ALLOC freed 4822K, 48% free 5922K/11256K, paused 38ms, total 38ms