【问题标题】:Null pointer exception in Dynamically add TableRow to TableLayout将 TableRow 动态添加到 TableLayout 中的空指针异常
【发布时间】:2012-09-26 11:59:30
【问题描述】:

我在冲浪时遇到了一个概念,以创建动态表格行并向其中添加内容。在代码下方描述 此代码抛出 NullPointerException。

布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:stretchColumns="0,1"
                android:id="@+id/tabledisplay" android:layout_weight="1" android:layout_height="wrap_content" android:layout_width="match_parent">
        </TableLayout>

代码

public void display_table(){
    try{
    TableLayout tl = (TableLayout)findViewById(R.id.tabledisplay);

    DBConnection db= new DBConnection();
    Connection con=db.getConnection();
    Statement st=con.createStatement();
    ResultSet rs=st.executeQuery("select report_name,report_createddate,user_name from reports");
    TableRow tr_head = new TableRow(this);
    tr_head.setId(10);
    tr_head.setBackgroundColor(Color.GRAY);
    tr_head.setLayoutParams(new LayoutParams(
    LayoutParams.FILL_PARENT,
    LayoutParams.WRAP_CONTENT));
    TextView label_date = new TextView(this);
    label_date.setId(20);
    label_date.setText("DATE");
    label_date.setTextColor(Color.WHITE);
    label_date.setPadding(5, 5, 5, 5);
    tr_head.addView(label_date);// add the column to the table row here

    TextView label_weight_kg = new TextView(this);
    label_weight_kg.setId(21);// define id that must be unique
    label_weight_kg.setText("Wt(Kg.)"); // set the text for the header 
    label_weight_kg.setTextColor(Color.WHITE); // set the color
    label_weight_kg.setPadding(5, 5, 5, 5); // set the padding (if required)
    tr_head.addView(label_weight_kg); // add the column to the table row here
    tl.addView(tr_head, new TableLayout.LayoutParams(
            LayoutParams.FILL_PARENT,
            LayoutParams.WRAP_CONTENT));
    Integer count=0;
    while (rs.next()) {
   String date = (rs.getString(1));// get the first variable
   String weight_kg = (rs.getString(2));// get the second variable
   // Create the table row
   TableRow tr = new TableRow(this);
   if(count%2!=0) tr.setBackgroundColor(Color.GRAY);
   tr.setId(100+count);
   tr.setLayoutParams(new LayoutParams(
   LayoutParams.FILL_PARENT,
   LayoutParams.WRAP_CONTENT));

   //Create two columns to add as table data
    // Create a TextView to add date
   TextView labelDATE = new TextView(this);
   labelDATE.setId(200+count); 
   labelDATE.setText(date);
   labelDATE.setPadding(2, 0, 5, 0);
   labelDATE.setTextColor(Color.WHITE);
   tr.addView(labelDATE);
   TextView labelWEIGHT = new TextView(this);
   labelWEIGHT.setId(200+count);
   labelWEIGHT.setText(weight_kg.toString());
   labelWEIGHT.setTextColor(Color.WHITE);
   tr.addView(labelWEIGHT);

   // finally add this to the table row
   tl.addView(tr, new TableLayout.LayoutParams(
                        LayoutParams.FILL_PARENT,
                        LayoutParams.WRAP_CONTENT));
               count++;
            }   


    }catch (Exception e) {
        // TODO: handle exception
        Toast.makeText(getApplicationContext(),e.toString(),Toast.LENGTH_LONG).show();
    }

}

请让我知道我的代码出了什么问题?

日志错误

09-25 10:02:58.125: I/System.out(560): java.lang.NullPointerException 09-25
10:02:58.154: I/System.out(560):    at  
android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:100) 09-25  
10:02:58.154: I/System.out(560):    at
co.analuo.app.HorzScrollWithListMenu.display_table(HorzScrollWithListMenu.java:189) 
09-25 10:02:58.164: I/System.out(560):  at 
co.analuo.app.HorzScrollWithListMenu$1.onItemClick(HorzScrollWithListMenu.java:111)   
09-25 10:02:58.164: I/System.out(560):  at 
android.widget.AdapterView.performItemClick(AdapterView.java:284) 09-25 10:02:58.164:  
I/System.out(560):  at android.widget.ListView.performItemClick(ListView.java:3513) 
09-25 10:02:58.164: I/System.out(560):  at 
android.widget.AbsListView$PerformClick.run(AbsListView.java:1812) 09-25 10:02:58.175: 
I/System.out(560):  at android.os.Handler.handleCallback(Handler.java
:587)
09-25 10:02:58.185: I/System.out(560):  at   
android.os.Handler.dispatchMessage(Handler.java:92)
09-25 10:02:58.185: I/System.out(560):  at android.os.Looper.loop(Looper.java:123)
09-25 10:02:58.185: I/System.out(560):  at 
android.app.ActivityThread.main(ActivityThread.java:3683)
09-25 10:02:58.204: I/System.out(560):  at 
java.lang.reflect.Method.invokeNative(Native Method)
09-25 10:02:58.204: I/System.out(560):  at 
 java.lang.reflect.Method.invoke(Method.java:507)
 09-25 10:02:58.204: I/System.out(560):     at 
 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
09-25 10:02:58.204: I/System.out(560):  at 
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
09-25 10:02:58.204: I/System.out(560):  at dalvik.system.NativeStart.main(Native Method)

【问题讨论】:

  • 你能指出哪一行抛出 NullPointerException。
  • 在这一行 TableLayout tl = (TableLayout)findViewById(R.id.tabledisplay);
  • 你是在调用setContentView之后调用这个方法display_table吗?你能发布完整的错误日志吗?
  • @Sathish:你能告诉我们你在调用 display_table 方法之前已经完成了 setContentView 吗?
  • findViewById(...) 不会导致NullPointerException 即使找不到具有您传递给它的资源ID 的UI 元素。你确定NPE 发生在TableLayout tl = (TableLayout)findViewById(R.id.tabledisplay); 行吗?

标签: android android-tablelayout


【解决方案1】:

我遇到了同样的问题,所以这是您的解决方案.. 不要将 Tablelayout 作为根布局 例如

`<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
    android:id="@+id/tlayoutID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>`

【讨论】:

    【解决方案2】:

    终于解决了

    如果 findViewById() 返回 NPE,请尝试以下方法:

    • 通过项目清理项目 -> 清理... -> 检查您的项目 -> 确定
    • 确保 ID 中绝对没有拼写错误
    • 确保 contentView 在 TableLayout 所在的位置显示正确的布局
    • 确保您在setContentView 之前findViewById

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

    在每个步骤之后重新清理项目也可能会有所帮助。

    引用自https://stackoverflow.com/a/11671533/1602230

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-19
      • 1970-01-01
      • 2023-03-18
      • 2012-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多