第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

Android 自定義 Toast 消息在單獨(dú)的類中不起作用

Android 自定義 Toast 消息在單獨(dú)的類中不起作用

qq_笑_17 2021-09-15 16:01:57
我為自定義吐司創(chuàng)建了以下方法。public void customToastMessage(String message){    LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);    View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));    TextView toastMessage = layout.findViewById(R.id.myCustomToastText);    toastMessage.setText(message);    Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);    warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);    warningMessage.setView(layout);    warningMessage.show();}只要這個(gè)方法存在于 MainActivity 中,它就可以正常工作,但是當(dāng)我將它移到一個(gè)單獨(dú)的類時(shí),我得到:“java.lang.IllegalStateException:無(wú)法在 android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:389) 處執(zhí)行 android:onClick 的方法”。我在下面的課程中需要改變什么?public class MyCustomUI extends AppCompatActivity {    private static Context con;    public MyCustomUI(Context con){        this.con = con;    }    public void customToastMessage(String message){         LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);         View layout = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));         TextView toastMessage = layout.findViewById(R.id.myCustomToastText);         toastMessage.setText(message);         Toast warningMessage = Toast.makeText(con, message, Toast.LENGTH_LONG);         warningMessage.setGravity(Gravity.TOP | Gravity.CENTER_HORIZONTAL, 0, 10);         warningMessage.setView(layout);         warningMessage.show();    }}
查看完整描述

1 回答

?
阿晨1998

TA貢獻(xiàn)2037條經(jīng)驗(yàn) 獲得超6個(gè)贊

我猜你的問題是當(dāng)你膨脹你的布局時(shí):


視圖布局 = inf.inflate(R.layout.custom_toast_layout,(ViewGroup)findViewById(R.id.myCustomToast));


我也猜測(cè)問題是(ViewGroup)findViewById(R.id.myCustomToast). 您正在嘗試查找該類上不存在但在 MainActivity 上的視圖/視圖組。


將它作為參數(shù)傳遞給您的方法(只是相關(guān)部分):


public void customToastMessage(String message, ViewGroup customToast){

    LayoutInflater inf = (LayoutInflater)con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    View layout = inf.inflate(R.layout.custom_toast_layout, viewgroup);


查看完整回答
反對(duì) 回復(fù) 2021-09-15
  • 1 回答
  • 0 關(guān)注
  • 333 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)