java - 设置背景颜色上的 NPE

标签 java android android-layout nullpointerexception

我创建了一个线性布局,其中包含四次相同的线性布局,以获得某种列表。但是,当我尝试更改其中包含的布局之一的背景颜色时,我得到一个空指针异常。有人知道为什么以及如何解决它吗?

我的类(class):

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 中添加颜色。当然,我可以制作两个 xml 文件,用于不同的颜色,但这应该可行。

xml 文件:

概述.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>

最佳答案

如果您为包含元素设置不同的 id,那么您的代码应该如下所示(布局中不再有 id 为 lloverviewLinearLayout):

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

如果您将 id 设置为 include 标记,那么这将是所添加布局的 Root View 的 id

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

相关文章:

java - 何时在 Java 中使用 Map 而不是 List?

android - 播放钛金属声音未找到错误功能

android - 如何以编程方式更改维度?

android - RecyclerView 没有正确展开项目

java - NullPointerException - android.view.Window.findViewById(int) - 为什么?

android - 我可以使工具栏的颜色渐变吗?

java - 在java中使用JpaTemplate来处理事务?

Java 全部替换,每出现两次就替换一次

c# - 如何从 Xamarin Forms for Android 的按钮 View 中删除额外的填充?

java - 为什么我的 Thread.yield() 不起作用