我一直在嘗試解決這一段時間,我似乎無法弄清楚我在做什麼錯。我試圖查看其他人遇到的類似問題,但他們的解決方案似乎無法解決我看來的問題。 Anywho ...自定義活動的結果失敗?
,我發現了錯誤:
java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1327, result=-1, data=Intent { (has extras) }} to activity {thinkgeist.wheresmystuff/thinkgeist.wheresmystuff.MainActivity}: java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteQuery: SELECT * FROM items 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)
當試圖從我創建了名爲CreateActivity方法返回。下面是它是如何從 MainActivity稱爲:
public void createItem(){
Intent intent = new Intent(this, CreateActivity.class);
startActivityForResult(intent, CREATE_ACTIVITY);
}
這個調用的代碼後,我們就跳到 CreateActivity:
public class CreateActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
final EditText etName = (EditText) findViewById(R.id.etName);
final EditText etDescription = (EditText) findViewById(R.id.etDescription);
final EditText etSerial = (EditText) findViewById(R.id.etSerial);
final EditText etLocation = (EditText) findViewById(R.id.etLocation);
final EditText etQuantity = (EditText) findViewById(R.id.etQuantity);
Button submit = (Button) findViewById(R.id.buttonSubmit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = etName.getText().toString();
String description = etDescription.getText().toString();
String serial = etSerial.getText().toString();
String location = etLocation.getText().toString();
int quantity = Integer.parseInt(etQuantity.getText().toString());
Item item = new Item(1, name, description, serial, location, quantity);
Intent resultIntent = new Intent();
resultIntent.putExtra("name", name);
resultIntent.putExtra("description", description);
resultIntent.putExtra("serial", serial);
resultIntent.putExtra("location", location);
resultIntent.putExtra("quantity", quantity);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
});
}
最後的代碼MainActivity處理該響應...
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
// Extract data from Create Activity
if(requestCode == CREATE_ACTIVITY && resultCode==RESULT_OK && data != null) {
String name = data.getStringExtra("name");
String description = data.getStringExtra("description");
String serial = data.getStringExtra("serial");
String location = data.getStringExtra("location");
int quantity = data.getIntExtra("quantity", 1);
addItemToDatabase(name, description, serial, location, quantity);
}
}
老實說,我認爲這只是我在某處使用一些不正確的語法,因爲我還不完全熟悉android開發。如果任何人都可以指出我正確的方向,將不勝感激。
是的,好像是這樣,請將addItemToDatabase()函數代碼包含到您的問題中。 – shtolik