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>
LOG:
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
答案 0 :(得分: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);
答案 1 :(得分: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));
答案 2 :(得分:0)
也许你在不同的XML中有重复的元素TableLayout1?
答案 3 :(得分:0)
好吧,我用了,接下来,删除了Tablelayout的XML文件,并在同一个位置创建一个新的,用另一个id,例如:android:id =“@ + id / tableMenu”,和你的 文件java是下一个:
TableLayout myTable =(TableLayout)findViewById(R.id.tableMenu);
对我来说是解决方案,你可以尝试一下