我试图在用xml创建的RelativeLayout alredy中插入我用Java创建的GridView。 我收到的错误是" java.lang.UnsupportedOperationException:AdapterView"不支持addView(View)。每次进入addView函数时它都会崩溃我的应用程序。 我尝试创建一个相对的错误的LinearLayout,但仍然是相同的错误。
以下是代码:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main3"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.work.app2.Main3Activity">
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="@+id/buttonDel" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/RL">
</RelativeLayout>
和Java:
RelativeLayout RelView;
private void addButton() {
RelView = (RelativeLayout) findViewById(R.id.RL);
GridView GV = new GridView(this);
GV.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
));
GV.setColumnWidth(100);
GV.setNumColumns(GridView.AUTO_FIT);
GV.setStretchMode(GridView.STRETCH_COLUMN_WIDTH);
GV.addView(RelView);//<-----Problem here
GridAdapter adapter = new GridAdapter(Main3Activity.this,IconsL,Icons2L,nstring);
RelatLayout.setAdapter(adapter);
RelatLayout.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, android.view.View view, int position, long id) {
showMessage("ss", "click"+position);
}
});
}
答案 0 :(得分:0)
您正在将相对布局添加到网格视图中。尝试执行相反的RelView.addView(GV)