为什么我的简单Toast不想出现并且应用程序崩溃了?

问题描述 投票:0回答:4

大家,我正在尝试做这样一件愚蠢的事情:将文本置于在 onclick 事件侦听器内创建的 Toast 中居中。

我在我使用的布局中创建了一个 TextView,当我尝试调用应该显示 toast 的事件时,应用程序崩溃并出现此异常:

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>

这段简单的代码问题出在哪里?

感谢关注!

编辑:这是 custom_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>
java android toast
4个回答
2
投票

您正在使用已附加到另一个视图层次结构的视图。您必须像这样单独膨胀您的自定义 toast 视图:

custom_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>

这就是你如何使用它:

LayoutInflater inflater = LayoutInflater.from(context);
View customToastView = inflater.inflate(R.layout.custom_toast, null);

TextView tvToast = (TextView) customToastView.findViewById(R.id.tvToast);
tvToast.setText("This is a custom toast with centered text");

Toast toast = new Toast(context);
toast.setView(customToastView);
toast.setDuration(Toast.LENGTH_SHORT);
toast.show();

0
投票

我的猜测是,您必须将 toast_layout.xml 设置为 toast 的视图,而不仅仅是其中的单个 textView 。 尝试在最外层的线性布局中添加一个 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 视图。

java.file

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();


所以“toast_layout”是包含布局的xml文件的名称,toast_layout_root是该文件中根元素的id(最外层的线性布局)。

希望这有帮助,

后续链接:
http://meddington.hubpages.com/hub/How-To-Toast-In-Android


0
投票

尝试使用 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();

0
投票

在我的例子中,我使用了充气机的默认根布局ID

val contentView = findViewById<ViewGroup>(android.R.id.content)
inflater.inflate(R.layout.custom_toast, contentView)

我通过传递 null 来修复它

inflater.inflate(R.layout.custom_toast, null)

它避免了不必要的复杂性,并且这是正确的方法,因为自定义视图不会成为父 ViewGroup 的一部分。

© www.soinside.com 2019 - 2024. All rights reserved.