代码之家  ›  专栏  ›  技术社区  ›  Otmàane Fikri

setColorFilter显示空对象引用

  •  1
  • Otmàane Fikri  · 技术社区  · 9 年前

    我创建了一个名为btn的布局资源文件。xml,它包含ImageView。 在主要活动中,我膨胀了btn。content_main中的xml。xml和我尝试设置膨胀视图的背景。但它告诉我: 可绘制。空对象引用上的setColorFilter(int,android.graphics.PorterDuff$Mode)'

    mainActivity中的java代码:

    View v;
    RelativeLayout rlt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        LayoutInflater myInflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
        v = myInflater.inflate(R.layout.btn,null,false);
        // Here is the probelm :
        v.getBackground().setColorFilter(Color.parseColor("#00ff00"), PorterDuff.Mode.DARKEN);
        rlt = (RelativeLayout) findViewById(R.id.rlt);
        rlt.addView(v);
    }
    

    content_main。xml布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        tools:context="com.example.othma.tjkljklj.MainActivity"
        tools:showIn="@layout/activity_main"
        android:id="@+id/rlt">
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!"
            android:id="@+id/textView" />
    
    </RelativeLayout>

    btn。xml布局资源文件

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:weightSum="1">
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/imgv1"
            android:src="@drawable/continu" />
    </LinearLayout>
    2 回复  |  直到 9 年前
        1
  •  1
  •   settaratici    9 年前

    在您的 btn.xml 您看到的文件没有设置任何可绘制的视图(线性布局),因此您得到了一个 空对象引用 错误

    设置颜色过滤器() 方法指定 颜色 波特-达夫模式 作为滤色器 可抽出的 .

    例如如果要更改imageview可绘制(或任何视图可绘制)的颜色,应使用setColorFilter()方法。

    imageview.getDrawable().setColorFilter(color, mode);
    

    如果要更改视图的背景色,则应使用 设置背景颜色() 方法

    view.setBackgroundColor(getResources().getColor(R.color.anycolor)); 
    
        2
  •  0
  •   Behzad Bahmanyar Kirill Boyarshinov    9 年前

    LinearLayout没有任何背景。

    你可能应该使用 setBackgroundColor(int剩余) 这样地:

    v.setBackgroundColor(Color.parseColor("#00ff00"));
    

    注意:明确地投射你膨胀的观点要安全得多。下面是完整的代码:

    LinearLayout v;
    RelativeLayout rlt;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        LayoutInflater myInflater = (LayoutInflater) getApplicationContext().getSystemService(getApplicationContext().LAYOUT_INFLATER_SERVICE);
    
        // Explicit Cast to LinearLayout
        v = (LinearLayout) myInflater.inflate(R.layout.btn,null,false);
        // setting background:
        v.setBackgroundColor(Color.parseColor("#00ff00"));
    
        rlt = (RelativeLayout) findViewById(R.id.rlt);
        rlt.addView(v);
    }