Java 为什么我的简单吐司没有';你不想出现并且应用程序崩溃吗?
每个人,我都在尝试做这样一件愚蠢的事情:将文本放在一个Toast中,这个Toast是在onclick事件侦听器中创建的 我已经在我使用的布局中创建了一个TextView,当我尝试调用应该显示toast的事件时,应用程序崩溃,出现以下异常:Java 为什么我的简单吐司没有';你不想出现并且应用程序崩溃吗?,java,android,toast,Java,Android,Toast,每个人,我都在尝试做这样一件愚蠢的事情:将文本放在一个Toast中,这个Toast是在onclick事件侦听器中创建的 我已经在我使用的布局中创建了一个TextView,当我尝试调用应该显示toast的事件时,应用程序崩溃,出现以下异常: 03-19 23:20:40.258: E/AndroidRuntime(3364): java.lang.IllegalArgumentException: View not attached to window manager 这是我的代码: to
03-19 23:20:40.258: E/AndroidRuntime(3364): java.lang.IllegalArgumentException: View not
attached to window manager
这是我的代码:
toastTextView = (TextView) findViewById(R.id.toastView);
Toast toast = Toast.makeText(MyActivity.this, "Very very very long long text that should be displayed in the middle of this toast",
Toast.LENGTH_SHORT);
toast.setView(toastTextView);
toast.show();
xml:
<?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">
<LinearLayout android:id="@+id/chart" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="0dip" android:layout_weight="1"/>
<TextView
android:id="@+id/toastView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content">
</LinearLayout>
这个简单代码的问题在哪里
谢谢你的关注
编辑:这是自定义_toast.xml的布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
</LinearLayout>
您使用的视图已附加到另一个视图层次结构。您必须单独为自定义toast视图充气,如下所示: 自定义_toast.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@android:color/black"
android:padding="5dp">
<TextView
android:id="@+id/tvToast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
尝试使用一个SpannableString来格式化您的Toast,您可以从中获得很多好处,而不用担心自定义视图
String fancyText = "MyFancy long text";
SpannableStringBuilder span = new SpannableStringBuilder(fancyText);
span.setSpan(new AlignmentSpan(Layout.Alignment.ALIGN_CENTER), 0, fancyText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Toast.makeText(MyActivity.this, span, Toast.LENGTH_SHORT).show();
我的猜测是,您必须将toast_layout.xml设置为toast的视图,而不仅仅是其中的单个文本视图。 尝试将id添加到最外层的线性布局,… toast_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout .../>
<TextView .../>
</LinearLayout>
因此,“toast_layout”是包含布局的xml文件的名称,而toast_layout_root是该文件中根元素(最外层的线性布局)的id 希望这有帮助,
凯 后续链接:
您需要为整个布局充气(或手动创建视图以使用它)。。。使用findViewById获得的视图已附加到其他视图感谢您的回复。我尝试过该代码,但我得到了这样的结果:AlignmentSpan无法解析为类型-SPAN_EXCLUSIVE_EXCLUSIVE无法解析为变量-我该怎么办?我尝试过膨胀土司,但当我单击并触发事件时,我看不到土司。我已经编辑了我的帖子,并把吐司的版式代码放进去了。用你发布的版式,你不会有太多的运气,那里没有文本视图或任何东西,当然不会显示任何东西。我将编辑我的答案以向您展示。我已经编辑了我的答案,为您的问题提供了完整的解决方案。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/toast_layout_root"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout .../>
<TextView .../>
</LinearLayout>
View toastRootView = getLayoutInflater().inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.toast_layout_root));
TextView text = findViewById(R.id.toastView);
text.setText("Very [...] long text [...]");
Toast toast = new Toast(MyActivity.this)
.setView(toastRootView)
.setDuration(Toast.LENGTH_SHORT)
.show();