改变按一下按钮布局的背景颜色 [英] Change layout background color on button click

查看:319
本文介绍了改变按一下按钮布局的背景颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想,当设置一个布局,我得到一个NullPointerException异常的背景颜色,我不知道为什么得到一个错误。我看各个岗位(这样按钮更改背景颜色点击?)和我的code似乎是正确的,但仍是布局似乎并没有被调用。
这是我的活动的code

I'm getting an error when trying to set the background color of a layout I get a nullpointerexception and I don't know why. I've looked at various posts (like this Change background color on button click?) and my code seems correct, but still the layout doesn't seem be called. This is my activity's code

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的误差

and this is the logcat error

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)

我也试图设置背景是这样的:

I have also tried to set the background this way:

findViewById(R.layout.city).setBackgroundColor( android.graphics.Color.parseColor("#0000FF"));

和其他一些变化和似乎没有任何工作...

and a few other variations and nothing seems to work...

(EDIT 2)我似乎已经找到了问题。这似乎与我试图编辑用其它活动相关的布局有关。不知道如何解决?

(EDIT 2) I seem to have found the problem. It seems to be related with me trying to edit layouts associated with other activities. Any Idea how to solve that?

推荐答案

共享preferences可能是您的解决方案。为的OnCreate变量每个活动检查并设置颜色。

Shared preferences could be your solution. Oncreate of every activity check for the variable and set the color.

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

now you can set your layout to bgcolor

这篇关于改变按一下按钮布局的背景颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