Я хочу создать динамическую разметку таблицы в потоке (потому что у меня огромная таблица и загрузка довольно длинная).Кажется, работает до четвертого ряда, но после появления ошибки.
Я не могу найти, где проблема.В этом Java-коде макет таблицы был упрощен для финансирования проблемы.
Мой Java-код:
public class DialogActivity extends Activity implements Runnable {
private TextView tv;
private ProgressDialog pd;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
tv = (TextView) this.findViewById(R.id.main1);
tv.setText("Press any key to start calculation");
pd = ProgressDialog.show(this, "Working..", "Calculating", true,false);
Thread thread = new Thread(this);
thread.start();
}
public void run() {
TableLayout table = (TableLayout)findViewById(R.id.tdyn);
TableRow row=null;
TextView label = null;
int size=10;
for (int i = 0; i < size; i++)
{
row = new TableRow(this);
//row.setId(100+i);
row.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
for (int j = 0; j < size; j++)
{
Log.i("Test",Integer.toString(i) + "-" + Integer.toString(j));
label = new TextView(this);
label.setText(Integer.toString(i) + "-" + Integer.toString(j) + " ");
label.setLayoutParams(new TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
label.setGravity(Gravity.CENTER_VERTICAL);
row.addView(label);
}
if(i%2==0)
row.setBackgroundColor(0xff222222);
else
row.setBackgroundColor(0xff000000);
table.addView(row);
}
handler.sendEmptyMessage(0);
}
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
tv.setText("ok");
}
};
}
Вот мой 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"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/main1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="test"
/>
<TableLayout android:layout_height="wrap_content" android:id="@+id/tdyn" android:background="#F87907" android:layout_width="fill_parent">
<TableRow>
<TextView android:id="@+id/info"
android:text="test11"
android:textColor="#000000"
android:layout_gravity="center_horizontal" />
<TextView android:id="@+id/info"
android:text="test12"
android:textColor="#000000"
android:layout_gravity="center_horizontal" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
Спасибо за вашу помощь
Edit : You can find the the logcat :
09-12 12:06:57.973: INFO/Test(215): 8-1
09-12 12:06:57.973: INFO/Test(215): 8-2
09-12 12:06:57.983: INFO/Test(215): 8-3
09-12 12:06:57.993: INFO/Test(215): 8-4
09-12 12:06:58.003: INFO/Test(215): 8-5
09-12 12:06:58.003: INFO/Test(215): 8-6
09-12 12:06:58.012: INFO/Test(215): 8-7
09-12 12:06:58.024: INFO/Test(215): 8-8
09-12 12:06:58.024: INFO/Test(215): 8-9
09-12 12:06:58.044: WARN/dalvikvm(215): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
09-12 12:06:58.044: ERROR/AndroidRuntime(215): Uncaught handler: thread Thread-8 exiting due to uncaught exception
09-12 12:06:58.073: ERROR/AndroidRuntime(215): android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.ViewRoot.checkThread(ViewRoot.java:2683)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.ViewRoot.requestLayout(ViewRoot.java:557)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.View.requestLayout(View.java:7918)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.widget.TableLayout.requestLayout(TableLayout.java:223)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.ViewGroup.addView(ViewGroup.java:1754)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.widget.TableLayout.addView(TableLayout.java:418)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.ViewGroup.addView(ViewGroup.java:1713)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.widget.TableLayout.addView(TableLayout.java:400)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.view.ViewGroup.addView(ViewGroup.java:1693)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at android.widget.TableLayout.addView(TableLayout.java:391)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at com.sil.DialogActivity.run(DialogActivity.java:65)
09-12 12:06:58.073: ERROR/AndroidRuntime(215): at java.lang.Thread.run(Thread.java:1096)
09-12 12:06:58.102: INFO/Process(52): Sending signal. PID: 215 SIG: 3
09-12 12:06:58.102: INFO/dalvikvm(215): threadid=7: reacting to signal 3
09-12 12:06:58.102: ERROR/dalvikvm(215): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
09-12 12:06:58.464: INFO/ARMAssembler(52): generated scanline__00000077:03515104_00000000_00000000 [ 33 ipp] (47 ins) at [0x546c90:0x546d4c] in 655949 ns
09-12 12:06:58.663: INFO/ActivityManager(52): Displayed activity com.sil/.DialogActivity: 4121 ms (total 4121 ms)