0

主要 Java 活动:

TList tl = new TList();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
}

public void AddmyCostumObj_buttonclik(View view) {

    Intent intent = new Intent(this, AddActivity.class);
    startActivityForResult(intent, 0);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(resultCode == RESULT_OK){
        Bundle T = data.getExtras();
        String new_myCostumObj_s = myCostumObj.myCostumObj_getString("ret");
        myCostumObj new1 = new myCostumObj();
        new1.string_to_myCostumObj(new_myCostumObj_s);
        tl.add_myCostumObj(new1);
    }
}

@Override
protected void onStart() {
    super.onStart();

    FileInputStream fis;
    try {
        fis = openFileInput("data_tasks");

        ObjectInputStream ois = new ObjectInputStream(fis);
        tl = (TList) ois.readObject();
        if (tl == null) tl = new TList(); // it sometimes gave nullPointer

        ois.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }
}

@Override
protected void onPause() {
    super.onPause();

    FileOutputStream fos;
    try {
        fos = openFileOutput("data_tasks", Context.MODE_PRIVATE);

        ObjectOutputStream oos = new ObjectOutputStream(fos);
        oos.writeObject(tl);
        oos.flush();
        oos.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }catch(IOException e){
        e.printStackTrace();
    }
}

}

添加 Java 活动:

// creates a new myCostumObj
public void add_buttonclick(View view){

    Intent intent = new Intent();
    Bundle T = new Bundle();
    T.putString("ret", myCostumObj.myCostumObj_toString());
    intent.putExtras(T);
    setResult(RESULT_OK, intent);
    finish();
}

重新启动时主要活动首先做什么?它是否做了预期的事情并从文件中读取 tl 并从包中添加 myCostumObj?如果是这样,那我做错了什么,因为 tl 总是显示为空。

4

0 回答 0