2012-10-28 46 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 
    }