0
  TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
  TableRow row = new TableRow(this);
  TextView t = new TextView(this);
  t.setText(String.valueOf(day[i]));
  row.addView(t);
  table.addView(row,new TableLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

最后一条指令是给我"Unable to start activity component info: java.lang.NullPointerException". 但是,具有相同指令的干净项​​目中的相同代码不是......

table.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<TableLayout android:id="@+id/TableLayout1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"  
    android:stretchColumns="0">
</TableLayout>
</LinearLayout>

日志:

04-27 14:19:27.267: E/AndroidRuntime(2503): FATAL EXCEPTION: main
04-27 14:19:27.267: E/AndroidRuntime(2503): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.seapredictions/com.seapredictions.ShowData}: java.lang.NullPointerException
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread.access$600(ActivityThread.java:123)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.os.Looper.loop(Looper.java:137)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread.main(ActivityThread.java:4424)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at java.lang.reflect.Method.invoke(Method.java:511)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at dalvik.system.NativeStart.main(Native Method)
04-27 14:19:27.267: E/AndroidRuntime(2503): Caused by: java.lang.NullPointerException
04-27 14:19:27.267: E/AndroidRuntime(2503):     at com.seapredictions.ShowData.onCreate(ShowData.java:33)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.Activity.performCreate(Activity.java:4465)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
04-27 14:19:27.267: E/AndroidRuntime(2503):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
04-27 14:19:27.267: E/AndroidRuntime(2503):     ... 11 more
04-27 14:19:27.636: I/dalvikvm(2503): threadid=3: reacting to signal 3
04-27 14:19:27.676: I/dalvikvm(2503): Wrote stack traces to '/data/anr/traces.txt'
04-27 14:19:27.807: I/dalvikvm(2503): threadid=3: reacting to signal 3
04-27 14:19:27.827: I/dalvikvm(2503): Wrote stack traces to '/data/anr/traces.txt'
04-27 14:19:27.887: D/dalvikvm(2503): GC_CONCURRENT freed 230K, 4% free 9243K/9543K, paused 3ms+3ms
4

4 回答 4

0

试试这个:

 TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
 TableRow tr1 = new TableRow(this);
      tr1.setLayoutParams(new LayoutParams( LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
      TextView textview = new TextView(this);
      textview.setText("Hello world");   //or whatever you want

      tr1.addView(textview);
      table.addView(tr1, new TableLayout.LayoutParams(
              LayoutParams.WRAP_CONTENT,
             LayoutParams.WRAP_CONTENT));
于 2012-04-27T14:48:29.117 回答
0

尝试这个:

TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow row = new TableRow(this);
TextView t = new TextView(this);
t.setText(String.valueOf(day[i]));
row.addView(t);
table.addView(row,new TableLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

或者你可以使用这种方式:

TableLayout table = (TableLayout) findViewById(R.id.TableLayout1);
TableRow row = new TableRow(this);

LinearLayout outerLayout = new LinearLayout(this);

LinearLayout.LayoutParams textLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);

textLayoutParams.setMargins(100, 0, 0, 0);

TextView t = new TextView(this);

t.setText(String.valueOf(day[i]));

outerLayout.addView(t, textLayoutParams);

row.addView(outerLayout);

table.addView(row);
于 2012-04-27T13:54:30.627 回答
0

也许您在不同的 XML 中有重复的元素,称为 TableLayout1?

于 2012-05-03T23:07:18.503 回答
0

好吧,我用了,下一个,删除XML文件的Tablelayout,在同一个位置新建一个,用另一个id,例如:android:id="@+id/tableMenu",你的文件java就是下一个:

TableLayout myTable = (TableLayout) findViewById(R.id.tableMenu);

是我的解决方案,你可以试试

于 2015-09-18T23:58:10.927 回答