尝试设置布局的背景色时出现错误,我得到了nullpointerexception,但我不知道为什么。我看过各种文章(例如单击按钮时是否会改变背景颜色?),我的代码似乎正确,但是似乎仍未调用布局。这是我的活动代码
public class Color extends Activity implements OnClickListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.color);
findViewById(R.id.blue).setOnClickListener(this);
findViewById(R.id.red).setOnClickListener(this);
findViewById(R.id.pink).setOnClickListener(this);
findViewById(R.id.green).setOnClickListener(this);
findViewById(R.id.yellow).setOnClickListener(this);
findViewById(R.id.light_blue).setOnClickListener(this);
findViewById(R.id.button_blue).setOnClickListener(this);
findViewById(R.id.button_red).setOnClickListener(this);
findViewById(R.id.button_pink).setOnClickListener(this);
findViewById(R.id.button_greend).setOnClickListener(this);
findViewById(R.id.button_yellow).setOnClickListener(this);
findViewById(R.id.button_light_blue).setOnClickListener(this);
}
public void onClick(View v){
switch (v.getId()){
case R.id.blue:
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout_call);
l1.setBackgroundResource(R.color.blue);
这是logcat错误
01-30 01:25:06.828: E/AndroidRuntime(4837): FATAL EXCEPTION: main
01-30 01:25:06.828: E/AndroidRuntime(4837): Process: com.example.primeirocasopratico, PID: 4837
01-30 01:25:06.828: E/AndroidRuntime(4837): java.lang.NullPointerException
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.example.primeirocasopratico.Color.onClick(Color.java:42)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.view.View.performClick(View.java:4463)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.view.View$PerformClick.run(View.java:18770)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Handler.handleCallback(Handler.java:808)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Handler.dispatchMessage(Handler.java:103)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.os.Looper.loop(Looper.java:193)
01-30 01:25:06.828: E/AndroidRuntime(4837): at android.app.ActivityThread.main(ActivityThread.java:5327)
01-30 01:25:06.828: E/AndroidRuntime(4837): at java.lang.reflect.Method.invokeNative(Native Method)
01-30 01:25:06.828: E/AndroidRuntime(4837): at java.lang.reflect.Method.invoke(Method.java:515)
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
01-30 01:25:06.828: E/AndroidRuntime(4837): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
01-30 01:25:06.828: E/AndroidRuntime(4837): at dalvik.system.NativeStart.main(Native Method)
我也尝试过这样设置背景:
findViewById(R.layout.city).setBackgroundColor( android.graphics.Color.parseColor("#0000FF"));
和其他一些变体,似乎没有任何效果...
(编辑2)我似乎已经找到了问题。这似乎与我尝试编辑与其他活动相关的布局有关。任何想法如何解决?
共享的首选项可能是您的解决方案。在每个活动的oncreate中,检查变量并设置颜色。
public void onClick(View v){
switch (v.getId()){
case R.id.blue:
backgroundColor("blue");
break;
}
}
private void backgroundColor(String color) {
// TODO Auto-generated method stub
SharedPreferences prefs = getSharedPreferences("BackgroundColor", MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.clear();
editor.putString("Color", color);
editor.commit();
}
在其他活动中
SharedPreferences prefs = getSharedPreferences("BackgroundColor",
MODE_PRIVATE);
String bgcolor = prefs.getString("Color","Anydefaultcolor");
现在您可以将布局设置为bgcolor
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句