简体   繁体   English

Android以编程方式创建TableLayout

[英]Android creating TableLayout programmatically

I created a sample Android project and I can display a Toast. 我创建了一个示例Android项目,可以显示Toast。 Now I want to display some data in a table. 现在,我想在表中显示一些数据。

I have tried this in my onCreate method (full method here): 我已经在我的onCreate方法(此处为完整方法)中进行了尝试:

  @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
            .findFragmentById(R.id.navigation_drawer);
    mTitle = getTitle();

    // Set up the drawer.
    mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
            (DrawerLayout) findViewById(R.id.drawer_layout));

    try {

        TableLayout table = (TableLayout) findViewById(R.id.navigation_drawer);
            TableRow tr = new TableRow(this);
            TextView textview = new TextView(this);
            textview.setText("Sup");
            textview.setTextColor(Color.YELLOW);

            tr.addView(textview);

            table.addView(tr);

        Toast.makeText(getApplicationContext(), "Well Done on getting the data", Toast.LENGTH_LONG).show();

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

And here is my fragment_main.xml file: 这是我的fragment_main.xml文件:

  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relLay"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment" >

<TextView
    android:id="@+id/section_label"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignTop="@+id/section_label"
    android:layout_centerHorizontal="true"
    android:text="Welcome!"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TableLayout
    android:id="@+id/MyLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/section_label"
    android:layout_marginLeft="18dp"
    android:layout_marginTop="40dp" >

    <TableRow
        android:id="@+id/tableRow1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

    <TableRow
        android:id="@+id/tableRow3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    </TableRow>

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

The problem is when I run the app I get this: 问题是当我运行该应用程序时,我得到以下信息:

   java.lang.NullPointerException
    at com.example.helloworld.MainActivity.onCreate(MainActivity.java:115)
    at android.app.Activity.performCreate(Activity.java:5431)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
    at android.app.ActivityThread.access$900(ActivityThread.java:161)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)

This is line 115: 这是第115行:

  table.addView(tr);

Why is the table null? 为什么表为空? And why is it so difficult to get a simple table to display? 为什么要显示一个简单的表格如此困难? Help me please.. 请帮帮我..

Just check that in your MainActivity class you have set 只需检查您在MainActivity类中设置的

setContentView(R.layout.fragment_main);

instead of 代替

setContentView(R.layout.activity_main);

there is a issue with your wrong layout file. 错误的布局文件存在问题。

Note : Also you have a TableRow in xml file so no need to create dynamically. 注意 :此外,您在xml文件中还有一个TableRow ,因此无需动态创建。 If you want to create dynamically then you have to remove all your views before add a table row in your TableLayout . 如果要动态创建,则必须先删除所有视图,然后才能在TableLayout添加表行。

您是否尝试设置TableRow id属性?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM