gpt4 book ai didi

java - 使用java代码设置RelativeLayout背景颜色

转载 作者:行者123 更新时间:2023-12-01 16:36:54 25 4
gpt4 key购买 nike

我有

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/mainRelativeLayout">

我正在尝试使用 java 代码设置背景(我不想在 xml 中进行),在 onCreate 方法中使用此代码:

View view = (View)findViewById(R.id.mainRelativeLayout);
view.setBackgroundColor(0xFF000000);

或者这段代码:

View view = (View)findViewById(R.id.mainRelativeLayout);
view.setBackgroundColor(android.R.color.white);

但是,当它尝试查找 id 时,它在第一行失败。问题出在哪里?谢谢

我的 onCreate() 代码开始:

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

堆栈跟踪:

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)

最佳答案

您是否能够在不更改背景颜色的情况下成功运行您的应用程序?因为我能够运行您的代码的简化版本,没有任何错误。我认为您的错误可能在其他地方。

您是否尝试过使用Project > Clean来清理和重建您的项目?

这是我使用的测试代码。

Activity :

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

View view = findViewById(R.id.mainLayout);
view.setBackgroundColor(0xFFEE3333);
}

和 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>

关于java - 使用java代码设置RelativeLayout背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8114324/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com