无法在 toast 中显示图像
我想在 Android 的 Toast 中显示图像。在我的layout.xml中,我定义了一个LinearLayout“svllid”,其中包含一个textview和一个tablelayout,如下所示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:weightSum="1" android:id="@+id/llid">
<EditText
android:id="@+id/cpuText"
android:hint="Enter cpu"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/ramText"
android:hint="Enter ram"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/bandwidthText"
android:hint="Enter bandwidth"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="@+id/imageRequestButton"
android:layout_height="wrap_content"
android:text="Download"
android:layout_width="fill_parent" android:onClick="sendImageRequest">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:weightSum="1" android:id="@+id/svllid">
<TextView android:text="client profile"
android:id="@+id/profileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#000000">
</TextView>
<TableLayout
android:paddingBottom="3pt"
android:background="#0000FF"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TableRow>
<TextView
android:paddingLeft="3pt"
android:paddingTop="3pt"
android:text="Image Name"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="blah.png"
android:textColor="#FFFFFF"
android:layout_width="315px"
android:layout_height="wrap_content" android:id="@+id/imageName"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Size"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="155kb"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageSize"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Dimensions"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="250 X 150px"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageDimension"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
我想显示id为'svllid' 在 Toast 中并显示我的活动代码中的 toast。
现在,在实际的 android 代码中,我首先调用
setContentView(R.layout.main);
随后,我从肥皂消息中读取图像。然后,我创建一个 ImageView,并将其插入到 LinearLayout 'svllid' 中,并在 Android Toast 中显示该 LinearLayout。
Toast imageToast = new Toast(this);
LinearLayout toastLayout = (LinearLayout) findViewById(R.id.svllid);
toastLayout.addView(image,1);
imageToast.setView(toastLayout);
imageToast.setDuration(Toast.LENGTH_LONG);
imageToast.show();
但是,这是行不通的。我的应用程序崩溃并出现错误:
java.lang.IllegalArgumentException: View not attached to window manager
有什么想法吗?
I want to show an image in a Toast in Android. In my layout.xml, I have defined a LinearLayout 'svllid', that contains a textview and a tablelayout, like so:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#CCCCCC"
android:weightSum="1" android:id="@+id/llid">
<EditText
android:id="@+id/cpuText"
android:hint="Enter cpu"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/ramText"
android:hint="Enter ram"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="@+id/bandwidthText"
android:hint="Enter bandwidth"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="@+id/imageRequestButton"
android:layout_height="wrap_content"
android:text="Download"
android:layout_width="fill_parent" android:onClick="sendImageRequest">
</Button>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#00FF00"
android:weightSum="1" android:id="@+id/svllid">
<TextView android:text="client profile"
android:id="@+id/profileName"
android:layout_width="fill_parent"
android:textStyle="bold"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#000000">
</TextView>
<TableLayout
android:paddingBottom="3pt"
android:background="#0000FF"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_height="wrap_content">
<TableRow>
<TextView
android:paddingLeft="3pt"
android:paddingTop="3pt"
android:text="Image Name"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="blah.png"
android:textColor="#FFFFFF"
android:layout_width="315px"
android:layout_height="wrap_content" android:id="@+id/imageName"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Size"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="155kb"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageSize"/>
</TableRow>
<TableRow>
<TextView
android:paddingLeft="3pt"
android:text="Dimensions"
android:layout_width="150px"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"/>
<TextView
android:paddingLeft="3pt"
android:text="250 X 150px"
android:textColor="#FFFFFF"
android:layout_width="300px"
android:layout_height="wrap_content" android:id="@+id/imageDimension"/>
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
I want to show the LinearLayout with id 'svllid' in a Toast and show the toast from my activity code.
Now, in the actual android code, I first call
setContentView(R.layout.main);
Subsequently, I read an image from a soap message. I then create an ImageView and I want to insert it into the LinearLayout 'svllid' and show that LinearLayout in an Android Toast.
Toast imageToast = new Toast(this);
LinearLayout toastLayout = (LinearLayout) findViewById(R.id.svllid);
toastLayout.addView(image,1);
imageToast.setView(toastLayout);
imageToast.setDuration(Toast.LENGTH_LONG);
imageToast.show();
However, it won't work. My application crashes with the error:
java.lang.IllegalArgumentException: View not attached to window manager
Any ideas why?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信您必须为根布局使用特定的 ID。您必须将其命名为“toast_layout”或“toast_layout_root”(不确定必须使用哪一个 - 文档在那里有点模糊,请尝试两者)。
正如 Varun 所说,您需要将布局放入其自己的布局文件中。
阅读 Android 文档并按照给定的示例进行操作。
I believe you have to use a specific ID for the root layout. You have to name it "toast_layout" or "toast_layout_root" (not sure which one has to be used- the documentation is slightly ambigious there, try both).
And as Varun said you need to put the layout in its own layout file.
Read the Android Doc's and follow the given example.
为此,您可以使用自定义 Toast 来显示图像。
请参阅此。
For this you can use custom Toast to display image.
Refer This.