Imed Aouidene Imed Aouidene - 3 months ago 10
Android Question

setting backgroud from ressources :Resources$NotFoundException

I am new in developement android apps , I'm trying to set the background directly from resources and I keep getting this Ecxeption.


MainActivity.java


package imed.aouidene.com.wallpaper;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {
Button next;
int count;
int tab[] = new int[3];

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().hide();
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);

count = 0 ;
String s = "" ;

for(int i=0;i<3;i++){
s="drawable/image"+(i+1)+".jpg";
tab[i]=getResources().getIdentifier(s,"drawable",getPackageName());

}

getWindow().setBackgroundDrawableResource(tab[count]);


next = (Button) findViewById(R.id.nextbutton);
next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (count < 2) {
count++;
} else {
count = 0;
}
getWindow().setBackgroundDrawableResource(tab[count]);


}
});


}
}



the problem is at :
getWindow().setBackgroundDrawableResource(tab[count]);

Answer

You should change s like this:

s = "image"+(i+1);

just get the resource name!