android ViewPager With Fragment Volley请求对空对象引用错误

时间:2018-01-15 09:02:10

标签: android android-fragments android-volley

嗨我有一个viewPager,上面有一些列表,列表数据来自服务器,当我更改页面几次这个错误时,这是​​我的代码到目前为止 这里错误

public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_last_world_news,container,false);
    prepareData();
    mListView = (ListView) view.findViewById(R.id.list_world_news);
    pb = (ProgressBar) view.findViewById(R.id.progresslastnews);
    refresh = (SwipeRefreshLayout) view.findViewById(R.id.refresh_news);
    btn = (Button) view.findViewById(R.id.btn_retry_last_news);
    tv = (AppCompatTextView) view.findViewById(R.id.tv_last_news);
    ConnectivityInit(HIDE);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            if (CheckInternet.isOnline(getContext())){
                ((MainActivity)getActivity()).getUrl("lastWorldNews");
                ConnectivityInit(HIDE);
                pb.setVisibility(View.VISIBLE);
                prepareData();
            }
        }
    });
    refresh.setColorSchemeColors(
            R.color.material_green_200,
            R.color.material_green_400,
            R.color.material_green_600,
            R.color.material_green_800
    );
    refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            prepareData();
            refresh.setRefreshing(false);
        }
    });

private void prepareData() {
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            LastWolrdNewsParser parser = new LastWolrdNewsParser();
            ArrayList<LastWorldNews> news = parser.ParseJson(response);
            LastWorldNewsAdapter adapter = new LastWorldNewsAdapter(getActivity().getApplicationContext(),
                    R.layout.last_world_news_list_item,news);
            pb.setVisibility(View.GONE);
            if (mListView.getVisibility() == View.INVISIBLE || mListView.getVisibility()==View.GONE){
                mListView.setVisibility(View.VISIBLE);
            }
            mListView.setAdapter(adapter);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            pb.setVisibility(View.GONE);

        }
    });

    RequestQueue quew = Volley.newRequestQueue(getActivity().getApplicationContext());
    quew.add(request);
}

@Override
public void onPause() {
    super.onPause();
    if (request != null){
        request.cancel();
    }
}

@Override
public void onDestroy() {
    super.onDestroy();
    if (request != null){
        request.cancel();

    }
}

我的viewPager setOffScreenLimit为4,我的Pager Pages为7 这是LastWorldNews Fragment

create table table1 
(
[date] datetime
)

insert into table1 values ('1999-02-02 00:00:00.000')

select * from table1
--1999-02-02 00:00:00.000

update table1 set [date] = '2000-01-01 11:11:11.100'
select * from table1
--2000-01-01 11:11:11.100

} 我想将pageoffScreenLimit更改为6,但我想知道有没有更好的方法?和onDestroy和onPause方法是一种标准方式?

2 个答案:

答案 0 :(得分:0)

尝试更改

 ClientSecrets cs = new ClientSecrets
        {
            ClientId = "Put_Your_Client_Id_Here",
            ClientSecret = "Put_Your_Client_Secret_Here"
        };
        string ApplicationName = "Google Calendar API .NET Quickstart";
         //Path where Json File is stored
        FileDataStore store = new FileDataStore(System.Web.HttpContext.Current.Server.MapPath("~/Uploads/Template/credentials/calendar-dotnet-quickstart.json/calendar-dotnet-quickstart.json"), true);
        UserCredential credential = GoogleWebAuthorizationBroker.AuthorizeAsync(cs, Scopes, "user", CancellationToken.None, store).Result;
        var service = new CalendarService(new BaseClientService.Initializer()
        {
            HttpClientInitializer = credential,
            ApplicationName = ApplicationName,
        });

getActivity().getApplicationContext()

删除getActivity()

答案 1 :(得分:0)

定义变量Context mContext;

将此方法添加到片段

@Override
    public void onAttach(Context context) {
        super.onAttach(context);
        mContext = context;
    }

此方法将初始化附加到活动的contaxt片段。 使用mContext代替getApplicationContext()方法