Android 在执行不同的httpget URL并在同一listview中加载结果时获取NullPointerException?

Android 在执行不同的httpget URL并在同一listview中加载结果时获取NullPointerException?,android,listview,android-listview,Android,Listview,Android Listview,在一个片段中,我正在执行AsyncTask,以便在onCreateView中从web获取JSON数据,并在ListView中加载数据 在NavigationDrawer菜单中,我有一些类别。单击时,我再次触发不同的URL,并将数据加载到用于片段的同一ListView中。在此之前,工作流程良好 问题来了: 在显示数据之前,我从NavigationDrawer中选择了一个类别,当我再次选择另一个类别时,我的ListView会得到NullPointerException 以下是我的片段代码: 和我的C

在一个片段中,我正在执行AsyncTask,以便在onCreateView中从web获取JSON数据,并在ListView中加载数据

在NavigationDrawer菜单中,我有一些类别。单击时,我再次触发不同的URL,并将数据加载到用于片段的同一ListView中。在此之前,工作流程良好

问题来了: 在显示数据之前,我从NavigationDrawer中选择了一个类别,当我再次选择另一个类别时,我的ListView会得到NullPointerException

以下是我的片段代码:

和我的CustomListViewAdapter:

和R.layout.fragment_类别:

请告诉我哪里出了问题

这是我的日志:


请添加logcat。尝试使用from方法获取充气机:layoutInflater=layoutInflater.frommcontext;在你的自定义适配器中。我试过了。但是没有任何改进。您可以与调试器一起检查您传递给自定义适配器的上下文是否为null吗?上下文包含类的我的包名。
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();
    }
}
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;
}
}
<?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>
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