在按钮单击时动态地将表行添加到表布局

时间:2013-08-19 07:18:21

标签: android nullpointerexception android-tablelayout dynamically-generated tablerow

每次在我的Android应用程序上单击按钮时,我都会尝试向表中添加新行。每次我点击添加行按钮时,它都会崩溃,并且在我尝试将行添加到表中的行上给出错误。错误是java.lang.NullPointerException(不确定这意味着我对android开发很新)。

以下是我的代码。任何输入都有帮助

错误发生在table.addView(tr,new ....... line(几乎是最后一行)。

public class addItem extends Activity 
{

    private TableLayout table = null;

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

        table = (TableLayout)findViewById(R.id.maintable);

        Button additembutton = (Button) findViewById(R.id.additembutton);
        additembutton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {

                update();
            }
        });

    }

    private void update()
    {
        TableRow tr = new TableRow(addItem.this);
        tr.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));

        TextView tv = new TextView(addItem.this);
        tv.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
        tv.setText("New Entry");
        tr.addView(tv);

        table.addView(tr, new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));


    }}
似乎除了最后一行之外,每个都没有问题。

logcat报告:

08-19 06:57:54.002: E/AndroidRuntime(3486): FATAL EXCEPTION: main 08-19 06:57:54.002: E/AndroidRuntime(3486): java.lang.NullPointerException 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem.update(addItem.java:47) 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem.access$0(addItem.java:37) 08-19 06:57:54.002: E/AndroidRuntime(3486): at com.mirafriends.recipeme.addItem$1.onClick(addItem.java:30) 08-19 06:57:54.002: E/AndroidRuntime(3486): at android.view.View.performClick(View.java:4204) 

1 个答案:

答案 0 :(得分:0)

我做了类似的事情来动态地向表中添加行。

请通过:

//----------------Essay name table body------------------------------------------
                essay_title_tr_data = new TableRow(this);
                essay_title_tr_data.setId(10);
                essay_title_tr_data.setBackgroundResource(R.drawable.grey_list_bg);
                essay_title_tr_data.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));


                    final TextView essay_title = new TextView(this);
                    essay_title.setId(20);
                    essay_title.setText(Html.fromHtml(parser.getValue(e, KEY_Title)));
                    essay_title.setTextColor(Color.BLACK);
                    essay_title.setPadding(5,5,5,5);
                    essay_title.setTextSize(10);
                    essay_title_tr_data.addView(essay_title);// add the column to the table row here


                    final TextView essay_description = new TextView(this);
                    essay_description.setId(20);
                    essay_description.setText(Html.fromHtml(parser.getValue(e, KEY_Description)));
                    essay_description.setTextColor(Color.BLACK);
                    essay_description.setPadding(5,5,5,5);
                    essay_description.setTextSize(10);
                    essay_title_tr_data.addView(essay_description);// add the column to the table row here
//                  String strSubmissionDate=parser.getValue(e, KEY_DateofFormSubmission);

                    final String url=parser.getValue(e, KEY_PDFUrl);



                    essay_title_table.addView(essay_title_tr_data, new TableLayout.LayoutParams(
                            LayoutParams.FILL_PARENT,
                            LayoutParams.WRAP_CONTENT));

布局文件:

<RelativeLayout
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:layout_below="@+id/relativeLayout1"
     android:layout_centerHorizontal="true" 
     android:padding="3dp">

     <RelativeLayout
         android:layout_width="match_parent"
         android:layout_height="match_parent"
         android:layout_alignParentTop="true"
         android:layout_centerHorizontal="true" 
         android:background="#FFFFFF">

         <ScrollView
             android:id="@+id/scrollView1"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true" >

             <RelativeLayout
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content" >

                 <ImageView
                     android:id="@+id/imageView1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentLeft="true"
                     android:layout_alignParentTop="true"
                     android:src="@android:drawable/ic_input_get" />

                 <TextView
                     android:id="@+id/textView1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_alignParentTop="true"
                     android:layout_marginLeft="24dp"
                     android:layout_toRightOf="@+id/imageView1"
                     android:text="@string/essay_corner"
                     android:textColor="#000000"
                     android:textSize="16sp" />

                 <HorizontalScrollView
                     android:id="@+id/horizontalScrollView1"
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content"
                     android:layout_below="@+id/imageView1"
                     android:layout_centerHorizontal="true" >

                     <RelativeLayout
                     android:layout_width="wrap_content"
                     android:layout_height="wrap_content" >

                     <TableLayout
                         android:id="@+id/essay_title_table"
                         android:layout_width="wrap_content"
                         android:layout_height="wrap_content"
                         android:layout_centerHorizontal="true"
                         android:layout_centerVertical="true" >


                     </TableLayout>

                 </RelativeLayout>
                 </HorizontalScrollView>

             </RelativeLayout>

         </ScrollView>

     </RelativeLayout>

 </RelativeLayout>

我希望你会有所了解。