用户的选择更改背景图片 [英] Change background image on user's choice
问题描述
大家好我希望用户更改所有活动的背景图片在我的用户选择的应用程序。
我能够改变活动的背景图像
从那里我改变形象
但如果我试图改变其它活动
的形象,我收到了 NullPointerException异常
!
是的,我检查了其他活动的布局
的ID!
这是code。
公共类设置延伸活动{
TextView的电视;
复选框CBS,THEME1,THEME2;
RelativeLayout的REL;
OnClickListener checkBoxListener; @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
的setContentView(R.layout.setting); CBS =(复选框)findViewById(R.id.cb);
THEME1 =(复选框)findViewById(R.id.theme1);
THEME2 =(复选框)findViewById(R.id.theme2);
// CBW =(复选框)findViewById(R.id.cbWord preSS);
checkBoxListener =新OnClickListener(){ 公共无效的onClick(视图v){
如果(cbS.isChecked()){
//什么
} 如果(theme2.isChecked()){
RelativeLayout的相对=(RelativeLayout的)findViewById(R.id.rel);
资源解析度= getResources();
可绘制可绘制= res.getDrawable(R.drawable.back_image1);
rel.setBackgroundDrawable(绘制);
// findViewById(R.id.rel).setBackgroundResource(R.drawable.back_image1);
} }
}; cbS.setOnClickListener(checkBoxListener);
theme2.setOnClickListener(checkBoxListener);
// cbW.setOnClickListener(checkBoxListener); }
}
当您使用引用一个布局文件你不能做到这一点.. findViewById()
,android系统查找这在当前的内容查看
而已。 (即你所使用设置视图的setContentView()
当前的活动)。所以obvioulsy该系统正将无法找到您所指定的资源,因此你将只能得到NullPointerExeption。
您必须分别保持基准为背景,并用它在你的其他活动,当你真正传递那里。
Guys I want the user to change the background image of all the activities in my app on user selection.
I am able to change the background image of the Activity
from where am changing the image
but if I try to change the image of other Activity
, I get a NullPointerException
!
Yes, I have checked that the id of other activity's Layout
!
this is the code.
public class setting extends Activity {
TextView tv;
CheckBox cbS, theme1, theme2;
RelativeLayout rel;
OnClickListener checkBoxListener;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setting);
cbS = (CheckBox) findViewById(R.id.cb);
theme1 = (CheckBox) findViewById(R.id.theme1);
theme2 = (CheckBox) findViewById(R.id.theme2);
// cbW=(CheckBox)findViewById(R.id.cbWordPress);
checkBoxListener = new OnClickListener() {
public void onClick(View v) {
if (cbS.isChecked()) {
// anything
}
if (theme2.isChecked()) {
RelativeLayout rel = (RelativeLayout) findViewById(R.id.rel);
Resources res = getResources();
Drawable drawable = res.getDrawable(R.drawable.back_image1);
rel.setBackgroundDrawable(drawable);
// findViewById(R.id.rel).setBackgroundResource(R.drawable.back_image1);
}
}
};
cbS.setOnClickListener(checkBoxListener);
theme2.setOnClickListener(checkBoxListener);
// cbW.setOnClickListener(checkBoxListener);
}
}
You cannot do this.. When you refer a layout file using findViewById()
, the android system looks for this in your current ContentView
only. (i.e the view which you have set using setContentView()
for the current activity). So obvioulsy the sytem will not be able find the resource you are specifying and hence you will get the NullPointerExeption only.
You have to maintain the reference to the backgrounds separately and use it in your other Activity when you actually pass on there.
这篇关于用户的选择更改背景图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!