Reputation: 13506
I have
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainRelativeLayout">
and I'm trying to setup the background using java code (I don't want to do it in xml) using this code in onCreate
method:
View view = (View)findViewById(R.id.mainRelativeLayout);
view.setBackgroundColor(0xFF000000);
or this code:
View view = (View)findViewById(R.id.mainRelativeLayout);
view.setBackgroundColor(android.R.color.white);
However, it fails at first line when it tries to find the id. Where's the problem? Thanks
start of my onCreate() code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getPrefs();
if (themePreference) {
setTheme(android.R.style.Theme_Light_NoTitleBar);
}else{
setTheme(android.R.style.Theme_Black_NoTitleBar);
}
setContentView(R.layout.main);
/*View view = (View)findViewById(R.id.mainRelativeLayout);
view.setBackgroundColor(0xFF000000);*/
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
this.submitBtn = (Button) this.findViewById(R.id.submitBtn);
this.cleanBtn = (Button) this.findViewById(R.id.clear_txt_Input);
this.inputQ = (EditText) this.findViewById(R.id.inputQ);
...
stack trace:
myApp [Android Application]
DalvikVM[localhost:8610]
Thread [<1> main] (Suspended (exception RuntimeException))
ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1768
ActivityThread.handleLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1784
ActivityThread.access$1500(ActivityThread, ActivityThread$ActivityClientRecord, Intent) line: 123
ActivityThread$H.handleMessage(Message) line: 939
ActivityThread$H(Handler).dispatchMessage(Message) line: 99
Looper.loop() line: 130
ActivityThread.main(String[]) line: 3835
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run() line: 847
ZygoteInit.main(String[]) line: 605
NativeStart.main(String[]) line: not available [native method]
Thread [<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1] (Running)
Upvotes: 0
Views: 14984
Reputation: 8749
Were you able to run your app successfully without changing the background color? Because I was able to run a simplified version of your code without any errors. I think your error might be elsewhere.
Have you tried using Project > Clean
to clean and rebuild your project?
Here is the test code I used.
The Activity:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.mainLayout);
view.setBackgroundColor(0xFFEE3333);
}
And the XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainLayout">
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Stack Overflow"/>
</RelativeLayout>
Upvotes: 6