setColorFilter 显示空对象引用
setColorFilter shows a null object reference
我创建了一个名为btn.xml的布局资源文件,它包含ImageView。
在主要 activity 中,我在 content_main.xml 中对 btn.xml 进行充气,然后尝试设置充气视图的背景。但它告诉我: Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
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>
您的 LinearLayout 没有任何背景。
您应该像这样使用 setBackgroundColor(int resid):
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);
}
在您的 btn.xml 文件中,如您所见,您没有为视图设置任何可绘制对象(线性布局),因此您得到了一个 null 对象参考错误。
setColorFilter() 方法指定一个 color 和 Porter- Duff 模式 是 drawable.
的滤色器
例如;如果你想改变 imageview drawable(或任何 view drawable)的颜色,你应该使用 setColorFilter() 方法。
imageview.getDrawable().setColorFilter(color, mode);
如果你想改变视图的背景颜色,那么你应该使用 setBackgroundColor() 方法。
view.setBackgroundColor(getResources().getColor(R.color.anycolor));
我创建了一个名为btn.xml的布局资源文件,它包含ImageView。 在主要 activity 中,我在 content_main.xml 中对 btn.xml 进行充气,然后尝试设置充气视图的背景。但它告诉我: Drawable.setColorFilter(int, android.graphics.PorterDuff$Mode)' on a null object reference
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>
您的 LinearLayout 没有任何背景。
您应该像这样使用 setBackgroundColor(int resid):
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);
}
在您的 btn.xml 文件中,如您所见,您没有为视图设置任何可绘制对象(线性布局),因此您得到了一个 null 对象参考错误。
setColorFilter() 方法指定一个 color 和 Porter- Duff 模式 是 drawable.
的滤色器例如;如果你想改变 imageview drawable(或任何 view drawable)的颜色,你应该使用 setColorFilter() 方法。
imageview.getDrawable().setColorFilter(color, mode);
如果你想改变视图的背景颜色,那么你应该使用 setBackgroundColor() 方法。
view.setBackgroundColor(getResources().getColor(R.color.anycolor));