OnClick在编辑toastview时崩溃,然后返回

时间:2011-04-27 00:19:47

标签: java android facebook

我正在尝试将已经使用XML的RelativeLayout添加到Toast的视图中。这不是我的主要UI线程。

@Override
        public void onClick(View v) 
        {
            mContext.setContentView(R.layout.toast_layout);
            Drawable myImage;

            Toast imageToast = new Toast(mContext);

            final RelativeLayout toastLayout = (RelativeLayout) mContext.findViewById(R.id.relative_toast);
            final ImageView myIv = (ImageView)toastLayout.findViewById(R.id.tImage);
            final TextView nameText = (TextView)toastLayout.findViewById(R.id.tFriendName);
            final TextView interestText = (TextView)toastLayout.findViewById(R.id.tInterests);

            myImage = getImageDrawable(mContext, friend.getmPic(), "image.jpg");

            myIv.setImageDrawable(myImage);

            nameText.setText("Name: " + friend.getmName() +
                             "\nID: " + friend.getmId());
            nameText.setPadding(10, 0, 0, 5);
            nameText.setTextColor(Color.BLACK);

            interestText.setText("Interests: " + friend.getmInterests());
            interestText.setTextColor(Color.BLACK);

            toastLayout.setBackgroundColor(Color.LTGRAY);
            toastLayout.setMinimumHeight(400);
            toastLayout.setMinimumWidth(250);
            toastLayout.setPadding(10, 10, 10, 10);

            imageToast.setView(toastLayout);
            imageToast.setDuration(Toast.LENGTH_LONG);
            imageToast.show();
        }

调试显示我从此范围返回时崩溃。

logcat的:

04-27 00:15:56.091: ERROR/AndroidRuntime(586): FATAL EXCEPTION: main
04-27 00:15:56.091: ERROR/AndroidRuntime(586): java.lang.IllegalArgumentException: View not attached to window manager
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.view.WindowManagerImpl.findViewLocked(WindowManagerImpl.java:355)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.view.WindowManagerImpl.removeView(WindowManagerImpl.java:200)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.widget.Toast$TN.handleShow(Toast.java:370)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.widget.Toast$TN$1.run(Toast.java:302)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.os.Handler.handleCallback(Handler.java:587)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.os.Handler.dispatchMessage(Handler.java:92)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.os.Looper.loop(Looper.java:123)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at android.app.ActivityThread.main(ActivityThread.java:4627)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at java.lang.reflect.Method.invokeNative(Native Method)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at java.lang.reflect.Method.invoke(Method.java:521)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-27 00:15:56.091: ERROR/AndroidRuntime(586):     at dalvik.system.NativeStart.main(Native Method)

我在这里改变的是我不应该做什么?

感谢。

1 个答案:

答案 0 :(得分:1)

如果您尝试制作自定义Toast布局,则应将布局的xml保存在自己的资源xml文件中(即toast_layout.xml) 那么你应该

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

其中toast_layout_root是前面提到的toast_layout.xml

的根元素

然后

toast.setView(layout);