2012-10-28 45 views
1

我想写一个程序,将屏幕的背景颜色更改为我决定的颜色。 我写了这样的事情,但是当它运行它崩溃 是什么STHE问题,请大家帮忙me.here是XML代码改变布局颜色android应用

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" > 

    <LinearLayout 

     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal" 
     android:layout_gravity="bottom" 
     android:background="#FFFFFF" 


     > 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Red" 
      /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Green" /> 

     <Button 
      android:id="@+id/button3" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="Blue" /> 
     <Button 
      android:id="@+id/button4" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="White" /> 

    </LinearLayout> 

</LinearLayout> 

而这里的.java代码

package com.example.flashlight; 

import android.os.Bundle; 
import android.app.Activity; 
import android.graphics.Color; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 

public class FlashLight extends Activity { 

Button red,green,blue,white; 
LinearLayout view; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_flash_light); 
    red=(Button) findViewById(R.id.button1); 
    green=(Button) findViewById(R.id.button2); 
    blue=(Button) findViewById(R.id.button3); 
    white=(Button) findViewById(R.id.button4); 

    red.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 

      view.setBackgroundColor(Color.RED); 

     } 
    }); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_flash_light, menu); 
    return true; 
} 
} 

回答

0

你不分配view变量来什么,这可能是在你的代码

尝试在XML文件中加入这一行造成NullPointerExceptionLinearLayout

android:id="@+id/view"

,加入这行到你的onCreate

view = (LinearLayout)findViewBiId(R.id.view);

+0

我做了你所说的,它只改变了按钮的线条而不是大厅背景。 – nomad

+0

您需要将'android:id = @ + id/view'添加到基础布局,这意味着涵盖所有背景的所有背景 – thepoosh

+0

非常感谢。您保存了一天:D – nomad

1

你需要分配一个id,你LinearLayout

<LinearLayout 
    android:id="@+id/view" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_gravity="bottom" 
    android:background="#FFFFFF" 

然后初始化你的观点

view = (LinearLayout) findViewById(R.id.view) 
red.setOnClickListener(new View.OnClickListener() { 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 

     view.setBackgroundColor(Color.RED); 

    } 
}); 
+0

我做了你说的,它只改变了按钮的线,而不是大厅background.did我做错了分配按钮我的意思是包装内容或什么 – nomad

+0

试图改变这个'LinearLayout'的高度'match_parent' –

+0

谢谢非常我解决了我的问题:)) – nomad

0

试试这个代码

这会为我工作... 在youyr xml文件集ID为您的线性布局视图。

<LinearLayout 

    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="horizontal" 
    android:layout_gravity="bottom" 
    android:id="@+id/view" 
    android:background="#FFFFFF" 


    > 

在你的Java文件的线性布局视图是因为ID的空。

所以把下面一行在你的java文件

view = (LinearLayout) findViewById(R.id.view); 

这会为我工作....

编码愉快..

0

后的onClick使用下面的代码:

view.setBackgroundColor(Color.DKGRAY); 

示例:

public void onClick(View v) { 
     view.setBackgroundColor(Color.DKGRAY); //Add any color you want 
    }