2012-05-07 3 views
1

내가있는 ScrollView 안에 TableLayout을 만들려고하고있는 오류를 제공하지만 내가 여기에 이미지 뷰 내가 내 로그 캣 오류TableLayout을가있는 ScrollView

Logcat error 

05-07 15:36:33.193: E/AndroidRuntime(681): java.lang.ClassCastException: android.widget.TableLayout$LayoutParams 

주요 넣어 사용하여 동적으로 채울 것입니다 whichi 화면에 여러 TableLayout을이 erorr 날 수 있습니다. XML

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 
     <ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
     > 
    <TableLayout android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/itable"> 
     <TableRow 
       android:id="@+id/trstate" 
       android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
     > 
     <TextView 
     android:id="@+id/tstate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="NewYork" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
      </TableRow> 
     <TableRow 
       android:id="@+id/trimage" 
       android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
       > 
     <ImageView 
       android:id="@+id/iimage" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
         /> 

     </TableRow> 

    </TableLayout> 

     </ScrollView> 

</LinearLayout> 

TableActivity.java

package com.table.image; 

    import java.io.BufferedInputStream; 
    import java.io.InputStream; 
    import java.net.URL; 
    import java.net.URLConnection; 
    import android.app.Activity; 
    import android.content.Context; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
     import android.os.Bundle; 
    import android.util.Log; 
    import android.view.Gravity; 
    import android.widget.ImageView; 
    import android.view.ViewGroup; 
    import android.widget.TableLayout; 
    import android.widget.TableLayout.LayoutParams; 
    import android.widget.TableRow; 
import android.widget.TextView; 

public class TableLayoutActivity extends Activity { 
    /** Called when the activity is first created. */ 

    public TableLayout itable; 
    public TableRow trimage; 
    public TableRow trstate; 
    public ImageView iimage; 
    public TextView tstate; 



    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 


     itable = (TableLayout)findViewById(R.id.itable); 
     itable.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 

     for (int i = 0; i < 3; i++) { 

      trstate = new TableRow(this); 
      trstate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 

      tstate = new TextView(this); 
      tstate.setLayoutParams(new TableRow.LayoutParams(50,50)); 
      tstate.setText("NewYork"); 
      trstate.addView(tstate); 



      trimage = new TableRow(this); 
      trimage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
        LayoutParams.WRAP_CONTENT)); 
      for (int j = 0; j < 3; j++) 
      { 
       Log.i("Test",Integer.toString(i) + "-" + Integer.toString(j)); 
       iimage = new ImageView(this); 
       iimage.setPadding(8,8,8,8); 
       iimage.setLayoutParams(new TableRow.LayoutParams(100,100)); 
       iimage.setImageResource(R.drawable.abc); 
       trimage.addView(iimage); 
      }    
      itable.addView(trimage); 
      itable.addView(trstate); 

     } 

    } 
} 

답변

2

TableLayoutLinearLayout 안에 잘못 작성하고 있습니다. ScrollView을 루트 레이아웃으로 설정하십시오.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:orientation="vertical"> 

<LinearLayout 
android:id="@+id/linearLayout1" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:gravity="center"> 

<AutoCompleteTextView 
android:id="@+id/autoCompleteTextView1" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:layout_weight="1" 
android:ems="10" 
android:text="AutoCompleteTextView" > 

<requestFocus /> 

</AutoCompleteTextView> 

</LinearLayout> 

<ScrollView 
android:id="@+id/scrollview1" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
android:orientation="vertical" android:layout_marginTop="10dip"> 

<TableLayout android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
      android:id="@+id/itable"> 
<TableRow 
     android:id="@+id/trstate" 
     android:layout_height="wrap_content" 
      android:layout_width="wrap_content"> 

</TableRow> 
<TableRow 
     android:id="@+id/trimage" 
     android:layout_height="wrap_content" 
      android:layout_width="wrap_content" 
     > 
<ImageView 
     android:id="@+id/iimage" 
android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
       /> 

<TextView 
    android:id="@+id/tstate" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="NewYork" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

</TableRow> 

</TableLayout> 

</ScrollView> 

</LinearLayout> 

편집 :

public class SampleAndroidActivity extends Activity { 
/** Called when the activity is first created. */ 

public TableLayout itable; 
public TableRow trimage; 
public TableRow trstate; 
public ImageView iimage; 
public TextView tstate; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    itable = (TableLayout) findViewById(R.id.itable); 
    /*Remove this line 
    * itable.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
    * LayoutParams.WRAP_CONTENT)); 
    */ 

    for (int i = 0; i < 3; i++) { 

     trstate = new TableRow(this); 
     trstate.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 

     tstate = new TextView(this); 
     tstate.setLayoutParams(new LayoutParams(50, 50)); 
     tstate.setText("NewYork"); 
     trstate.addView(tstate); 

     trimage = new TableRow(this); 
     trimage.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, 
       LayoutParams.WRAP_CONTENT)); 
     for (int j = 0; j < 3; j++) { 
      Log.i("Test", Integer.toString(i) + "-" + Integer.toString(j)); 
      iimage = new ImageView(this); 
      iimage.setPadding(8, 8, 8, 8); 
      iimage.setLayoutParams(new LayoutParams(100, 100)); 
      iimage.setImageResource(R.drawable.events); 
      trimage.addView(iimage); 
     } 
     itable.addView(trimage); 
     itable.addView(trstate); 

    } 

} 
} 
+0

예 코드가 작동하지만 화면 맨 위에 문제가 하나 더 생깁니다 다른 부분이 스크롤되지 않는 수동 검색 상자가 있습니다 스크롤은 스크롤입니다. 그래서 scrollview에 기본 선형 레이아웃을 넣으면 어떻게 할 수 있습니까? 전체 화면 스크롤보다하지만 내 검색 상자가 아닌 테이블 레이아웃 만 스크롤하려합니다 – Sunny

+0

이 링크보기 Sunny

+0

위 링크는 표시되지 않습니다 .. – Nishant

0

변경하여 .xml 파일의 코드입니다. 는 문제를 해결할 수 있습니다 측면에서 그 의미 태그,

<ScrollView 
      android:layout_width="fill_parent" 
      android:layout_height="fill_parent" 
     > 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" > 

<TableLayout android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
        android:id="@+id/itable"> 
     <TableRow 
       android:id="@+id/trstate" 
       android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
     > 
     <TextView 
     android:id="@+id/tstate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="NewYork" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 
      </TableRow> 
     <TableRow 
       android:id="@+id/trimage" 
       android:layout_height="wrap_content" 
        android:layout_width="wrap_content" 
       > 
     <ImageView 
       android:id="@+id/iimage" 
     android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
         /> 

     </TableRow> 

    </TableLayout> 

</LinearLayout> 

</ScrollView> 

을 태그를 넣어.