Serviceからレイアウトをinflateできない罠
環境
AndroidStudio 2.2.3
compileSdkVersion 24
buildToolsVersion "23.0.3"
現象
Serviceにこんな感じのコード書いたら描画されなかった。(View自体は生成されてる)
@Override public int onStartCommand(Intent intent, int flags, int startId) { super.onStartCommand(intent, flags, startId); getWindowManager().addView(LayoutInflater.from(this).inflate(R.layout.sample, null), params); return START_STICKY; }
原因
よくわからんけどリソースに書かれてた
xmlns:app="http://schemas.android.com/apk/res-auto"
を消したら描画されるようになった
だめなリソース
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@mipmap/ic_launcher" android:id="@+id/floatingButton" /> </LinearLayout>
大丈夫なリソース
<?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"> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@mipmap/ic_launcher" android:id="@+id/floatingButton" /> </LinearLayout>