我有问题。我不明白。我想将动态行添加到tableLayout。我的主要活动类:
public class DutyRoster extends Activity {
/** Called when the activity is first created. */
ArrayList<String> updateList = new ArrayList<String>();
ArrayList<String> totalDate, totalExam = new ArrayList<String>();
String[] temp = null;
webServiceCall wsc = new webServiceCall();
TableLayout tablo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_duty_roster);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.duty_roster, menu);
return true;
}
private void init() {
tablo = (TableLayout)DutyRoster.this.findViewById(R.id.tablo);
final AlertDialog ad = new AlertDialog.Builder(this).create();
Button buton1=(Button)findViewById(R.id.remind);
buton1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (updateList.size() == 0) {
ad.setMessage("None selected!");
} else {
startAnotherActivity();
}
}
});
try {
String resp = wsc.Call();
new SoapCall().execute(resp);
}catch (Exception e) {
ad.setTitle("Error!");
ad.setMessage(e.toString());
ad.show();
}
createDynamicTable(temp);
}
public void startAnotherActivity () {
Intent i = new Intent(this, ScheduleActivity.class);
Bundle b = new Bundle();
b.putStringArrayList("updateList", updateList);
i.putExtra("rowList", b);
startActivity(i);
}
//add dynamic rows
public void createDynamicTable(String[] tableContentList){
for (int i = 0; i < tableContentList.length; i++){
TableRow newLine = new TableRow(this);
newLine.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.WRAP_CONTENT));
TextView rowDate = new TextView(this);
TextView rowName = new TextView(this);
TextView rowPeriod = new TextView(this);
CheckBox checkBox = new CheckBox(this);
checkBox.setOnClickListener(getOnClickDoSomething(checkBox));
rowDate.setText(tableContentList[i]);
rowName.setText(tableContentList[i + 1]);
rowPeriod.setText(tableContentList[i + 2]);
totalDate.add(tableContentList[i]);
totalExam.add(tableContentList[i + 1]);
newLine.addView(checkBox);
newLine.addView(rowDate);
newLine.addView(rowName);
newLine.addView(rowPeriod);
tablo.addView(newLine, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT));
i += 3;
}
}
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
int location = button.getId();
updateList.add(totalDate.get(location) + "/" + totalExam.get(location));
}
};
}
//creating parallel process via AsyncTask structure
private class SoapCall extends AsyncTask<String, String[], String[]> {
private ProgressDialog dialog = new ProgressDialog(DutyRoster.this);
@Override
protected void onPreExecute() {
dialog.setMessage("Loading...");
dialog.show();
}
@Override
protected void onPostExecute(String[] temp) {
dialog.dismiss();
}
@Override
protected String[] doInBackground(String... responses) {
String rsp = responses[0];
String delimiter = "#";
try{
temp = rsp.split(delimiter);
}catch(Exception ex)
{
ex.printStackTrace();
}
return temp;
}
}
}
我收到消息“应用程序Tickler(进程omu.ceng.tickler)已意外停止。请在我运行代码时再次尝试”。
我的布局文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableLayout
android:id="@+id/tablo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="*" >
<TableRow
android:id="@+id/tableRow1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<CheckBox
android:id="@+id/allCheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/date" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name_of_exam" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/period_of_exam" />
</TableRow>
</TableLayout>
</ScrollView>
<Button
android:id="@+id/remind"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remind" />
</LinearLayout>
所以我也有一些日志。
01-18 22:48:26.645: E/AndroidRuntime(717): FATAL EXCEPTION: main
01-18 22:48:26.645: E/AndroidRuntime(717): **java.lang.RuntimeException: Unable to start activity ComponentInfo{omu.ceng.tickler/omu.ceng.tickler.DutyRoster}: java.lang.NullPointerException**
01-18 22:48:26.645: E/AndroidRuntime(717): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.os.Handler.dispatchMessage(Handler.java:99)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.os.Looper.loop(Looper.java:123)
01-18 22:48:26.645: E/AndroidRuntime(717): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-18 22:48:26.645: E/AndroidRuntime(717): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 22:48:26.645: E/AndroidRuntime(717): at java.lang.reflect.Method.invoke(Method.java:507)
01-18 22:48:26.645: E/AndroidRuntime(717): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-18 22:48:26.645: E/AndroidRuntime(717): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-18 22:48:26.645: E/AndroidRuntime(717): at dalvik.system.NativeStart.main(Native Method)
01-18 22:48:26.645: E/AndroidRuntime(717): **Caused by: java.lang.NullPointerException**
01-18 22:48:26.645: E/AndroidRuntime(717): at omu.ceng.tickler.DutyRoster.createDynamicTable(DutyRoster.java:109)
01-18 22:48:26.645: E/AndroidRuntime(717): at omu.ceng.tickler.DutyRoster.init(DutyRoster.java:85)
01-18 22:48:26.645: E/AndroidRuntime(717): at omu.ceng.tickler.DutyRoster.onCreate(DutyRoster.java:38)
我认为线路上的错误原因=&gt; tablo =(TableLayout)DutyRoster.this.findViewById(R.id.tablo); 但我找不到解决方案。 请帮我。我在等你的建议。
答案 0 :(得分:0)
当您在temp
中传递时,字段createDynamicTable(temp)
为空,当您访问tableContentList.length
时,该字段会在方法正文中爆炸。事实是,AsyncTask
在后台生成另一个执行doInBackground()
的线程。在这里,您可以初始化字段temp
,但在createDynamicTable(temp)
之前,您无法指望这一点。
尝试在createDynamicTable(temp)
中调用onPostExecute()
并删除第85行
@Override
protected void onPostExecute(String[] temp) {
dialog.dismiss();
DutyRoster.this.temp = temp;
createDynamicTable(temp);
}
顺便提一下,注意泄漏!