2

カスタム トースト (アイコンとメッセージ付き) を表示しようとすると、いくつかの問題が発生します。これがコードです。

private void makeToast(String text, int icon) {

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.message));
        ((TextView) layout.findViewById(R.id.message)).setText(text);

        layout = inflater.inflate(R.layout.custom_toast,(ViewGroup) findViewById(R.id.icon));

        // 0 - Exclamation, 1 - Noow icon
        if(icon == 0){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.exclamation);
        }else if(icon == 1){
            ((ImageView) layout.findViewById(R.id.icon)).setImageResource(R.drawable.nicon);
        }


        Toast toast = new Toast(getBaseContext());
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
    }

2番目のパラメータが0または1のときのアイコンとメッセージを変更したいです。2番目のレイアウトの呼び出しに問題があることはわかっていますが、解決方法がわかりません。

ありがとうございます。

編集:

このメソッドは、AsyncTask の onPostExecute から呼び出します。これを言い忘れました。

Thread [<11> AsyncTask #1] (Suspended (exception RuntimeException)) 
    ThreadPoolExecutor.runWorker(ThreadPoolExecutor$Worker) line: not available 
    ThreadPoolExecutor$Worker.run() line: not available 
    Thread.run() line: not available    

編集2:

@Override
protected void onProgressUpdate(Void... values) {
    super.onProgressUpdate(values);
    makeToast("loading nightclubs...", 1);
    }
}

編集3:

@Override
        protected void onPostExecute(List<Category> result) {
            Log.i("result", result.toString());

            // Cargamos el listado a pasarle a categoryId en la
            // consulta
            for (int k = 0; k < result.size(); k++) {
                ALLOFTHEM += result.get(k).getId();
                if (k < result.size() - 1) {
                    ALLOFTHEM += ",";
                }
            }

            Log.i("POST", ALLOFTHEM);
        }

解決しました!!!

利用した...

runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

...UI で乾杯したいときはいつでも、うまくいきます。

皆さんのお陰で。

4

4 に答える 4

0

このように使用します:

        LayoutInflater inflater = getLayoutInflater();
        View layout = inflater.inflate(R.layout.custom_toast_layout, null);

        TextView text = (TextView) layout.findViewById(R.id.toast_message);
        ImageView img = (ImageView) layout.findViewById(R.id.imageView1);

        if (icon == 0) {
            text.setText("icon 0");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher));
        }

        if (icon == 1) {
            text.setText("icon 1");
            img.setImageDrawable(getResources().getDrawable(R.drawable.ic_launcher_one));
        }

        Toast toast = new Toast(getApplicationContext());
        toast.setGravity(Gravity.BOTTOM, 0, 0);
        toast.setDuration(Toast.LENGTH_LONG);
        toast.setView(layout);
        toast.show();
于 2013-05-28T09:55:25.753 に答える
0

onPostExecute()メソッドを次のように変更します

protected void onPostExecute(String file_url) {
            // anything else; you want to do here

            // updating UI from Background Thread
            runOnUiThread(new Runnable() {
                public void run() {
                 makeToast("loading nightclubs...", 1);
                }
            });

        }
于 2013-05-28T10:19:50.683 に答える