在一个线程创建一个动态tableLayout [英] Creating a dynamic tableLayout in a thread
问题描述
我想创建一个线程动态表格布局(因为我有一个巨大的桌子和装载很长)。它似乎工作,直到第四行,但出现错误后。
我找不到在哪里的问题。在这个Java code,表格的布局已经被简化资助的问题。
我的Java code:
公共类DialogActivity扩展活动实现Runnable { 私人TextView的电视;
私人ProgressDialog PD;
@覆盖
公共无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
的setContentView(R.layout.main); 电视=(TextView中)this.findViewById(R.id.main1);
tv.setText(preSS任意键开始计算); PD = ProgressDialog.show(这一点,工作..,计算,真,假); 线程线程=新主题(本);
thread.start(); } 公共无效的run(){
TableLayout表=(TableLayout)findViewById(R.id.tdyn); 连续的TableRow = NULL;
TextView的标签= NULL; INT大小= 10;
的for(int i = 0; I<大小;我++)
{
行=新的TableRow(本);
//row.setId(100+i);
row.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
对于(INT J = 0; J<大小; J ++)
{
Log.i(测试,Integer.toString(I)+ - + Integer.toString(j)条);
标签=新的TextView(本);
label.setText(Integer.toString(I)+ - + Integer.toString(J)+);
label.setLayoutParams(新TableRow.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
label.setGravity(Gravity.CENTER_VERTICAL);
row.addView(标签);
} 如果(我%2 == 0)
row.setBackgroundColor(0xff222222);
其他
row.setBackgroundColor(0xff000000); table.addView(行);
}
handler.sendEmptyMessage(0);
} 私人处理程序处理程序=新的处理程序(){
@覆盖
公共无效的handleMessage(消息MSG){
pd.dismiss(); tv.setText(OK); }
};}
下面是我的XML code:
<?XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
> < LinearLayout中的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:方向=垂直
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
>
<的TextView
机器人:ID =@ + ID / MAIN1
机器人:layout_width =FILL_PARENT
机器人:layout_height =WRAP_CONTENT
机器人:文字=测试
/> < TableLayout机器人:layout_height =WRAP_CONTENT机器人:ID =@ + ID / tdyn机器人:背景=#F87907的android:layout_width =FILL_PARENT> <&的TableRow GT;
< TextView的机器人:ID =@ + ID /信息
机器人:文字=TEST11
机器人:文字颜色=#000000
机器人:layout_gravity =CENTER_HORIZONTAL/> < TextView的机器人:ID =@ + ID /信息
机器人:文字=TEST12
机器人:文字颜色=#000000
机器人:layout_gravity =CENTER_HORIZONTAL/>
< /&的TableRow GT;
< / TableLayout>
< / LinearLayout中>< / LinearLayout中>
感谢您的帮助。
编辑:您可以找到的logcat的:
12月9日至12日:06:57.973:信息/测试(215):8-1
12月9日至12日:06:57.973:信息/测试(215):8-2
12月9日至12日:06:57.983:信息/测试(215):8-3
12月9日至12日:06:57.993:信息/测试(215):8-4
12月9日至12日:06:58.003:信息/测试(215):8-5
12月9日至12日:06:58.003:信息/测试(215):8-6
12月9日至12日:06:58.012:信息/测试(215):8-7
12月9日至12日:06:58.024:信息/测试(215):8-8
12月9日至12日:06:58.024:信息/测试(215):8-9
12月9日至12日:06:58.044:WARN / dalvikvm(215):主题ID = 15:螺纹未捕获的异常退出(组= 0x4001b188)
12月9日至12日:06:58.044:ERROR / AndroidRuntime(215):未捕获的处理程序:螺纹螺纹8退出,由于未捕获的异常
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):$ android.view.ViewRoot CalledFromWrongThreadException:只有创建视图层次可以触摸其观点原来的线程。
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.ViewRoot.checkThread(ViewRoot.java:2683)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.ViewRoot.requestLayout(ViewRoot.java:557)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.View.requestLayout(View.java:7918)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.widget.TableLayout.requestLayout(TableLayout.java:223)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.ViewGroup.addView(ViewGroup.java:1754)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.widget.TableLayout.addView(TableLayout.java:418)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.ViewGroup.addView(ViewGroup.java:1713)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.widget.TableLayout.addView(TableLayout.java:400)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.view.ViewGroup.addView(ViewGroup.java:1693)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在android.widget.TableLayout.addView(TableLayout.java:391)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在com.sil.DialogActivity.run(DialogActivity.java:65)
12月9日至12日:06:58.073:ERROR / AndroidRuntime(215):在java.lang.Thread.run(Thread.java:1096)
12月9日至12日:06:58.102:信息/流程(52):发送信号。 PID:215 SIG:3
12月9日至12日:06:58.102:信息/ dalvikvm(215):主题ID = 7:反应信号3
12月9日至12日:06:58.102:ERROR / dalvikvm(215):无法打开堆栈跟踪文件'/data/anr/traces.txt':拒绝权限
12月9日至12日:06:58.464:信息/ ARMAssembler(52):产生scanline__00000077:在655949 NS:03515104_00000000_00000000在[0x546d4c 0x546c90] [33 IPP(47项)
12月9日至12日:06:58.663:信息/ ActivityManager(52):显示活动com.sil / .DialogActivity:4121毫秒(共4121毫秒)
请尝试写below.There的code是从当前的code.All一些修改的东西都通过动态$ C $产生仅C。
公共类DialogActivity延伸活动{ 私人的LinearLayout mainLayout;
私人的LinearLayout innerLayout;
私人TextView的电视;
私人ProgressDialog PD;
私人诠释大小= 10;
私人INT增量= 0;
私人TableLayout表;
私人上下文的背景下;
私人的TableRow排; @覆盖
公共无效的onCreate(捆绑冰柱){
super.onCreate(冰柱);
上下文=这一点; mainLayout =新的LinearLayout(背景);
mainLayout.setLayoutParams(新LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
mainLayout.setOrientation(LinearLayout.VERTICAL); innerLayout =新的LinearLayout(背景);
innerLayout.setLayoutParams(新LinearLayout.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
innerLayout.setOrientation(LinearLayout.VERTICAL); 电视=新的TextView(背景);
tv.setText(preSS任意键开始计算); 表=新TableLayout(背景);
table.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
table.setBackgroundColor(Color.parseColor(#F87907)); PD =新ProgressDialog(背景);
pd.setMessage(计算);
pd.setTitle(工作......);
pd.setIndeterminate(真);
pd.setMax(大小);
pd.show(); ThreadRender线程=新ThreadRender();
thread.start(); } 私有类ThreadRender继承Thread { 公共无效的run(){
尺蠖prepare()。
TextView的标签= NULL; 的for(int i = 0; I<大小;我++){
行=新的TableRow(背景);
// row.setId(100 + I);
row.setLayoutParams(新的LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT)); 对于(INT J = 0; J<大小; J ++){
Log.i(测试,
Integer.toString(I)+ - + Integer.toString(j)条);
标签=新的TextView(背景);
label.setText(Integer.toString(I)+ -
+ Integer.toString(J)+);
label.setLayoutParams(新TableRow.LayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
label.setGravity(Gravity.CENTER_VERTICAL);
row.addView(标签);
} 如果(ⅰ%2 == 0){
row.setBackgroundColor(0xff222222);
}其他{
row.setBackgroundColor(0xff000000);
}
table.addView(行);
递增++;
handler.sendEmptyMessage(101);
}
handler.sendEmptyMessage(102);
}
} 私人处理程序处理程序=新的处理程序(){
@覆盖
公共无效的handleMessage(消息MSG){
开关(msg.what){
案例102:
innerLayout.addView(电视);
innerLayout.addView(表);
mainLayout.addView(innerLayout);
的setContentView(mainLayout);
pd.dismiss();
tv.setText(OK);
打破;
案例101:
Log.i(测试,+增量);
pd.setProgress(增量);
打破;
}
}
};
}
I want to create a dynamic table layout in a thread (because I have a huge table and loading is quite long). It seems to work till the fourth row but after an error appears.
I can't find where the issue is. In this java code, the table layout has been simplified to fund the issue.
My java code :
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");
}
};
}
Here is my xml code :
<?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>
Thank you for your help
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)
Please try out the code written below.There is some modification from your current code.All the things are generated dynamically through code only.
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;
}
}
};
}
这篇关于在一个线程创建一个动态tableLayout的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!