设置的RelativeLayout的backgroundColor使用Java代码

问题描述:

设置的RelativeLayout的backgroundColor使用Java代码

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

但是,它未能在第一线时它会尝试鳍d 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) 
+0

在其onCreate方法内点你打findviewbyid?你能提供你的完整代码吗? – Michele

+0

我已将它添加到帖子中 – simPod

+0

您可以提供错误堆栈跟踪吗? – NOSTRA

是你可以在不改变背景颜色成功运行你的应用程序?因为我能够无误地运行代码的简化版本。我认为你的错误可能在其他地方。

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

这是我使用的测试代码。

的活动:

@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> 
+0

好吧,项目>干净的帮助。这很奇怪......当我删除后台代码时,它工作正常。清洁后,即使使用后台代码也可以使用。谢谢 – simPod