代码之家  ›  专栏  ›  技术社区  ›  Radesh

吐司让人感到意外

  •  -1
  • Radesh  · 技术社区  · 8 年前

    在一些类似设备的应用程序中 三星Galaxy Grand Duos 安卓7.1.2我的定制祝酒辞

    java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.LinearLayout
              at ir.mrchabok.chabokpassenger.Helpers.Helper.MyToast(Helper.java:196)
              at ir.mrchabok.chabokpassenger.Activity.MapsActivity$4.onClick(MapsActivity.java:345)
              at android.view.View.performClick(View.java:5637)
              at android.view.View$PerformClick.run(View.java:22433)
              at android.os.Handler.handleCallback(Handler.java:751)
              at android.os.Handler.dispatchMessage(Handler.java:95)
              at android.os.Looper.loop(Looper.java:153)
              at android.app.ActivityThread.main(ActivityThread.java:6244)
              at java.lang.reflect.Method.invoke(Native Method)
              at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:891)
    

    我的定制吐司代码是:

    public static void MyToast(Context context, String text){
        Toast toast = Toast.makeText(context, text, Toast.LENGTH_LONG);
        LinearLayout toastLayout = (LinearLayout) toast.getView();
        TextView tvToast = (TextView) toastLayout.getChildAt(0);
        tvToast.setTypeface(globTypeFace);
        toast.show();
    }
    

    正如你所知,吐司的布局是线性布局,我没有改变布局。

    1 回复  |  直到 7 年前
        1
  •  0
  •   toBe NULL    8 年前

    JAVAClassCastException:android。小装置。RelativeLayout无法转换为android。小装置。线性布局

    这似乎是一种相对论,而不是线性布局。因此,您可以尝试将LinearLayout更改为RelativeLayout。

    顺便说一句,你最好使用Toast#setView

    推荐文章