伙计我收到错误,我不知道如何解决,请帮助我。
这是我的代码:
package com.appschool.www.projectphase1516;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.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 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.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 android.view.ViewGroup.addViewInner(ViewGroup.java:3562)
at android.view.ViewGroup.addView(ViewGroup.java:3415)
at android.view.ViewGroup.addView(ViewGroup.java:3391)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:299)
at android.app.Activity.setContentView(Activity.java:1949)
at com.appschool.www.projectphase1516.AccountlistActivity.onCreate(AccountlistActivity.java:29)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.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.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
请帮帮我们!
代码应该创建特定数量的按钮和文本视图。
我每次去那个活动时都试图删除并重新创建所有这些,但我不确定为什么我会一直收到错误:[
修改
感谢它现在运行,但还有另一个问题
它不会创建超过1个textview和一个按钮,我不知道为什么
(我对java开发很新,所以我不知道像我上面的评论这样的复杂功能)我只是在做作业:P
答案 0 :(得分:0)
删除
setContentView(horiz);
horiz
已经是活动内容视图的一部分。
答案 1 :(得分:0)
您的方法是错误的,您希望通过创建多个文本视图来显示信息来填充您的布局,您需要的是列表视图和自定义列表适配器,这样您就可以从共享的首选项信息中获取信息将保存在列表中,然后将其传递给适配器,以便适配器可以构建您想要的所有视图,并且比您的方法更好,更轻松地完成它。
答案 2 :(得分:0)
第一个答案:horiz
已经有一个父母(它已经是你设置的内容视图的一部分)。删除setContentView(horiz)
回答你的第二个问题:
SharedPreferences sp = getSharedPreferences("details",1);
int numofaccounts = sp.getInt("numofaccouns",0);
这意味着您可以使用条目details
获得共享偏好(numofaccouns
)。然而,那个不存在,因为你永远不会保存它。您只获得一个不存在的条目,如果该条目不存在,则将其默认为0
。 forloop初始化为for (int i = 0; i <= numofaccouns; i++)
,这意味着你总是点击索引i == 0
,因为numofaccouns
总是0
final String SHARED_PREFS = "details";
final String NUMBER_OF_ACCOUNTS = "numberOfAccounts";
final int NUMBER_OF_ACCOUNTS_DEFAULT = 10;
int numberOfAccounts = 75;
//to save into sharedpreferences:
getSharedPreferences(SHARED_PREFS, MODE_PRIVATE).edit().put(NUMBER_OF_ACCOUNTS, numberOfAccounts).commit();
//to retrieve that value:
numberOfAccounts = getSharedPreferences(SHARED_PREFS, MODE_PRIVATE).getInt(NUMBER_OF_ACCOUNTS, NUMBER_OF_ACCOUNTS_DEFAULT);
保存所有帐户等等,可以采用与上面给出的示例完全相同的方式完成。