尝试以编程方式添加tableRow时获取nullPointerException

时间:2016-03-31 13:11:11

标签: android android-activity nullpointerexception

我有一个addActivity活动,我在其中输入数据。我想传递它在listActivity中创建一行。但是,出于某种原因,我得到一个nullPointerException并且应用程序崩溃了。我在尝试添加行时收到错误。

这里是代码,它出现在哪里:

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

    Button addButton = (Button) findViewById(R.id.addBttn);
    addButton.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            TableLayout tl = (TableLayout) findViewById(R.id.tableLayout);
            TableRow tr = new TableRow(AddActivity.this.getBaseContext());
            tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
            TextView tv = new TextView(AddActivity.this.getBaseContext());
            TextView tv2 = new TextView(AddActivity.this.getBaseContext());
            TableLayout.LayoutParams lp = new TableLayout.LayoutParams();
            lp.gravity= Gravity.CENTER_HORIZONTAL;
            tv2.setLayoutParams(lp);
            CheckBox cb = new CheckBox(AddActivity.this.getBaseContext());
            TableLayout.LayoutParams lp2 = new TableLayout.LayoutParams();
            lp2.gravity= Gravity.RIGHT;
            cb.setLayoutParams(lp2);

            EditText productNameEdit = (EditText) findViewById(R.id.productNameEdit);
            EditText amountEdit = (EditText) findViewById(R.id.amountEdit);

            tv.setText(productNameEdit.getText().toString());
            tv2.setText(amountEdit.getText().toString());

            tr.addView(tv);
            tr.addView(tv2);
            tr.addView(cb);

            tl.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT, TableLayout.LayoutParams.WRAP_CONTENT)); // null pointer exception 
        }
    });

}

activity_add.xml

<LinearLayout
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Nazwa:"
        android:id="@+id/productNameText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/productNameEdit"
        android:editable="true" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Ilość:"
        android:id="@+id/amountText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/amountEdit"
        android:editable="true"
        android:inputType="number" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dodaj"
        android:id="@+id/addBttn" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Anuluj"
        android:id="@+id/cancelBttn" />
</LinearLayout>

以下是日志:

03-31 09:17:07.561    1780-1780/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
        at com.example.anna.lab2.AddActivity$1.onClick(AddActivity.java:54)
        at android.view.View.performClick(View.java:4240)
        at android.view.View$PerformClick.run(View.java:17721)
        at android.os.Handler.handleCallback(Handler.java:730)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5103)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:525)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

布局文件中没有TableLayout ID tableLayout

这就是为什么

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout);

tl在这里为空。

尝试在布局中添加它:

<TableLayout
        android:id="@+id/tableLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

答案 1 :(得分:0)

您正在设置

setContentView(R.layout.activity_add);

然后你正在打电话

TableLayout tl = (TableLayout) findViewById(R.id.tableLayout);

R.id.tableLayout位于其他文件中,而不在R.layout.activity_add中,因此会抛出NullPointerException