锁定屏幕时,Android中出现资源错误

时间:2016-06-07 17:45:58

标签: java android eclipse android-intent

 Process: com.example.in.androidwithlogin, PID: 9993
Java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.in.androidwithlogin/com.example.in.androidwithlogin.LoginActivity}: android.content.res.Resources$NotFoundException: Resource ID
    #0x7f04001b
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
    at a
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
    at android.app.ActivityThread.main(ActivityThread.java:5584) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:515) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
    at dalvik.system.NativeStart.main(Native Method)                                                                                           

我有5个活动的横向应用程序。今天它开始出现这个错误。我用设置id动态制作了背景。这可能是问题

public class LoginActivity extends Activity {

    String TAG = "Chic1",
   username,password;

    private Button btn_login;
    private TextView txt_username,txt_password;//,txt_attempts_left;

    int counter = 3;

    MediaPlayer mMediaPlayer;

    CheckBox chckbxLoginRemember;

    boolean isChecked;//for checkbox

    Login login;

    DatabaseHandler dbHandler;

    boolean wasDbExisted;//maine yollamak lazim cünlü orda da kontrol var, burda exists bakınca yeniyse artik orda exist bakinca yeni olmayacagi icin

    @Override
    protected void onPause() {

        if(mMediaPlayer.isPlaying()&&mMediaPlayer!=null ){
            Log.d(TAG, "loginactivityonpause mediaisplay not null isplaying");
            mMediaPlayer.pause();
        }
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "loginactivityondestry");
        super.onDestroy();

        if(mMediaPlayer!=null ) {
            Log.d(TAG, "loginactivityondestry mediaisplay not null");
            mMediaPlayer.release();
        }
        else{
            Log.d(TAG, "loginactivityondestry mediaisplay null");//// TODO: 6/2/2016 sil test icin
        }

    }

    @Override
    protected void onStop() {
        Log.d(TAG, "loginactivityonstop");
        super.onStop();

        if(mMediaPlayer!=null ) {
            Log.d(TAG, "loginactivityonstop mediaisplay not null");
            mMediaPlayer.stop();
            //  mMediaPlayer.release();
        }
        else{
            Log.d(TAG, "loginactivityonstop mediaisplayisplaying"+mMediaPlayer.isPlaying());//// TODO: 6/2/2016 sil test icin
        }
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "loginactivityonresume");
        super.onResume();

        mMediaPlayer = MediaPlayer.create(this, R.raw.sound_loginactivity);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.start();//.release();// STOPSHIP: 6/2/2016
    }

    @Override
    protected void onRestart() {
        Log.d(TAG, "loginactivityonrestart");
        super.onRestart();
    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        Log.d(TAG,"*****************************");
        Log.d(TAG,"*****************************");
        Log.d(TAG,"*****************************");
        Log.d(TAG,"");
        Log.d(TAG, "login activity OnCreate************");

        super.onCreate(savedInstanceState);
        dbHandler = new DatabaseHandler(this);//islemler icin gerekiyor onceden almak




        if (wasDbExisted &&
                dbHandler.isLoginRemember()!=null) {//db varsa ve reember seciliyse maine git.
            Log.d(TAG, "login activity butonlogin on click remembertrue isLoginRemember=" + dbHandler.isLoginRemember()
                    +"dbexists"+dbHandler.ifDatabaseExists());//todo sil hepsini

            Intent i = new Intent(LoginActivity.this, MainActivity.class);
            i.putExtra("username", dbHandler.isLoginRemember());
            i.putExtra("wasDbExisted", wasDbExisted);

            // intent.putExtra("MESSAGE", "You have been successfully Registered");
            startActivity(i);
            finish();
        }
        else {

            if(   dbHandler.isLoginRemember()!=null){

                Intent i = new Intent(LoginActivity.this, MainActivity.class);
                i.putExtra("username", dbHandler.isLoginRemember());
                i.putExtra("wasDbExisted", wasDbExisted);
                startActivity(i);
                finish();
            }
            else{


            setContentView(R.layout.activity_login);
                RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_login);
                int pic = R.drawable.img_loginactivity_background;
                relativeLayout.setBackgroundResource(pic);

        mMediaPlayer = new MediaPlayer();

        btn_login = (Button) findViewById(R.id.button_login);

        login = new Login();

        txt_username = (TextView) findViewById(R.id.editText_username);
        txt_password = (TextView) findViewById(R.id.editText_password);
       // txt_attempts_left = (TextView) findViewById(R.id.text_attempts_left);

        chckbxLoginRemember = (CheckBox) findViewById(R.id.chckbox_loginremember);

        btn_login.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d(TAG, "login activity butonlogin on click");

                username = txt_username.getText().toString();
                password = txt_password.getText().toString();
//                txt_attempts_left.setBackgroundColor(Color.YELLOW);
                isChecked = chckbxLoginRemember.isChecked();

                if (!isChecked) {
                    login.setLoginRemember(0);
                    Log.d(TAG, "login activity butonlogin on click rememberfalse");//// TODO: 6/2/2016 gecicisil
                } else {
                    login.setLoginRemember(1);
                    Log.d(TAG, "login activity butonlogin on click remembertrue");
                }

                login.setLoginName(username);
                login.setLoginPassword(password);

                switch (dbHandler.isLoginSuccessful(login)) {// 3 seçebek var
                    //2= success
                    //1= wrong password
                    //0= no data go online then add to local if success

                    case 0:
                        if (!CheckNetwork.isOnline(LoginActivity.this)) {

                            CheckNetwork.showNoConnectionDialog(LoginActivity.this, true, 0);
                            Log.d(TAG, "LoginActivity isnotonline while try wait error");

                        } else {
                            counter--;
                            new SigninActivity(LoginActivity.this, login,wasDbExisted).execute();//username postexecuteta lazim
                        }

                      //  txt_attempts_left.setText(Integer.toString(counter));

                        if (counter == 0) {//todo azaltiyor sonra ekrana yansitiyor kalani. orda azaldiktan sonra async bittigi icin oyle kalyır bir süre
                            btn_login.setEnabled(false);
                        }

                        break;

                    case 1:
                        Log.d(TAG, "login activity case 1 result unsuccess");
                        Toast.makeText(getApplicationContext(), "Giris islemi basarisiz, yanlis sifre, lutfen tekrar deneyiniz...", Toast.LENGTH_LONG).show();

                        counter--;
                      //  txt_attempts_left.setText(Integer.toString(counter));

                        if (counter == 0) {//todo azaltiyor sonra ekrana yansitiyor kalani. orda azaldiktan sonra async bittigi icin oyle kalyır bir süre
                            btn_login.setEnabled(false);
                        }

                        break;

                    case 2://bourca counterla ugrasmadim
                        Log.d(TAG, "login activity onpostexe result success");
                        Toast.makeText(getApplicationContext(), "Giris islemi basarili!", Toast.LENGTH_LONG).show();
                        login.toString();//// TODO: 6/2/2016 geçici sil

                        if(isChecked){
                            dbHandler.rememberLogin(username);//gerekiyor cünkü sonradan hatirla deyince hatirlamiyor.sadeee asyncde yaratirken oluyort
                        }

                        Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                        intent.putExtra("username", username);
                        intent.putExtra("wasDbExisted", wasDbExisted);

                        // intent.putExtra("MESSAGE", "You have been successfully Registered");
                        startActivity(intent);
                        finish();
                        break;
                }
            }
        });
        Log.d(TAG, "login activity oncreate end*********");
    } }
    }

}

