Android以编程方式将RelativeLayout添加到TableLayout

时间:2012-01-24 16:22:24

标签: android relativelayout tablelayout tablerow

所以,我决定学习编写Android应用程序。我更喜欢通过编写代码来学习,所以我设置了我的环境并潜入Android!

然后打了我的脑袋:)。

我正在尝试将导入RelativeLayout作为TableRows添加到表视图中。

这是我想要重复使用的表格布局,

 <?xml version="1.0" encoding="utf-8"?>   
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/upload_list_table"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:stretchColumns="*" >

</TableLayout>

我把它包含在我的主要活动中

<?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:id="@+id/home_activity"
  android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >

<include android:id="@+id/homeActivityUploadList" layout="@layout/upload_list_table" />
</LinearLayout>

然后,我用RelativeLayout创建了我的行

<?xml version="1.0" encoding="utf-8"?>
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/upload_list_table_row_relative"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip" >



<ImageView
    android:id="@+id/fileThumb"
    android:layout_width="90px"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_gravity="left"
    android:padding="2px"

    android:src="@drawable/nothumb" />

<TextView
    android:id="@+id/fileDescription"
    android:layout_width="fill_parent"
    android:layout_height="26dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentRight="true"
    android:layout_toRightOf="@id/fileThumb"
    android:ellipsize="marquee"
    android:singleLine="true"
    android:text="Desc" />


<TextView
    android:id="@+id/fileTitle"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_above="@id/fileDescription"
    android:layout_alignParentRight="true"
    android:layout_alignParentTop="true"
    android:layout_alignWithParentIfMissing="true"
    android:layout_toRightOf="@id/fileThumb"
    android:gravity="center_vertical"
    android:text="Title" />

 </RelativeLayout>

现在,我正在尝试这个

 public class HomeActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_activity);


    TableLayout tbl = (TableLayout)findViewById(R.id.homeActivityUploadList);
    RelativeLayout row = (RelativeLayout)findViewById(R.layout.upload_list_table_row_relative);

    tbl.addView(row);


}

但是,我得到NullPointerException。它找不到

RelativeLayout row = (RelativeLayout)findViewById(R.layout.upload_list_table_row_relative);

我也试过

RelativeLayout row = (RelativeLayout)findViewById(R.id.upload_list_table_row_relative);

我做错了什么?

由于

日志输出

    01-24 11:30:15.757: D/dalvikvm(1625): GC_EXTERNAL_ALLOC freed 46K, 53% free    2539K/5379K, external 1625K/2137K, paused 68ms

01-24 11:30:15.877:D / dalvikvm(1625):GC_EXTERNAL_ALLOC释放1K,53%免费2538K / 5379K,外部2250K / 2810K,暂停58ms 01-24 11:30:15.967:D / AndroidRuntime(1625):关闭VM 01-24 11:30:15.967:W / dalvikvm(1625):threadid = 1:线程退出未捕获异常(组= 0x40015560) 01-24 11:30:15.977:E / AndroidRuntime(1625):致命异常:主要 01-24 11:30:15.977:E / AndroidRuntime(1625):java.lang.RuntimeException:无法启动活动ComponentInfo {com。 * .HomeActivity}:java .lang.NullPointerException 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.os.Handler.dispatchMessage(Handler.java:99) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.os.Looper.loop(Looper.java:123) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread.main(ActivityThread.java:3683) 01-24 11:30:15.977:E / AndroidRuntime(1625):at java.lang.reflect.Method.invokeNative(Native Method) 01-24 11:30:15.977:E / AndroidRuntime(1625):at java.lang.reflect.Method.invoke(Method.java:507) 01-24 11:30:15.977:E / AndroidRuntime(1625):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 01-24 11:30:15.977:E / AndroidRuntime(1625):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 01-24 11:30:15.977:E / AndroidRuntime(1625):at dalvik.system.NativeStart.main(Native Method) 01-24 11:30:15.977:E / AndroidRuntime(1625):引起:java.lang.NullPointerException 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.view.ViewGroup.addView(ViewGroup.java:1821) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.widget.TableLayout.addView(TableLayout.java:403) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.view.ViewGroup.addView(ViewGroup.java:1808) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.widget.TableLayout.addView(TableLayout.java:394) 01-24 11:30:15.977:E / AndroidRuntime(1625):at com。 * .onCreate(HomeActivity.java:23) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 01-24 11:30:15.977:E / AndroidRuntime(1625):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 01-24 11:30:15.977:E / AndroidRuntime(1625):... 11 more

    android:ellipsize=

1 个答案:

答案 0 :(得分:2)

您必须使用LayoutInflater

LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout  row = (RelativeLayout ) layoutInflater.inflate(R.layout.upload_list_table_row_relative, null);