gpt4 book ai didi

android - 不支持的操作异常 : addView(View) is not supported in AdapterView

转载 作者:行者123 更新时间:2023-11-30 01:01:33 25 4
gpt4 key购买 nike

我查看了存在相同错误的现有帖子,但代码/实现与我的不同,但是一些帖子中的建议并没有帮助解决问题。我试图在单击图像时用另一个 fragment 替换显示网格(图像列表)的 fragment 。我在运行时看到错误“Java.lang.UnsupportedOperationException:AdapterView 不支持 addView(View)”,但无法弄清楚是代码的哪一部分导致了这个错误,也不知道需要做什么来解决这个问题。感谢您帮助解决此问题。代码如下:

带网格的 fragment (图像列表):

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;
}
}

fragment 资源文件:

<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>

替换 fragment :

 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>

最佳答案

在 GridView 所在的 Fragment Resource 文件 中执行此操作,将名为 content(或任何其他名称)的 id 添加到您的 Relative 布局中,如下所示 android:id="@+id/content" 然后在您进行 fragment 替换的代码中,编辑这行代码 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代码类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();

关于android - 不支持的操作异常 : addView(View) is not supported in AdapterView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39316340/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com