Reputation: 3622
i am trying to write a program that changes the background color of the screen to the color that i decided. i wrote something like that but when it runs it crashes what is sthe problem,please help me.here is the xml code
<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>
and here is the .java code
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;
}
}
Upvotes: 1
Views: 10673
Reputation: 1690
After onClick use this below code:
view.setBackgroundColor(Color.DKGRAY);
Example:
public void onClick(View v) {
view.setBackgroundColor(Color.DKGRAY); //Add any color you want
}
Upvotes: 0
Reputation: 2097
Try this code
This will work for me... In youyr xml file set id for your Linear Layout as a view.
<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"
>
In Your java file your linear layout view is null because of id.
so put below line in your java file
view = (LinearLayout) findViewById(R.id.view);
This will work for me....
Happy coding..
Upvotes: 0
Reputation: 6849
you need to assign an id to your 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"
then initialize your view
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);
}
});
Upvotes: 1
Reputation: 12587
you are not assigning the view
variable to anything, that is probably causing a NullPointerException
in your code
try adding in your XML file this line under the LinearLayout
:
android:id="@+id/view"
and adding this line to your onCreate
:
view = (LinearLayout)findViewBiId(R.id.view);
Upvotes: 0