错误无法启动活动和onPause / Resume主题

时间:2013-11-17 06:28:43

标签: android android-intent sharedpreferences

在我的应用程序中。当用户按下按钮(下一个),然后获取secondAc。在此活动中,我想检查edittext中的数据,如果不为null,它将转到saveFile保存文件。然后应用程序关闭并打开agian时用户留在secondAc我想检查onResume查找文件,如果它有我想得到它并祝酒他们 但我有错误告诉“无法开始活动”

这是我在mainActivity中的代码

 package com.example.prtest;

import android.app.Activity;
public class MainActivity extends Activity {


private Button bth;

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

    Button bth = (Button)findViewById(R.id.next);
    bth.setOnClickListener(new View.OnClickListener(){

        @Override
        public void onClick(View v) {
            Intent i = new Intent(MainActivity.this, secondAc.class);
            startActivity(i);   
        }


    });

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

这是我在secondAc中的代码

package com.example.prtest;

import android.app.Activity;
public class secondAc extends Activity {

private SharedPreferences spf;
private EditText edit;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.id.layout2);
    edit = (EditText)findViewById(R.id.edit);

}

@Override
protected void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    getFile();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    String data = edit.getText().toString();
    if(data != null){
        saveFile(data);
    }
}

protected void saveFile(String data){
    spf = this.getSharedPreferences("info", MODE_PRIVATE);
    SharedPreferences.Editor editor = spf.edit();
    editor.putString("data", data);
    editor.clear();
    editor.commit();
}

protected void getFile(){
    spf = this.getSharedPreferences("info", MODE_PRIVATE);
    String getData = spf.getString("data", null);
    if(getData != null){
        Toast.makeText(getApplicationContext(), "mee", Toast.LENGTH_SHORT).show();
    }else{
        Toast.makeText(getApplicationContext(), "maimee", Toast.LENGTH_SHORT).show();
    }


}

}

这是我的logcat错误

11-17 13:12:42.193: E/AndroidRuntime(1166): FATAL EXCEPTION: main
 11-17 13:12:42.193: E/AndroidRuntime(1166): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prtest/com.example.prtest.secondAc}: android.content.res.Resources$NotFoundException: Resource ID #0x7f080002 type #0x12 is not valid
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread.access$600(ActivityThread.java:122)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.os.Handler.dispatchMessage(Handler.java:99)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.os.Looper.loop(Looper.java:137)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread.main(ActivityThread.java:4340)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at java.lang.reflect.Method.invokeNative(Native Method)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at java.lang.reflect.Method.invoke(Method.java:511)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at dalvik.system.NativeStart.main(Native Method)
 11-17 13:12:42.193: E/AndroidRuntime(1166): Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f080002 type #0x12 is not valid
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:2112)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.content.res.Resources.getLayout(Resources.java:858)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.view.LayoutInflater.inflate(LayoutInflater.java:394)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.Activity.setContentView(Activity.java:1835)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at com.example.prtest.secondAc.onCreate(secondAc.java:19)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.Activity.performCreate(Activity.java:4465)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
 11-17 13:12:42.193: E/AndroidRuntime(1166):    ... 11 more

谢谢

1 个答案:

答案 0 :(得分:0)

您想要setContentView(R.id.layout2);而不是setContentView(R.layout.layout2);吗?