2012-07-26 25 views
0

我創建了一個線性佈局,它包含四次相同的線性佈局,以獲得一個列表。但是,當我嘗試更改其中包含這些佈局的背景顏色時,我收到了nullpointerexception。任何人都知道爲什麼以及如何解決它?NPE on setbackgroundcolor

我的課:

protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.overview); 

     String language = Locale.getDefault().getISO3Language(); 
     georgiaI = Typeface.createFromAsset(getAssets(), "georgiai.ttf"); 
     arialB = Typeface.createFromAsset(getAssets(), "arialbd.ttf"); 


     llPlaces= (LinearLayout)findViewById(R.id.ll_overview_list_places); 


     // Places 
     llitem1= (LinearLayout)llPlaces.findViewById(R.id.lloverview); 
     llitem1.setBackgroundColor(R.color.list_even); 
     String item1a; 
     String item1b; 
     if(language.equals("nld")){ 
      item1a = "dutchstring"; 
      item1b = "dutchstring"; 
     }else{ 
      item1a = "englishString"; 
      item1b = "englishString"; 
     } 
     tvitem1a=(TextView)llPlaces.findViewById(R.id.tvoverviewname); 
     tvitem1a.setTypeface(arialB); 
     tvitem1a.setText(item1a.toUpperCase()); 
     tvitem1b=(TextView)llPlaces.findViewById(R.id.tvoverviewtext); 
     tvitem1b.setTypeface(georgiaI); 
     tvitem1b.setText(item1b); 
     iv1= (ImageView)llPlaces.findViewById(R.id.ivoverviewimage); 
     iv1.setImageResource(R.drawable.categories_places); 

當NPE發生是行:llitem1.setBackgroundColor(R.color.list_even); 我想改變顏色,這就是爲什麼我沒有在XML中添加顏色。 Offcourse我可以製作兩個xml文件,用於不同的顏色,但這應該起作用。

XML文件:

overview.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:background="@color/orange"> 

    <include 
      android:id="@+id/bt_overview_list_places" 
      layout="@layout/single_overview_item" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 
     <include 
      android:id="@+id/bt_overview_list_agenda" 
      layout="@layout/single_overview_item" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 
     <include 
      android:id="@+id/bt_overview_list_shopping" 
      layout="@layout/single_overview_item" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 
     <include 
      android:id="@+id/bt_overview_list_food" 
      layout="@layout/single_overview_item" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center"/> 


</LinearLayout> 

單概述項目:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/lloverview" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:orientation="vertical" 
    android:background="@color/custom_button_grey" 
    > 
<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="horizontal" 
    > 
    <ImageView 
     android:id="@+id/ivoverviewimage" 
     android:layout_width="50dp" 
     android:layout_height="50dp" android:src="@drawable/ic_launcher" android:scaleType="centerCrop" 
     android:layout_margin="10dp" 
     android:layout_weight="1"/> 

    <LinearLayout 
     android:layout_width="225dp" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tvoverviewname" 
     android:layout_width="225dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="left|top" 
     android:layout_marginLeft="10dip" 
     android:text="naam" 
     android:textSize="16dip" 
     android:maxLines="1" 
     android:ellipsize="marquee" 
     android:textColor="@color/black"/> 

<TextView 
    android:id="@+id/tvoverviewtext" 
    android:layout_width="225dp" 
    android:layout_height="wrap_content" 
    android:layout_marginLeft="10dip" 
    android:maxLines="3" 
    android:text="tip" 
    android:textSize="12dip" 
    android:ellipsize="end" 
    android:textColor="@color/black"/> 

</LinearLayout> 



<ImageView 
    android:id="@+id/arrow" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:src="@drawable/iconarrowsmall" 
     android:layout_gravity="center_vertical" 
     android:layout_margin="2dip" 
    /> 

</LinearLayout> 
<ImageView 
    android:id="@+id/bottom_border" 
    android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/custombuttonlist_bottomborder" 
    /> 

</LinearLayout> 

回答

1

如果您對包含內容設置不同的ID,然後你的代碼應該是這樣的(有不再是LinearLayout,其編號爲lloverview):

llPlaces= (LinearLayout)findViewById(R.id.ll_overview_list_places); 
llPlaces.setBackgroundColor(R.color.list_even); 

如果您將id設置爲include標記,那麼這將是id用於添加布局的根視圖。

+0

謝謝,工作完美。不知道它是這樣工作的。 – Jasper 2012-07-26 13:47:20