I am getting error when i use Fragment,Tabspager in Android java file
Asked Answered
P

1

1

I have created 3 fragment java files and used TabsPager file to swipe the fragments. Now I added working code on StoreHomeFragment.java code added to storehomefragment is collection of populating into gridview

but now I am get error in 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;
}}

And in "case 1" above error is popped up. I use right click and Quick Fix

Change method return type to StoreHomeFragment 

I have uploaded the image as well to just show the error popping up if I choose that one then every other fragments in the switch case will get error can please anyone help me what mistake I might have committed

enter image description here

And the code which is present in 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;    
}
Perineum answered 7/5, 2014 at 8:52 Comment(13)
Please paste the code of StoreHomeFragment classOwn
at case one try casting it to a Fragment,if it is extending a fragment class like, case 1: return (Fragment) new StoreHomefragment();Saviour
@Saviour ya i will try it and let you knowPerineum
@Saviour same error yaar i am not able to understand what is happening if i type cast it also it is not workingPerineum
@Kedarnath pls help me yaarPerineum
Please paste the code of StoreHomeFragment classOwn
@Kedarnath i will edit here only haPerineum
@Kedarnath pls check the StoreHomeFragment.java codePerineum
@Kedarnath Can u pls help me in correcting the errorPerineum
@SandeepV please paste the code of StoreGenreFragment class as well.Own
@Kedarnath no i have not added anything there yaar only i added to StoreHomeFragment class but it happened like thisPerineum
I know, but I want to see its code.Own
let us continue this discussion in chatOwn
T
0

Check if StoreHomeFragment extends Fragment and, if you use the support library, check your class imports because Fragment class is also contained in support library. Pratically you have to import the same Fragment class that you use in StoreGenreFragment or StoreStepFragment

Tafoya answered 7/5, 2014 at 9:11 Comment(2)
check the above code i have already the extends fragment @ethePerineum
Ya i have added in my project but i havent pasted it because the people who answer question must understand correctly where i am getting issie so @ethePerineum

© 2022 - 2024 — McMap. All rights reserved.