尝试以编程方式将TableRow添加到TableLayout时出现NullPointerException

时间:2014-06-18 14:54:40

标签: java android xml nullpointerexception android-tablelayout

我试图以编程方式将TableRow添加到TableLayout,但它一直给我一个NPE。

这是我的onCreate方法:

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

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }   

    try {
        BufferedReader br = new BufferedReader(new InputStreamReader(openFileInput(MainActivity.FILE_SERVERS)));
        TableLayout table = (TableLayout) findViewById(R.id.table_servers);
        String line = null;
        while ((line = br.readLine()) != null){
            String[] values = line.split(",");
            MCServer server = new MCServer(values[0], values[1], values[2], values[3], Boolean.parseBoolean(values[4]));
            servers.add(server);
            TableRow row = new TableRow(this);
            TextView name = new TextView(this);
            TextView ip = new TextView(this);
            TextView interval = new TextView(this);
            TextView status = new TextView(this);
            name.setText(values[0]);
            ip.setText(values[1] + ":" + values[2]);
            interval.setText(values[3]);
            status.setText("----");
            row.addView(name);
            row.addView(ip);
            row.addView(interval);
            row.addView(status);
            table.addView(row); 
        }
        br.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

这是我的fragment_main.xml:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/table_servers"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TableRow>
    <TextView
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_weight="1"
        android:padding="3dip"
        android:text="@string/servers_table_r1c1" />
    <TextView
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_weight="1"
        android:padding="3dip" 
        android:text="@string/servers_table_r1c2" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_weight="0.6"
        android:padding="3dip"
        android:text="@string/servers_table_r1c3" />

    <TextView
        android:layout_width="0dp"
        android:layout_height="23dp"
        android:layout_weight="0.6"
        android:padding="3dip"
        android:text="@string/servers_table_r1c4" />
</TableRow>
<!-- This one is for testing purposes -->
<TableRow>
    <TextView
        android:text="@string/servers_table_r1c1"
        android:padding="3dip" />
    <TextView
        android:text="@string/servers_table_r1c2"
        android:padding="3dip" />
    <TextView
        android:text="@string/servers_table_r1c3"
        android:padding="3dip" />
    <TextView
        android:text="@string/servers_table_r1c4"
        android:padding="3dip" />
</TableRow>

</TableLayout>

它在这条线上给了我一个NPE:

table.addView(row);

我不知道问题所在,我该如何解决?

1 个答案:

答案 0 :(得分:0)

由于您将这些视图放置在 Fragment的布局中,因此在片段到位之前它们不属于活动。

简而言之:将try...代码移动到片段onCreateView()中。而不是findViewById,请使用rootView.findViewById(),其中rootView是方法返回的视图。