这里我引用了代码link
我必须添加多个textviews @ runtime并且可能它的工作但是textview没有显示,我必须在表格布局中添加表格行并显示textview。但它不起作用。
这是我的代码:
tx[i] = new TextView(S1.this);
tx[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tx[i].setText(name);
tr[i] = new TableRow(S1.this);
tr[i].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
rl.addView(tr[i]); // Exception
此处抛出异常,app停止工作。
这是我的logcat:
04-08 02:13:05.255: E/AndroidRuntime(1716): FATAL EXCEPTION: AsyncTask #1
04-08 02:13:05.255: E/AndroidRuntime(1716): Process: com.app.crossdine, PID: 1716
04-08 02:13:05.255: E/AndroidRuntime(1716): java.lang.RuntimeException: An error occured while executing doInBackground()
04-08 02:13:05.255: E/AndroidRuntime(1716): at android.os.AsyncTask$3.done(AsyncTask.java:300)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
04-08 02:13:05.255: E/AndroidRuntime(1716): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.lang.Thread.run(Thread.java:841)
04-08 02:13:05.255: E/AndroidRuntime(1716): Caused by: java.lang.NullPointerException
04-08 02:13:05.255: E/AndroidRuntime(1716): at com.app.crossdine.S1$Getitems.doInBackground(S1.java:155)
04-08 02:13:05.255: E/AndroidRuntime(1716): at com.app.crossdine.S1$Getitems.doInBackground(S1.java:1)
04-08 02:13:05.255: E/AndroidRuntime(1716): at android.os.AsyncTask$2.call(AsyncTask.java:288)
04-08 02:13:05.255: E/AndroidRuntime(1716): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
请指导我。
答案 0 :(得分:1)
借助问题中提供的链接以及以下link,您可以像下面一样使用它(确保rl
正确指向您要添加此内容的布局/视图表格)。
更新:因为您使用的是AsyncTask,所以无法修改此方法中的UI内容。我在onPostExecute方法中做了。此外,您应该使用TableRow.LayoutParams
设置TextView的布局参数。我的工作代码如下
首先将以下内容添加到我的xml布局中的按钮
android:onClick="addTable"
然后添加以下java活动
public void addTable(View view){
CreateTableTask mTask = new CreateTableTask();
mTask.execute();
}
public class CreateTableTask extends AsyncTask<Void, Void, TableLayout> {
@Override
protected TableLayout doInBackground(Void... params) {
LayoutParams params1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT);
TableLayout layoutINNER = new TableLayout(MainActivity.this);
layoutINNER.setLayoutParams(params1);
TextView[] tx = new TextView[10];
TableRow[] tr = new TableRow[10];
for(int i=0; i<10; i++) {
tx[i] = new TextView(MainActivity.this);
tx[i].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tx[i].setText("Data");
tr[i] = new TableRow(MainActivity.this);
tr[i].setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
tr[i].addView(tx[i]);
// and then adding table row in tablelayout
layoutINNER.addView(tr[i]);
}
return layoutINNER;
}
@Override
protected void onPostExecute(final TableLayout newTable) {
if (newTable!=null) {
// this rl is the layout where I want this table
View rl= findViewById(R.id.mainlayout);
((ViewGroup) rl).addView(newTable);
}
}
}