asyntask片段上的适配器错误

时间:2018-05-14 04:02:13

标签: android android-recyclerview android-asynctask fragment

recyclerView.setAdapter(adapter);行的logcat中显示错误 以下方法

protected void onPostExecute(String s) {
 mDialog.dismiss();
 rssObject = new Gson().fromJson(s,RSSObject.class);
 FeedAdapter adapter = new 
 FeedAdapter(rssObject,getActivity());
 recyclerView.setAdapter(adapter);
 adapter.notifyDataSetChanged();
}

错误Logcat

NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setAdapter(android.support.v7.widget.RecyclerView$Adapter)' on a null object reference
        at tona.dousting.org.ftlog4.Fragments.RSSFragment$1.onPostExecute(RSSFragment.java:98)
        at tona.dousting.org.ftlog4.Fragments.RSSFragment$1.onPostExecute(RSSFragment.java:75)

查看此课程中的所有代码

package tona.dousting.org.ftlog4.Fragments;


import android.app.ProgressDialog;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

import tona.dousting.org.ftlog4.MainActivity;
import tona.dousting.org.ftlog4.R;
import tona.dousting.org.ftlog4.RSSPackages.Adapter.FeedAdapter;
import tona.dousting.org.ftlog4.RSSPackages.Common.HTTPDataHandler;
import tona.dousting.org.ftlog4.RSSPackages.Model.RSSObject;
import com.google.gson.Gson;

/**
 * A simple {@link Fragment} subclass.
 */
public class RSSFragment extends Fragment {

    Toolbar toolbar;
    RecyclerView recyclerView;
    RSSObject rssObject;

    public RSSFragment() {
        // Required empty public constructor
    }

    //RSS link
    private final String RSS_link="http://rss.nytimes.com/services/xml/rss/nyt/Science.xml";
    private final String RSS_to_Json_API = "https://api.rss2json.com/v1/api.json?rss_url=";


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_rss, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);

        //for crate home button
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        //toolbar.setTitle("News");
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);



        // 1. get a reference to recyclerView
        //recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);
        RecyclerView recyclerView = (RecyclerView) view.findViewById(R.id.recyclerView);

        // 2. set layoutManger
        recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));

       // recyclerView.setLayoutManager(linearLayoutManager);

        loadRSS();

        return view;
    }

    private void loadRSS() {
        AsyncTask<String,String,String> loadRSSAsync = new AsyncTask<String, String, String>() {

            ProgressDialog mDialog = new ProgressDialog(getActivity());

            @Override
            protected void onPreExecute() {
                mDialog.setMessage("Please wait...");
                mDialog.show();
            }

            @Override
            protected String doInBackground(String... params) {
                String result;
                HTTPDataHandler http = new HTTPDataHandler();
                result = http.GetHTTPData(params[0]);
                return  result;
            }

            @Override
            protected void onPostExecute(String s) {
                mDialog.dismiss();
                rssObject = new Gson().fromJson(s,RSSObject.class);
                FeedAdapter adapter = new FeedAdapter(rssObject,getActivity());
                recyclerView.setAdapter(adapter);
                adapter.notifyDataSetChanged();
            }
        };

        StringBuilder url_get_data = new StringBuilder(RSS_to_Json_API);
        url_get_data.append(RSS_link);
        loadRSSAsync.execute(url_get_data.toString());


    }


    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.menu_refresh)
            loadRSS();
        return true;
    }



}

0 个答案:

没有答案