无法从自定义活动中提交结果?

时间:2017-02-18 10:53:15

标签: android android-intent android-activity android-sqlite

我现在已经尝试解决这个问题了一段时间,我似乎无法弄清楚我在做什么有什么不对。我试图查找其他人遇到的类似问题,但他们的解决方案似乎无法解决我的问题。 Anywho ...

我收到错误:

  

java.lang.RuntimeException:传递结果失败   ResultInfo {who = null,request = 1327,result = -1,data = Intent {(has   额外的)}}到活动   {thinkgeist.wheresmystuff / thinkgeist.wheresmystuff.MainActivity}:   java.lang.IllegalStateException:尝试重新打开已经关闭的   object:SQLiteQuery:SELECT * FROM items                         在android.app.ActivityThread.deliverResults(ActivityThread.java:4053)                         在android.app.ActivityThread.handleSendResult(ActivityThread.java:4096)                         在android.app.ActivityThread.-wrap20(ActivityThread.java)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1516)                         在android.os.Handler.dispatchMessage(Handler.java:102)                         在android.os.Looper.loop(Looper.java:154)                         在android.app.ActivityThread.main(ActivityThread.java:6077)                         at java.lang.reflect.Method.invoke(Native Method)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                         在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开发。如果有人能指出我正确的方向,那将非常感激。

1 个答案:

答案 0 :(得分:2)

  

java.lang.IllegalStateException:尝试重新打开已经关闭的   object:SQLiteQuery

我想在addItemToDatabase()中你试图对已关闭的数据库执行操作。