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

次に、XML ファイルの Tablelayout を削除し、別の ID を使用して同じ位置に新しいファイルを作成します。

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

私にとっての解決策でした、あなたはそれを試すことができます

于 2015-09-18T23:58:10.927 に答える
0

異なる XML で TableLayout1 という要素が重複している可能性がありますか?

于 2012-05-03T23:07:18.503 に答える
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 に答える