class SigninActivity  extends AsyncTask<String,Void,String> {
    ProgressDialog dialog;
    private Context context;
    String TAG = "Chic";
    private String username, password;

    DatabaseHandler dbHandler;

    Login login;

    boolean wasDbExisted;

    int loginRemember;

    public SigninActivity(Context context, Login login, boolean wasDbExisted) {
        Log.d(TAG, "login activity signinactivity consturct");

        this.login = new Login();

        this.context = context;
       // this.username = username;
        this.login = login;
        this.wasDbExisted = wasDbExisted;

        username = this.login.getLoginName();
        password = this.login.getLoginPassword();
        loginRemember = this.login.getLoginRemember();
        this.login.toString();//// TODO: 6/2/2016 geçici sil
    }

    protected void onPreExecute(){
        Log.d(TAG, "login activity signinactivity onpre");
        dialog = ProgressDialog.show(context, "Lutfen bekleyiniz...", null, true, true);
    }

    @Override
    protected String doInBackground(String... arg0) {
        Log.d(TAG, "login activity signinactivity doin");

      //  Log.d(TAG, "login activity signinactivity consturct try");
     //   String username = (String) arg0[0];
       // String password = (String) arg0[0];

        Log.d(TAG, "login activity signinactivity username: " + username + " password: " + password);

        String uri = "http://oep.esy.es/login.php?" + "username=" + username + "&password=" + password;
        StringBuilder sb = new StringBuilder();

        try {
            BufferedReader bufferedReader = null;
            URL url = new URL(uri);
            Log.d(TAG, "login activity url " + url);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();

        bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
        //  Log.d(TAG," bufferedReader "+ bufferedReader);
        String json;

            while ((json = bufferedReader.readLine()) != null) {
                sb.append(json + "\n");
     //           Log.d(TAG, "login activity sb " + sb.toString());
                Log.d(TAG, "login activity sb: " + sb);
            }
        } catch (IOException e) {
            Log.d(TAG, "login activity hata doin");
            return new String("Exception: " + e.getMessage());
        }//// TODO: 6/1/2016 internet filan gitmsse exception ile bir seyler yapilmali
        return sb.toString().trim();
    }

