简体   繁体   English

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

[英]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" 在我的应用程序中。当用户按下按钮(下一个)时,然后获取secondAc。在此活动中,我要检查edittext中的数据(如果不为null),将转到saveFile保存文件。然后应用程序关闭并在用户停留时打开agian secondAc我想签入onResume来查找文件,如果它有我想要获取它并敬酒他们,但是我有错误,它被告知“无法启动活动”

this is my code in mainActivity 这是我在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 这是我在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 这是我的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

thank you 谢谢

Instead of setContentView(R.id.layout2); 代替setContentView(R.id.layout2); do you want to do setContentView(R.layout.layout2); 你想做setContentView(R.layout.layout2); ?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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