當我執行下面的代碼,我得到一個IllegalStateException ..IllegalStateException異常而使用表中的Android
public class DatabaseView extends Activity{
TextView tv;
MySQLiteHelper h;
TableLayout main;
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newview);
h=new MySQLiteHelper(this);
main=(TableLayout) findViewById(R.id.tb1);
table();
}
private void table() {
// TODO Auto-generated method stub
TableRow header_row=new TableRow(this);
header_row.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
TextView item=new TextView(this);
item.setText("Item");
header_row.addView(item);
TextView amt=new TextView(this);
amt.setText("Amount");
header_row.addView(amt);
main.addView(header_row, new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
Cursor c=h.getAllEntry();
c.moveToFirst();
do
{
TableRow tr=new TableRow(this);
tr.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
TextView tv=new TextView(this);
Log.d(" ", c.getString(2));
tv.setText(c.getString(2));
tr.addView(item);
TextView amnt=new TextView(this);
Log.d(" ", c.getString(1));
amnt.setText(c.getString(1));
tr.addView(amt);
tr.addView(tv);
tr.addView(amnt);
main.addView(tr,new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
c.moveToNext();
}while(!c.isAfterLast());
}
錯誤日誌is--
AndroidRuntime(1692):致命異常:main AndroidRuntime(1692):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.example.expensesdatabase/com.example.expensesdatabase.DatabaseView}:> java.lang.IllegalStateException:指定的子項已經有一位家長。您必須先調用子對象的父對象的removeView()。 AndroidRuntime(1692):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211) AndroidRuntime(1692):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) AndroidRuntime(1692):at android .app.ActivityThread.access $ 600(ActivityThread.java:141) AndroidRuntime(1692):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1256) AndroidRuntime(1692):at android.os.Handler。 AndroidRuntime(1692):在android.os.Looper.loop(Looper.java:137) AndroidRuntime(1692):at android.app.ActivityThread.main(ActivityThread.java:5103) AndroidRuntime(1692):在java.lang.reflect.Method.invokeNative(Native Method) AndroidRuntime(1692):at java.lang.re flect.Method.invoke(Method.java:525)
請告訴爲什麼這個錯誤發生和解決same.Thanks提前。