我正在开发一个Android应用程序,用于在SQLite数据库中存储个人信息,将其撤出并在Activity中显示列表。实际上,当用户将数据放入数据库并返回到Activity时,必须刷新页面才能升级包含新数据的列表。我使用语句onCreate(null)
来创建它,但是出现了一个例外:java.lang.IllegalStateException: Already attached
。
以下是代码:
/* MainActivity */
public class MainActivity
extends AppCompatActivity
implements View.OnClickListener{
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==0x1&&resultCode==RESULT_OK){
onCreate(null); // Upgrade the list
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button addItem=(Button)findViewById(R.id.addItem);
addItem.setOnClickListener(this);
.....
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.addItem:
Intent intent=new Intent(this,AddItemActivity.class);
startActivityForResult(intent,0x1);
break;
default:
break;
}
}
}
/*AddItemActivity*/
public class AddItemActivity
extends AppCompatActivity
implements View.OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_item);
Button submit=(Button)findViewById(R.id.submit);//get submit button
submit.setOnClickListener(this);
....
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.submit:
EditText editName=(EditText)findViewById(R.id.editName),
editAge=(EditText)findViewById(R.id.editAge);
insert(new Person(
editName.getText().toString(),
Integer.parseInt(editAge.getText().toString())));
//return
setResult(RESULT_OK,null);
finish();
break;
default:
break;
}
}
}
stackTrace是这样的:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=null} to activity {com.example.sqlitetest/com.example.sqlitetest.MainActivity}: java.lang.IllegalStateException: Already attached
at android.app.ActivityThread.deliverResults(ActivityThread.java:4053)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)
at android.app.ActivityThread.-wrap20(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1516)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: java.lang.IllegalStateException: Already attached
at android.support.v4.app.FragmentManagerImpl.attachController(FragmentManager.java:2871)
at android.support.v4.app.FragmentController.attachHost(FragmentController.java:104)
at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:317)
at android.support.v7.app.AppCompatActivity.onCreate(AppCompatActivity.java:85)
at com.example.sqlitetest.MainActivity.onCreate(MainActivity.java:29)
at com.example.sqlitetest.MainActivity.onActivityResult(MainActivity.java:23)
at android.app.Activity.dispatchActivityResult(Activity.java:6915)