        @Override
    protected void onPostExecute(String result){
            dialog.dismiss();
            Log.d(TAG, "login activity onpostexe, result='"+result+"'");

            if(result.equals("success")){
                Log.d(TAG, "login activity onpostexe result success");
                Toast.makeText(context,"Giris islemi basarili!", Toast.LENGTH_LONG).show();

                //sadece successa koyuyorum locale, yanlissa kontrol yok oyle bir username var mi yok mu
                //zaten intenrete baglanmissa localde kayitli degildir. o yüzdfen update yapmyorum add yapiyorum
                dbHandler = new DatabaseHandler(context);
                login.toString();//// TODO: 6/2/2016 geçici sil
                dbHandler.addLogin(login);
dbHandler.getAllLogin();//// TODO: 6/2/2016 geçici sil

                Intent intent = new Intent(context, MainActivity.class);
                intent.putExtra("username", username);
                intent.putExtra("wasDbExisted", wasDbExisted);
               // intent.putExtra("MESSAGE", "You have been successfully Registered");
                context.startActivity(intent);
                ((Activity) context).finish();
            }
            else{
                Log.d(TAG, "login activity onpostexe result unsuccess");
                Toast.makeText(context,"Giris islemi basarisiz oldu, lutfen tekrar deneyiniz...", Toast.LENGTH_LONG).show();
                //// TODO: 6/2/2016 counter 0sa tekrar denyiniz olmaz da bosvwer.
            }
    }//postexecute end
}//async end

这是布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/relative_layout_login"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".LoginActivity">

<!--    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"-->


    <TextView android:text="EVINE HOS GELDIN!" android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/text_login"
        android:textSize="40dp"
        android:layout_marginTop="77dp"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:textColor="#ea4c0e" />



    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/editText_username"
        android:hint="Kullanici adi giriniz..."
        android:focusable="true"
        android:textColorHighlight="#ff7eff15"
        android:textColorHint="#ffff25e6"
android:layout_alignLeft="@id/text_login"
android:layout_alignStart="@id/text_login"


        android:layout_centerVertical="true"
        android:layout_alignRight="@+id/text_login"
        android:layout_alignEnd="@+id/text_login" />

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:inputType="textPassword"

        android:ems="10"
        android:id="@+id/editText_password"
        android:layout_below="@+id/editText_username"

        android:layout_alignEnd="@id/editText_username"

        android:textColorHint="#ffff299f"
        android:hint="Sifreyi giriniz..."
        android:layout_alignLeft="@+id/editText_username"
        android:layout_alignStart="@+id/editText_username"
        android:layout_alignRight="@id/editText_username" />

    <CheckBox
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Sifreyi hatirla?"
        android:id="@+id/chckbox_loginremember"
        android:layout_alignLeft="@+id/text_login"
        android:layout_alignStart="@+id/text_login"
        android:layout_below="@+id/editText_password"
        android:textColor="#171515"
        android:shadowColor="#08b62e"
        android:checked="false" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Giris yap"
        android:id="@+id/button_login"
        android:textColor="#2876dc"
        android:layout_below="@+id/editText_password"
        android:layout_alignRight="@+id/editText_password"
        android:layout_alignEnd="@+id/editText_password" />



</RelativeLayout>




SHORTLY

我没有把图像放在那里。我在相对布局中动态创建

setContentView(R.layout.activity_login);
                RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_login);
                int pic = R.drawable.img_loginactivity_background;
                relativeLayout.setBackgroundResource(pic);

当我锁定screeen时,它会出错。在这种行为之后,在主要活动中同样发生了。

202行,其中setContentView(R.layout.activity_login);

我根据答案改为代码,但没有工作:

@Override
    protected void onPause() {
        Log.d(TAG, "loginactivityonpause***********");


        if(mMediaPlayer.isPlaying()&&mMediaPlayer!=null ){
            Log.d(TAG, "loginactivityonpause mediaisplay not null isplaying");
            mMediaPlayer.pause();
        }

        Log.d(TAG, "loginactivityonpause ends***********");

        super.onPause();
    }

    @Override
    protected void onDestroy() {
        Log.d(TAG, "loginactivityondestry");

        if(mMediaPlayer!=null ) {
            Log.d(TAG, "loginactivityondestry mediaisplay not null");
            mMediaPlayer.release();
        }
        else{
            Log.d(TAG, "loginactivityondestry mediaisplay null");//// TODO: 6/2/2016 sil test icin
        }
        super.onDestroy();
    }

    @Override
    protected void onStop() {
        Log.d(TAG, "loginactivityonstop");

        if(mMediaPlayer!=null ) {
            Log.d(TAG, "loginactivityonstop mediaisplay not null");
            mMediaPlayer.stop();
            //  mMediaPlayer.release();
        }
        else{
            Log.d(TAG, "loginactivityonstop mediaisplayisplaying"+mMediaPlayer.isPlaying());//// TODO: 6/2/2016 sil test icin
        }
        super.onStop();
    }

    @Override
    protected void onResume() {
        Log.d(TAG, "loginactivityonresume");
        super.onResume();

        mMediaPlayer = MediaPlayer.create(this, R.raw.sound_loginactivity);
        mMediaPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
        mMediaPlayer.setLooping(true);
        mMediaPlayer.start();//.release();// STOPSHIP: 6/2/2016
    }

    @Override
    protected void onRestart() {
        Log.d(TAG, "loginactivityonrestart");
        super.onRestart();
    }


