【问题标题】:Change layout background color on button click单击按钮时更改布局背景颜色
【发布时间】:2015-03-29 10:49:43
【问题描述】:

我在尝试设置布局的背景颜色时遇到错误,我得到一个空指针异常,我不知道为什么。我查看了各种帖子(例如Change background color on button click?),我的代码似乎正确,但似乎仍然没有调用布局。 这是我的活动代码

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"));

还有其他一些变化,但似乎没有任何效果......

(EDIT 2) 我似乎找到了问题所在。这似乎与我试图编辑与其他活动相关的布局有关。任何想法如何解决这个问题?

【问题讨论】:

  • 第 42 行是哪一行?
  • @AlexKl1.setBackgroundResource(R.color.blue);
  • 是的。那就看下面我的回答,没错。
  • 你的 layout_call LinearLayout 在哪里?
  • @Dhina 在另一个名为call.java的活动中

标签: android android-linearlayout background-color


【解决方案1】:

你没有给我们足够的信息给你一个完整的答案,但是如果你给我们的是完整的代码,那么你的错误就在这里:

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);

具体来说,最后两行。确保您的LinearLayout 实际上称为layout_callNullPointerException 表示您可能在名称中输入了一个类型,或者根本没有给它一个名称。

编辑:既然您已经清楚哪一行是错误,我可以确认这是错误。 R.id.layout_call 不存在。

试试这个:

public class Color extends Activity implements OnClickListener { 

private LinearLayout linLay;    

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.color);

    linLay = (LinearLayout) findViewById(R.id.layout_call);

    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:

        linLay.setBackgroundResource(R.color.blue);

【讨论】:

  • 我认为错误在那里,因为应用程序仅在我单击时停止,NullPointerException 可能意味着 l1 为空。 id 已更正。一开始我没有给它,但是在浏览了几篇文章后,我在所有想要更改背景的布局中添加了一个 ID(我有 9 个),我已经尝试了所有这些,但我总是得到相同的结果错误。你还需要我提供什么?
  • @Rasmnev 试试我刚刚添加的内容
  • 我已经尝试过这种方式...同样的错误我什至进行了清理/重建并再次尝试但没有...
  • 并且id不是xml中的layout_call,而是layout_color。我认为 OP 意味着有一些其他布局。将 BG 设置为相对布局可以正常工作,无需编辑。
  • @Rasmnev 共享首选项应该是您的解决方案 IMO。 Oncreate 每个活动检查变量并设置颜色。利大于弊
【解决方案2】:

如果您想更改另一个 Activity 的 UI,请通过 Broadcast 或 EventBus 向其发送消息。像这样

EventBus.getDefault().post(new EventUpdateUI());

【讨论】:

  • 很抱歉,我从未使用过 EventBus,或广播...你能告诉我如何更改,例如,活动call.java 的布局颜色,其布局具有 ID @ 987654324@关联?
【解决方案3】:

共享偏好可能是您的解决方案。在每个活动的创建时检查变量并设置颜色。

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

【讨论】:

    猜你喜欢
    • 2014-10-09
    • 2017-08-02
    • 2014-10-10
    • 1970-01-01
    • 1970-01-01
    • 2019-04-14
    • 2014-10-07
    • 1970-01-01
    相关资源
    最近更新 更多