-4
    package com.example.scripthelper;

    import android.app.ActionBar.LayoutParams;
    import android.app.Activity;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.LinearLayout;
    import android.widget.TableLayout;
    import android.widget.TableRow;
    import android.widget.TextView;

    public class AddLine extends Activity implements OnClickListener {

    private static final String TAG = "myLogs";


    Button btnAdd;
    Button btnCancel;

    EditText camNumberText;
    EditText shotNumberText;
    EditText descriptionText;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_line);

        btnAdd = (Button) findViewById(R.id.btnAdd);
        btnAdd.setOnClickListener(this);
        btnCancel = (Button) findViewById(R.id.btnCancel);
        btnCancel.setOnClickListener(this);
        camNumberText = (EditText) findViewById(R.id.camNumberText);
        shotNumberText = (EditText) findViewById(R.id.shotNumberText);
        descriptionText = (EditText) findViewById(R.id.descriptionText);



    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.btnAdd:
            TableLayout tl = (TableLayout)findViewById(R.id.mainTableLayout);
            TableRow row = new TableRow(this);
            LinearLayout rowLayout = new LinearLayout(this);
            TextView tv = new TextView(this);
            tv.setText(camNumberText.getText().toString());
            LayoutParams marginsRight = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
            marginsRight.setMargins(50, 100, 25, 100);
            Log.d(TAG, "Right margin 30");
            tv.setLayoutParams(marginsRight);
            TextView tv1 = new TextView (this);
            tv1.setText(descriptionText.getText().toString());
            Log.d(TAG, "Description text good");
            //View row = getLayoutInflater().inflate(R.layout.activity_main, row);
            tl.addView(row);
            row.addView(rowLayout);
            rowLayout.addView(tv);
            //rowLayout.addView(tv1);
        case R.id.btnCancel:
            break;

        }
    }



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_add_line, menu);
        return true;
    }

   }

日志猫:

09-10 15:01:25.801: E/AndroidRuntime(2758): FATAL EXCEPTION: main
09-10 15:01:25.801: E/AndroidRuntime(2758): java.lang.NullPointerException
09-10 15:01:25.801: E/AndroidRuntime(2758):     at com.example.scripthelper.AddLine.onClick(AddLine.java:64)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.view.View.performClick(View.java:4084)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.view.View$PerformClick.run(View.java:16966)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.os.Handler.handleCallback(Handler.java:615)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.os.Looper.loop(Looper.java:137)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at android.app.ActivityThread.main(ActivityThread.java:4745)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at java.lang.reflect.Method.invokeNative(Native Method)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at java.lang.reflect.Method.invoke(Method.java:511)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-10 15:01:25.801: E/AndroidRuntime(2758):     at dalvik.system.NativeStart.main(Native Method)

我刚刚启动了android,想知道为什么它会导致这个错误。我在想它可能与 addLine.xml 中activityMain.xml而不是 addLine.xml 中的视图有关。

谢谢!

编辑* 这是导致问题的行。第 64 行:“tl.addView(row);”

4

2 回答 2

1

tl是空的。

TableLayout tl = (TableLayout)findViewById(R.id.mainTableLayout); //returns null
tl.addView(row); //throws NPE

findViewById()可以返回 null,请检查您的R.id.mainTableLayout

于 2013-09-10T13:23:02.340 回答
1

文档说,关于findViewById()

查找具有给定 id 的子视图。如果此视图具有给定的 id,则返回此视图。

这意味着您只能获取您加载的布局的子视图setContentView(); 如果您尝试从另一个布局加载视图,则findViewById()返回null

从文档:

返回:在层次结构中具有给定 id 的视图或 null

因此,如果您尝试获取R.id.mainTableLayout,正如您所说的那样在另一个活动中,则返回 null

于 2013-09-10T15:36:48.500 回答