安卓是由java引起的。lang.IllegalStateException:指定的子级已具有父级。必须首先对子级的父级调用removeView()
伙计们,我遇到了一个错误,我不知道如何解决它,请帮助我
这是我的密码:
package com.appschool.www.projectphase1516;
import 安卓.app.Activity;
import 安卓.content.Intent;
import 安卓.content.SharedPreferences;
import 安卓.os.Bundle;
import 安卓.view.Menu;
import 安卓.view.MenuItem;
import 安卓.view.View;
import 安卓.view.ViewGroup;
import 安卓.widget.Button;
import 安卓.widget.FrameLayout;
import 安卓.widget.LinearLayout;
import 安卓.widget.Switch;
import 安卓.widget.TextView;
import com.appschool.www.projectphase1516.R;
import org.w3c.dom.Text;
public class AccountlistActivity extends Activity implements View.OnClickListener {
LinearLayout horiz;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_accountlist);
horiz = (LinearLayout) findViewById(R.id.linearlayouthere);
Accountlistcreator();
setContentView(horiz);
}
public void Accountlistcreator(){
SharedPreferences sp = getSharedPreferences("details",1);
int numofaccounts = sp.getInt("numofaccouns",0);
horiz.removeAllViews();
for(int i=0;i<=numofaccounts;i++) {
TextView titleaccount = new TextView(this);
titleaccount.setText(sp.getString("accountTitle" + i, ""));
titleaccount.setTextSize(20);
horiz.addView(titleaccount);
Button seeaccount = new Button(this);
seeaccount.setText("See Details");
seeaccount.setId(i);
seeaccount.setLayoutParams(new FrameLayout.LayoutParams(200,100));
seeaccount.setOnClickListener(this);
horiz.addView(seeaccount);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.accountlist, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(View view) {
SharedPreferences sp = getSharedPreferences("details",1);
int num = sp.getInt("numofaccouns",0);
for(int x=0;x<=num;x++){
if(view.getId()==x){
Intent myaccount = new Intent(AccountlistActivity.this,MyaccountActivity.class);
myaccount.putExtra("numbtn",x);
startActivity(myaccount);
}
}
}
}
下面是我得到的错误:
Process: com.appschool.www.projectphase1516, PID: 2751
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.appschool.www.projectphase1516/com.appschool.www.projectphase1516.AccountlistActivity}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at 安卓.app.ActivityThread.access$800(ActivityThread.java:135)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:136)
at 安卓.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at 安卓.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at 安卓.view.ViewGroup.addView(ViewGroup.java:3415)
at 安卓.view.ViewGroup.addView(ViewGroup.java:3391)
at com.安卓.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
at com.安卓.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
at 安卓.app.Activity.setContentView(Activity.java:1949)
at com.appschool.www.projectphase1516.AccountlistActivity.onCreate(AccountlistActivity.java:29)
at 安卓.app.Activity.performCreate(Activity.java:5231)
at 安卓.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at 安卓.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at 安卓.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at 安卓.app.ActivityThread.access$800(ActivityThread.java:135)
at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at 安卓.os.Handler.dispatchMessage(Handler.java:102)
at 安卓.os.Looper.loop(Looper.java:136)
at 安卓.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.安卓.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.安卓.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
请帮帮我,伙计们
代码应该创建特定数量的按钮和文本视图
每次我去参加那个活动时,我都试图删除并重新创建它们,但我不确定为什么会不断出现错误:[
编辑:
谢谢,它现在运行,但还有一个问题
它不会创建超过1个文本视图和一个按钮,我不知道为什么
(我对java开发相当陌生,所以我不知道上面的评论这样复杂的特性)我只是在做作业:p
# 1 楼答案
第一个答案:
horiz
已经有一个父级(它已经是您设置的contentview的一部分)。删除setContentView(horiz)
回答你的第二个问题:
这意味着您可以使用条目
numofaccouns
获得共享的引用(details
)。然而,那一个并不存在,因为你永远不会保存它。您只会得到一个不存在的条目,如果该条目不存在,则默认为0
。forloop初始化为for (int i = 0; i <= numofaccouns; i++)
,这意味着您总是点击索引i == 0
,因为numofaccouns
总是0
保存所有帐户等等,可以用与上面给出的示例完全相同的方式完成
# 2 楼答案
您的方法是错误的,您希望通过创建多个文本视图来显示信息,从而用大量信息填充布局,您需要的是一个列表视图和一个自定义列表适配器,这样您就可以从共享首选项信息中获取要保存在列表中的信息,然后将其传递给适配器,以便适配器可以构建您想要的所有视图,并比您的方法做得更好、更容易
# 3 楼答案
移除
horiz
已经是活动内容视图的一部分