我是开发Android应用的新手,我试图直接从资源设置背景,并且我一直在获得此异常。
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]);
}
});
}
}
问题在于: 的 getWindow()setBackgroundDrawableResource(标签[COUNT]);
答案 0 :(得分:0)
你应该改变这样:
s = "image"+(i+1);
获取资源名称!