Я создаю файл ресурсов макета с именем btn.xml, он содержит ImageView. в основной деятельности i раздувают btn.xml в content_main.xml, и я пытаюсь установить фон с раздутым представлением. но он показывает мне: Drawable.setColorFilter (интермедиат, android.graphics.PorterDuff $ Mode)»на нулевой ссылки на объектsetColorFilter показывает нулевую ссылку на объект
код Java в mainActivity:
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>
пост макет – Blackbelt
я редактировал пост –
btn.xml не имеет фона. Почему вы ожидаете, что getBackground вернет что-то не null? – Blackbelt