当我使用片段,Tabspager在Android中的Java文件,我得到错误 [英] I am getting error when i use Fragment,Tabspager in Android java file
问题描述
我已经创建了3段的java文件和使用TabsPager文件刷卡片段。现在我说的StoreHomeFragment.java code工作$ C $下加入storehomefragment是填充到GridView控件集合
但现在我得到TabsPager误差
TabsPagerAdapterStore.java
公共类TabsPagerAdapterStore扩展FragmentPagerAdapter {公共TabsPagerAdapterStore(FragmentManager FM){
超(FM);
}@覆盖
公共片段的getItem(INT指数){ 开关(指数){
情况下0:返回新StoreGenreFragment();
案例1:返回新StoreHomeFragment(); //这一行错误
案例2:返回新StoreStepFragment();
} 返回null;
}@覆盖
公众诠释的getCount(){
//获取项目计数 - 等于标签数
返回3;
}}
和在上面的错误案例1弹出。我用右键单击并快速修复
更改方法返回类型StoreHomeFragment
我已经上传的图像,以及只显示错误弹出如果让我选择一个,然后在开关壳体每隔片段会得到错误可以请人帮助我,我可能犯了什么错
块引用>而code这是StoreHomeFragment.java present
公共类StoreHomeFragment扩展片段{@覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,
捆绑savedInstanceState){ 查看rootView = inflater.inflate(R.layout.store_home,集装箱,FALSE); 如果(android.os.Build.VERSION.SDK_INT> 9){
。StrictMode.ThreadPolicy政策=新StrictMode.ThreadPolicy.Builder()permitAll()建();
StrictMode.setThreadPolicy(政策);
}
// 网格视图
最终的GridView gridView1 =(GridView控件)rootView.findViewById(R.id.store_home_gridview); 字符串URL =http://192.168.1.132/Android/App/good.php; 尝试{
JSONArray数据=新JSONArray(getJSONUrl(URL)); 最终的ArrayList<&HashMap的LT;字符串,字符串>> MyArrList =新的ArrayList<&HashMap的LT;字符串,字符串>>();
HashMap的<字符串,字符串>地图; 的for(int i = 0; I< data.length();我++){
JSONObject的C = data.getJSONObject(I)
地图=新的HashMap<字符串,字符串>();
map.put(名,c.getString(名字));
map.put(艺术家,c.getString(艺术家));
map.put(价格,c.getString(价格));
map.put(形象,c.getString(图像));
MyArrList.add(地图);
} //gridView1.setAdapter(new ImageAdapter(这一点,MyArrList));
gridView1.setAdapter(新ImageAdapter(这一点,MyArrList)); }赶上(JSONException E){ e.printStackTrace();
}赶上(例外五){ e.printStackTrace();
}
返回rootView;
}
解决方案检查
StoreHomeFragment
扩展片段
和,如果使用支持库,请检查您的类的进口,因为片段
类也包含在支持库。 Pratically你要导入在StoreGenreFragment
或StoreStepFragment 使用相同的
片段
类code>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
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; }
解决方案Check if
StoreHomeFragment
extendsFragment
and, if you use the support library, check your class imports becauseFragment
class is also contained in support library. Pratically you have to import the sameFragment
class that you use inStoreGenreFragment
orStoreStepFragment
这篇关于当我使用片段,Tabspager在Android中的Java文件,我得到错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!