3
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
                           (ViewGroup) findViewById(R.id.toast_layout_root));

TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("This is a custom toast");

Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

これは、カスタム トーストを作成する行です ( http://developer.android.com/guide/topics/ui/notifiers/toasts.htmlから) 。

正常に動作しますが、コードを使用する理由がわかりません

(ViewGroup) findViewById(R.id.toast_layout_root)

findViewByIdメソッドがビューの「現在の」要素を検索すると、メソッドはnullを返すと思います。

つまり、意味のないコードですね。

その問題は、サービスで create custom toast を使用しようとしているのだろうか

サービスはfindViewByIdを持っていません。

そのため、レイアウトが壊れる原因となる inflate(id, null) を試みるだけです。

私はすでにhttp://www.doubleencore.com/2013/05/layout-inflation-as-intended/の記事を読みました。

レイアウトパラメータの損失情報なしで適切にインフレートするにはどうすればよいですか

4

0 に答える 0