gpt4 book ai didi

android - 在线程中创建动态 tableLayout

转载 作者:行者123 更新时间:2023-11-29 16:20:25 26 4
gpt4 key购买 nike

我想在线程中创建一个动态表格布局(因为我有一个巨大的表格并且加载时间很长)。它似乎一直工作到第四行,但在出现错误之后。

我找不到问题出在哪里。在此 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)

最佳答案

请尝试下面编写的代码。您当前的代码有一些修改。所有的东西都是通过代码动态生成的。

public class DialogActivity extends Activity {

private LinearLayout mainLayout;
private LinearLayout innerLayout;
private TextView tv;
private ProgressDialog pd;
private int size = 10;
private int increment = 0;
private TableLayout table;
private Context context;
private TableRow row;

@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
context = this;

mainLayout = new LinearLayout(context);
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
mainLayout.setOrientation(LinearLayout.VERTICAL);

innerLayout = new LinearLayout(context);
innerLayout.setLayoutParams(new LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
innerLayout.setOrientation(LinearLayout.VERTICAL);

tv = new TextView(context);
tv.setText("Press any key to start calculation");

table = new TableLayout(context);
table.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
table.setBackgroundColor(Color.parseColor("#F87907"));

pd = new ProgressDialog(context);
pd.setMessage("Calculating");
pd.setTitle("Working....");
pd.setIndeterminate(true);
pd.setMax(size);
pd.show();

ThreadRender thread = new ThreadRender();
thread.start();

}

private class ThreadRender extends Thread {

public void run() {
Looper.prepare();
TextView label = null;

for (int i = 0; i < size; i++) {
row = new TableRow(context);
// 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(context);
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);
increment++;
handler.sendEmptyMessage(101);
}
handler.sendEmptyMessage(102);
}
}

private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 102:
innerLayout.addView(tv);
innerLayout.addView(table);
mainLayout.addView(innerLayout);
setContentView(mainLayout);
pd.dismiss();
tv.setText("ok");
break;
case 101:
Log.i("Test", " " + increment);
pd.setProgress(increment);
break;
}
}
};
}

关于android - 在线程中创建动态 tableLayout,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7386164/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com