从ViewPager内容移动到新的片段

时间:2016-01-13 10:02:56

标签: android android-fragments gridview android-viewpager

我有一个活动两个片段,** Fragment1 Fragment2 。 Fragment1包含 ViewPager ,每页都有一个GridView。我想在点击 GridView 项目时将 Fragment1 替换为 Fragment2 ,但会出现以下错误。

                java.lang.IllegalArgumentException: No view found for id 0x7f0d0078 (atrtrendy.nxg.com.atrtrendy:id/container_main) for fragment Fragment_ViewProduct{425b2ec0 #2 id=0x7f0d0078}
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
        at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5052)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
        at dalvik.system.NativeStart.main(Native Method)

我正在做的事情如下:

private class ProductGridAdapter extends BaseAdapter
{
    Context context;
    ArrayList<Product> productArrayList;
    String url = "http://ecom.nxglabs.in/uploads/productImages/";
    public ProductGridAdapter(FragmentActivity activity, ArrayList<Product> productArrayList) {
        this.context = activity;
        this.productArrayList = productArrayList;
    }

    @Override
    public int getCount() {
        return productArrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return productArrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {

        LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        String strView = preferences.getString("View","Grid");
        if(strView.equals("Layout"))
        {
            convertView = inflater.inflate(R.layout.product_view_layout_item, null);
        }else {
            convertView = inflater.inflate(R.layout.product_view_item, null);
        }

        ImageView productImg = (ImageView) convertView.findViewById(R.id.productImg);
        TextView productTitle = (TextView) convertView.findViewById(R.id.productTitle);
        TextView productBrand = (TextView) convertView.findViewById(R.id.productBrand);

        Product product = productArrayList.get(position);

        productTitle.setText(product.getProductName());
        productBrand.setText(product.getBrandName());
        Glide.with(getActivity()).load(url + product.getProductImage()).into(productImg);

        productImg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Fragment_ViewProduct viewProduct = new Fragment_ViewProduct();
                Bundle bundle = new Bundle();
                bundle.putParcelableArrayList("ProductList", productArrayList);
                bundle.putInt("Position", position);
                viewProduct.setArguments(bundle);
                getFragmentManager().beginTransaction().replace(R.id.container_main, viewProduct).commit();
            }
        });
        return convertView;
    }
}

1 个答案:

答案 0 :(得分:0)

替换时应使用用于添加fragment,相同fragment manager的管理员。您要在fragment中添加Activity并在Adapter中替换。这可能是个问题。

希望这有帮助。