简体   繁体   中英

Error Unable to start activity and onPause/Resume subject

in my app .When user press a button(next) ,Then fetch secondAc.In this activity i want to check data in edittext if not null it will go to saveFile to save file.then app is close and open agian when user stay in secondAc i want to check in onResume to find file if it have i want to get it and toast them But i have error it is tell "Unable to start activity"

this is my code in 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;
}

}

this my code in 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();
    }


}

}

this is my logcat error

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

thank you

Instead of setContentView(R.id.layout2); do you want to do setContentView(R.layout.layout2); ?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM