片段中的RequestQueue rQueue = Volley.newRequestQueue(getActivity())

时间:2018-11-06 18:22:22

标签: android android-fragments android-volley

我有这个问题,我知道有类似的问题也有答案。但是没有人帮助我,我几乎要失去它了。我正在尝试使用gson,wordpress api,片段和凌空构建一个应用程序。每个片段都有请求。但是在这一点上 每个片段中的RequestQueue rQueue = Volley.newRequestQueue(getActivity())都有问题

我的Tab1片段类

 public class Tab1 extends Fragment {


        String url = "http://absradiotv.com/wp-json/wp/v2/posts?per_page=10&fields=id,title,featured_image_url";


        List<Object> list;
        Gson gson;
        ProgressDialog progressDialog;
        ListView postList;
        Map<String,Object> mapPost;

        Map<String,Object> mapTitle;

        int postID;
        String postTitle[];
        String postType[];
        String postFeaturedMedia[];




        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


            progressDialog = new ProgressDialog(getActivity(), R.style.MyProgressTheme);
            View rootView = inflater.inflate(R.layout.fragment_tab1, container, false);
            postList = (ListView)rootView.findViewById(R.id.postList);

            progressDialog.setMessage(" ");
            progressDialog.setCancelable(true);
            progressDialog.setCanceledOnTouchOutside(true);

            progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
            progressDialog.show();

            progressDialog.dismiss();

            StringRequest request = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                @Override
                public void onResponse(String s) {
                    gson = new Gson();
                    list = (List) gson.fromJson(s, List.class);
                    postTitle = new String[list.size()];
                    postType = new String[list.size()];
                    postFeaturedMedia = new String[list.size()];

                      ArrayList<String>listt = new ArrayList<>();

                    for(int i=0;i<list.size();++i){
                        mapPost = (Map<String,Object>)list.get(i);


                        mapTitle = (Map<String, Object>) mapPost.get("title");
                        postTitle[i] = (String) mapTitle.get("rendered");
                        postType[i] = (String) mapPost.get("type");

                        postFeaturedMedia[i] = (String) mapPost.get("featured_image_url");

                        listt.add(postTitle[i]);
                        listt.add(postType[i]);

                        listt.add(postFeaturedMedia[i]);

                    }

    //Here I tried to populate listview with id postList

                     postList.setAdapter(new ArrayAdapter(getActivity(),android.R.layout.simple_list_item_1, listt));


                    progressDialog.dismiss();
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError volleyError) {






                        Toast.makeText(getActivity(), "Sorry! An error occured!", Toast.LENGTH_LONG).show();
                        progressDialog.dismiss();

                }
            });

//I added a check to test if getActivity returns null, but it doesnt

                 if(getActivity() != null) {
                     RequestQueue rQueue = Volley.newRequestQueue(this.getActivity().getApplicationContext());
                     rQueue.add(request);
                 }
            else
                 {
                     Toast.makeText(getActivity(), "A big null issue", Toast.LENGTH_LONG).show();
                 }


            return rootView;
        }       

    }

这是我的PagerAdapter

public class PagerAdapter extends FragmentStatePagerAdapter {
    int mNumOfTabs;

    public PagerAdapter(FragmentManager fm, int NumOfTabs) {
        super(fm);
        this.mNumOfTabs = NumOfTabs;
    }

    @Override
    public Fragment getItem(int position) {

        switch (position) {
            case 0:
                Tab1 tab1 = new Tab1();
                return tab1;
            case 1:
                Tab2 tab2 = new Tab2();
                return tab2;
            case 2:
                Tab3 tab3 = new Tab3();
                return tab3;
            default:
                return null;
        }
    }

    @Override
    public int getCount() {
        return mNumOfTabs;
    }
}

这是我的MainActivity

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

         toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");

        TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
        tabLayout.addTab(tabLayout.newTab().setText("LATEST"));
        tabLayout.addTab(tabLayout.newTab().setText("POLITICS"));
        tabLayout.addTab(tabLayout.newTab().setText("STATE"));
        tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

        final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
        final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(), tabLayout.getTabCount());
        viewPager.setAdapter(adapter);
        viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
        tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                viewPager.setCurrentItem(tab.getPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main_drawer, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer,toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);



        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        return super.onOptionsItemSelected(item);


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }



    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }


    }

0 个答案:

没有答案