当我使用的片段,Tabspager在Android的Java文件,我得到错误(I am gettin

2019-10-20 02:10发布

我创建了3个java文件和使用TabsPager文件刷卡片段。 现在我添加上加入storehomefragment StoreHomeFragment.java代码工作代码是填充到GridView控件的集合

但现在我在TabsPager得到错误

TabsPagerAdapterStore.java

public class TabsPagerAdapterStore extends FragmentPagerAdapter {

public TabsPagerAdapterStore(FragmentManager fm) {
    super(fm);
}

@Override
public Fragment getItem(int index) {

    switch (index) {
    case 0: return new StoreGenreFragment();
    case 1: return new StoreHomeFragment(); //this line error 
    case 2: return new StoreStepFragment();
    }

    return null;
}

@Override
public int getCount() {
    // get item count - equal to number of tabs
    return 3;
}}

而在“案例1”上面的错误被弹出。 我用右键单击并快速修复

Change method return type to StoreHomeFragment 

我已上传的图像,以及只显示错误弹出,如果我选择一个,然后每隔片段在开关的情况下会得到错误可以请人帮助我,我可能犯了什么错误

并且其存在于StoreHomeFragment.java代码

public class StoreHomeFragment extends Fragment {

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

    View rootView = inflater.inflate(R.layout.store_home, container, false);

    if (android.os.Build.VERSION.SDK_INT > 9) {
        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
    }


    // Gridview
    final GridView gridView1 = (GridView)rootView.findViewById(R.id.store_home_gridview); 

    String url = "http://192.168.1.132/Android/App/good.php";

    try {
        JSONArray data = new JSONArray(getJSONUrl(url));

        final ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
        HashMap<String, String> map;

        for(int i = 0; i < data.length(); i++){
            JSONObject c = data.getJSONObject(i);
            map = new HashMap<String, String>();
            map.put("name", c.getString("name"));
            map.put("artist", c.getString("artist"));
            map.put("price", c.getString("price"));
            map.put("image", c.getString("image"));


            MyArrList.add(map);
        }           

        //gridView1.setAdapter(new ImageAdapter(this,MyArrList));
        gridView1.setAdapter(new ImageAdapter(this,MyArrList));

    } catch (JSONException e) {

        e.printStackTrace();
    } catch (Exception e) {

        e.printStackTrace();
    }
    return rootView;    
}

Answer 1:

检查StoreHomeFragment延伸Fragment ,如果你使用的支持库,请检查您的类的进口,因为Fragment类也包含在支持库。 Pratically你必须输入相同的Fragment ,你在使用类StoreGenreFragmentStoreStepFragment



文章来源: I am getting error when i use Fragment,Tabspager in Android java file