UnsupportedOperationException:AdapterView不支持addView(View)

时间:2016-09-04 11:18:57

标签: android android-fragments

我查看了相同错误的现有帖子,但代码/实现与我的不同,但某些帖子中的建议无助于解决问题。我试图在图像点击时替换显示网格(图像列表)与另一个片段的片段。我在运行时看到错误“Java.lang.UnsupportedOperationException:addView(View)在AdapterView中不受支持”,但无法确定哪部分代码导致此错误以及需要采取哪些措施来解决此问题。感谢您的帮助以解决此问题。代码如下:

带网格的片段(图像列表):

public class OneFragment extends Fragment {

public OneFragment() {
    // Required empty public constructor
}

private ListView mListView;
GridView grid;
String[] web = {"Doctors","Dentists","Test" };
int[] imageId = {R.mipmap.doctors,R.mipmap.dentists,R.mipmap.ic_launcher};

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_one, container, false);
    int   iconSize=getResources().getDimensionPixelSize(android.R.dimen.app_icon_size);

    CustomGrid adapter = new CustomGrid(view.getContext(), iconSize);
    GridView gridview = (GridView) view.findViewById(R.id.grid);
    gridview.setAdapter(adapter);

    gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view,
                                int position, long id) {
            Toast.makeText(view.getContext(), "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
            Log.d("MyApp", "I am here");

            //setContentView(R.layout.list_item_main);

            ListItemMainFragment nextFrag = new ListItemMainFragment();

            FragmentTransaction ft = getFragmentManager().beginTransaction();

            ft.replace(R.id.grid, nextFrag);
            ft.setTransition(FragmentTransaction.TRANSIT_NONE);// it will anim while calling fragment.
            ft.addToBackStack(null); // it will manage back stack of fragments.
            ft.commit();
        }
    });

    return view;
 }
}

片段资源文件:     

<GridView
    android:numColumns="auto_fit"
    android:gravity="center"
    android:columnWidth="100dp"
    android:stretchMode="columnWidth"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/grid"
    />

</RelativeLayout>

替换片段:

 public class ListItemMainFragment extends Fragment implements  Item.DataListener {
private ListView mListView;

public ListItemMainFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.list_item_main, container, false);
    mListView = (ListView) view.findViewById(R.id.item_list_view);

    //final String DBReference = "all/" + position + "/" + web[+position];
    final String DBReference = "all/0/Doctors";
    Item.getRecipesFromDB(ListItemMainFragment.this, DBReference);

    return view;
}

@Override
public void newDataReceived(ArrayList<Item> itemList) {
    ItemAdapter adapter = new ItemAdapter(mListView.getContext(), itemList);
    mListView.setAdapter(adapter);
 }
}

物品清单资源档案:          

<ListView
    android:id="@+id/item_list_view"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
</ListView>

1 个答案:

答案 0 :(得分:1)

在您的网格视图所在的Fragment Resource file内执行此操作,在您的相对布局中添加名为content(或任何其他名称)的ID,如android:id="@+id/content",然后在您的代码中正在进行替换片段,将此行代码ft.replace(R.id.grid, nextFrag);编辑为ft.replace(R.id.content, nextFrag);。我处于同样的困境,这对我有用。

<RelativeLayout>
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:id="@+id/content"

<GridView
   android:numColumns="auto_fit"
   android:gravity="center"
   android:columnWidth="100dp"
   android:stretchMode="columnWidth"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:id="@+id/grid" />

</RelativeLayout>

Java代码class OneFragment

        ListItemMainFragment nextFrag = new ListItemMainFragment();
        FragmentTransaction ft = getFragmentManager().beginTransaction();

        ft.replace(R.id.content, nextFrag);
        ft.setTransition(FragmentTransaction.TRANSIT_NONE);// it will anim while calling fragment.
        ft.addToBackStack(null); // it will manage back stack of fragments.
        ft.commit();