PID: 27578
                                                                                         java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.caneraydin.androidwithlogin/com.example.caneraydin.androidwithlogin.LoginActivity}: android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2429)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493)
                                                                                             at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4014)
                                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:166)
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5584)
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
                                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                                          Caused by: android.content.res.Resources$NotFoundException: Resource ID #0x7f04001b
                                                                                             at android.content.res.Resources.getValue(Resources.java:2327)
                                                                                             at android.content.res.Resources.loadXmlResourceParser(Resources.java:3692)
                                                                                             at android.content.res.Resources.getLayout(Resources.java:2143)
                                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
                                                                                             at android.view.LayoutInflater.inflate(LayoutInflater.java:354)
                                                                                             at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:331)
                                                                                             at android.app.Activity.setContentView(Activity.java:2018)
                                                                                             at com.example.caneraydin.androidwithlogin.LoginActivity.onCreate(LoginActivity.java:203)
                                                                                             at android.app.Activity.performCreate(Activity.java:5447)
                                                                                             at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
                                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2393)
                                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2493) 
                                                                                             at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:4014) 
                                                                                             at android.app.ActivityThread.access$900(ActivityThread.java:166) 
                                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289) 
                                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                                             at android.app.ActivityThread.main(ActivityThread.java:5584) 
                                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268) 
                                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084) 
                                                                                             at dalvik.system.NativeStart.main(Native Method) 
06-07 21:27:55.111 760-760/? E/KeyguardHostView: Error when trying to bind default AppWidget: java.lang.IllegalArgumentException: not a appwidget provider: ComponentInfo{com.sec.android.app.keyguard/com.sec.android.app.keyguard.KeyguardClockWidgetProvider}
06-07 21:27:55.161 760-760/? E/LSO: LSO Service is not yet ready!!!
06-07 21:27:55.211 760-760/? E/KeyguardHostView: KeyguardHostView()
06-07 21:27:55.251 760-760/? E/KeyguardHostView: Error when trying to bind default AppWidget: java.lang.IllegalArgumentException: not a appwidget provider: ComponentInfo{com.sec.android.app.keyguard/com.sec.android.app.keyguard.KeyguardClockWidgetProvider}
06-07 21:27:55.291 760-760/? E/LSO: LSO Service is not yet ready!!!
06-07 21:27:55.511 591-772/? E/lights: write_int failed to open -1
06-07 21:27:56.042 2300-2300/? E/accuweather: [MSC_Daemon]>>> WU:1591 [0:0] [NameNotFoundException] !!
06-07 21:27:57.683 27662-27662/? A/Adjust: PRODUCTION: Adjust is running in Production mode. Use this setting only for the build that you want to publish. Set the environment to `sandbox` if you want to test your app!
06-07 21:27:58.374 27662-27662/? E/dalvikvm: Could not find class 'android.media.VolumeProvider', referenced from method uv.d
06-07 21:27:58.544 591-772/? E/lights: write_int failed to open -1
06-07 21:27:58.584 158-401/? E/cutils: Failed to mkdirat(/storage/extSdCard/Android): Read-only file system
06-07 21:27:58.985 27662-27662/? E/dalvikvm: Could not find class 'com.amazon.device.messaging.ADM', referenced from method com.appboy.Appboy.<init>
06-07 21:28:07.283 591-855/? E/Watchdog: !@Sync 2814

再次同一行,

 setContentView(R.layout.activity_login);

我在登录时锁定了屏幕,当我启用时,应用程序消失了:(

1 个答案:

答案 0 :(得分:0)

将onDestroy()更改为:

    @Override
    protected void onDestroy() {
      Log.d(TAG, "loginactivityondestry");

      if(mMediaPlayer!=null ) {
        Log.d(TAG, "loginactivityondestry mediaisplay not null");
        mMediaPlayer.release();
      }
      else{
        Log.d(TAG, "loginactivityondestry mediaisplay null");//// TODO: 6/2/2016 sil test icin
      }
      super.onDestroy();

   }

在你做完最后的事情之前,你不想打电话给超级onDestroy。