2016-11-22 7 views
0

xmlで作成したRelativeLayout alredyにJavaで作成したGridViewを挿入しようとしています。 私が受け取るエラーは "java.lang.UnsupportedOperationException:addView(View)はAdapterViewでサポートされていません"で、アプリケーションがaddView関数を呼び出すたびにアプリケーションがクラッシュします。 私は相対的なLinearLayout instedを作成しようとしましたが、それと同じエラーです。あなたはグリッドビューに相対的なレイアウトを追加しているプログラムで作成したGridViewをRelativeLayoutに追加する

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

あなたは 'RelView.addView(GV)'を意味しますか? – nandsito

+0

最初のことは、あなたのXMLでは、相対レイアウト(id = RL)の終了タグが見つからないということです。それを修正した後、すべてのアダプタを設定した後にグリッドビューをrlビューに追加します。 – eshb

答えて

0

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

はコードです。反対にしてみてくださいRelView.addView(GV)

+0

あなたはRelView.addView(GV)が見つからなかったので、今すぐ動作します。ありがとうございました。 – Robert

関連する問題