IllegalStateException异常而使用表中的Android
当我执行下面的代码,我得到一个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提前。
在do-while循环中,您将重新添加您已添加到父级的item
和amt
视图。
当您查看发布的部分下方的logcat时,这样的错误很容易诊断。在它下面有一个“引起”异常,指出您的确切代码行。