简体   繁体   中英

Toast works in Java activity but not kotlin

I have a custom toast and it works in java activity but not in kotlin, in kotlin activity, it throws the following error:

kotlin.TypeCastException: null cannot be cast to non-null type android.view.ViewGroup 

on this line

val layout = inflater.inflate(R.layout.custom_toast,
      findViewById<View>(R.id.custom_toast_container) as ViewGroup)

Here is the toast:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/custom_toast_container"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="8dp"
android:background="@color/colorPrimary"
>
<ImageView android:src="@drawable/ic_done_black_24dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="8dp"
    android:tint="@color/colorBackground"
    />
<TextView android:id="@+id/text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#FFF"
    />

How i call it in java:

LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast_container));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Already reported");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.BOTTOM, 0, 145);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();

How android studio converts it kotlin:

 val inflater = layoutInflater
 val layout = inflater.inflate(R.layout.custom_toast,
 findViewById<View>(R.id.custom_toast_container) as ViewGroup)

 val text = layout.findViewById<View>(R.id.text) as TextView
 text.text = "Already reported"
 val toast = Toast(context)
 toast.setGravity(Gravity.BOTTOM, 0, 145)
 toast.duration = Toast.LENGTH_LONG
 toast.view = layout
 toast.show()

What am I doing wrong here?

I think it's because of findViewById returns nullable but you're triyng to cast as non-null type. Here i modified your code a little bit:

 val inflater = layoutInflater
 val layout = inflater.inflate(R.layout.custom_toast,
 findViewById<View>(R.id.custom_toast_container) as ViewGroup?)

 val text = layout?.findViewById<View>(R.id.text) as TextView?
 text?.text = "Already reported"
 val toast = Toast(context)
 toast.setGravity(Gravity.BOTTOM, 0, 145)
 toast.duration = Toast.LENGTH_LONG
 toast.view = layout
 toast.show()

I hope this'll help you.

Toast.makeText(applicationContext,"this is toast message",Toast.LENGTH_SHORT).show()  

        val toast = Toast.makeText(applicationContext, "Hello Javatpoint", Toast.LENGTH_SHORT)  
        toast.show()  

        val myToast = Toast.makeText(applicationContext,"toast message with gravity",Toast.LENGTH_SHORT)  
        myToast.setGravity(Gravity.LEFT,200,200)  
        myToast.show()  

Easy way to avoiding findViewById

In your build.gradle(app) file add below line.

apply plugin: 'kotlin-android-extensions'

Now in your .kt file you can write below code.

 val inflater = layoutInflater
        var inflatedFrame = inflater.inflate(R.layout.demo, null)

        inflatedFrame.text.text = "Already reported"

        val toast = Toast(context)
        toast.setGravity(Gravity.BOTTOM, 0, 145)
        toast.duration = Toast.LENGTH_LONG
        toast.view = inflatedFrame
        toast.show()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM