应用程序使用表格布局的意图崩溃

时间:2014-07-28 14:33:49

标签: android android-intent runtimeexception

当我去一个页面创建一个表时,试着这样做。当我在没有桌子的情况下转移到页面时它会起作用,当我独自拥有桌子时,它工作正常,但是当我将两者合并时,它只会与AndroidRuntime error崩溃...这是我的代码:

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

    Intent i = getIntent();
    createTable();

}

public void createTable(){
     setContentView(R.layout.activity);
     TableLayout lo = (TableLayout) findViewById(R.id.displayLinear);

     TableRow headerRow = new TableRow(this);
     TableRow.LayoutParams hp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
     headerRow.setLayoutParams(hp);
     tv = new TextView(this);
     tv.setText("Text Header");
     headerRow.addView(tv);
     lo.addView(headerRow);

        for (int i = 0; i < 100; i++) {

            TableRow row= new TableRow(this);
            TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
            row.setLayoutParams(lp);
            checkBox = new CheckBox(this);
            tv = new TextView(this);
            qty = new TextView(this);
            checkBox.setText("hello");
            qty.setText("10");
            row.addView(checkBox);
            row.addView(qty);
            lo.addView(row,i);
        }
 }

logcat的:

07-28 10:17:13.400: E/AndroidRuntime(10363): FATAL EXCEPTION: main
07-28 10:17:13.400: E/AndroidRuntime(10363): Process: cooper.tirerfid, PID: 10363
07-28 10:17:13.400: E/AndroidRuntime(10363): java.lang.RuntimeException: Unable to start activity ComponentInfo{cooper.tirerfid/cooper.tirerfid.AllTiresActivity}: java.lang.NullPointerException
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2235)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2285)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread.access$800(ActivityThread.java:138)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1236)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at    android.os.Handler.dispatchMessage(Handler.java:102)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.os.Looper.loop(Looper.java:149)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread.main(ActivityThread.java:5061)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at java.lang.reflect.Method.invokeNative(Native Method)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at java.lang.reflect.Method.invoke(Method.java:515)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:788)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:604)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at dalvik.system.NativeStart.main(Native Method)
07-28 10:17:13.400: E/AndroidRuntime(10363): Caused by: java.lang.NullPointerException
07-28 10:17:13.400: E/AndroidRuntime(10363):    at cooper.tirerfid.AllTiresActivity.createTable(AllTiresActivity.java:90)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at cooper.tirerfid.AllTiresActivity.onCreate(AllTiresActivity.java:30)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.Activity.performCreate(Activity.java:5387)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-28 10:17:13.400: E/AndroidRuntime(10363):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199)

当我单步执行时,它会在此行出错:      lo.addView(headerRow);

XML文件:

<TableLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:background="#C0C0C0"  >

<!-- Once the column headers are known go back into the string.xml file and add them to elimiate     warnings -->

<TableRow
  android:background="#C0C0C0" >
<TextView android:text="RFID"
    android:layout_width="100dp"        
    android:layout_column="0"
    android:layout_weight="1"/>
<TextView android:text="Number"
    android:layout_width="30dp"
    android:layout_column="1"
    android:layout_weight="1">
</TextView>
<TextView android:text="Yes/No"
    android:layout_width="30dp"
    android:layout_column="2"
    android:layout_weight="1">
</TextView>
</TableRow>
<ScrollView android:layout_height="fill_parent">      
<TableLayout android:id="@+id/displayLinear"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#C0C0C0"  >          
<TableRow>
    <TextView android:text="Al"
    android:layout_width="100dp"        
    android:layout_column="0"
    android:layout_weight="1">
    </TextView>
    <TextView android:text="1000"
    android:layout_width="30dp"
    android:layout_column="1"
    android:layout_weight="1">
    </TextView>
    <TextView android:text="2"
    android:layout_width="30dp"
    android:layout_column="2"
    android:layout_weight="1">
    </TextView>
    </TableRow>
  </TableLayout>
</ScrollView>     

2 个答案:

答案 0 :(得分:2)

根据您发布的代码和logcat数据,我能看到的唯一可能性是

TableLayout lo = (TableLayout) findViewById(R.id.displayLinear);

返回null。您确定R.layout.activity的商品ID为displayLinear吗?如果你发布了你的activity.xml文件会有所帮助。

当您单步执行代码时,调用lofindViewById()的值是多少?

编辑: 您发布的XML无法编译。您有2个TableLayout开始标记但只有一个TableLayout结束标记。看起来你可能在错误的地方复制/粘贴了一些东西。

答案 1 :(得分:0)

很可能找不到TableLayout,因此findViewById()返回一个空指针,尝试其中一些:

  • 通过项目清理项目 - &gt;干净...... - &gt;检查你的项目 - &gt;行
  • 确保ID
  • 中绝对没有拼写错误
  • 确保contentView显示TableLayout存在的正确布局
  • 确保setContentView
  • 之前findViewById

我假设我列出的第三个选项很可能是你的问题。

在完成上述每个步骤后,重新清洁项目也会有所帮助。