ClassCastException: android.widget.LinearLayout$LayoutParams 无法转换为 android.widget.FrameLayout$LayoutParams

ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams

Java 代码:

package com.bariskarapelit.stajprojesi_1;

import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.view.MotionEvent;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.GridLayout;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.VideoView;

import androidx.constraintlayout.widget.ConstraintLayout;

import com.twilio.video.VideoTextureView;

import pl.droidsonroids.gif.GifImageView;

public class MainActivity extends Activity
{

    GridLayout gridLayout;
    VideoView videoView;
    String videoPath;
    VideoTextureView  videoTextureView;
    Uri uri;
    GifImageView gifImageView;


    int windowwidth;
    int windowheight;

    private LinearLayout.LayoutParams layoutParams;  //linear layout yaptığın zaman buraya dikkat et

    Handler handler;



    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);



        gifImageView= findViewById(R.id.gift);

        gifImageView.setVisibility(View.INVISIBLE);

        FrameLayout frameLayout = findViewById(R.id.frame_layout);






        //videoView=findViewById(R.id.video_view_top_right);
        //Uri uri = Uri.parse("android.resource://"+getPackageName()+"/"+R.raw.video);
        //videoView.setVideoURI(uri);

        windowwidth = getWindowManager().getDefaultDisplay().getWidth();
        windowheight = getWindowManager().getDefaultDisplay().getHeight();

        handler = new Handler();

        frameLayout.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent event) {


                LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) frameLayout.getLayoutParams();
                switch (event.getAction()) {
                    case MotionEvent.ACTION_DOWN:




                        break;
                    case MotionEvent.ACTION_MOVE:



                        System.out.println("TIKLANDI");


                        gifImageView.setVisibility(View.VISIBLE);


                        int x_cord = (int) event.getRawX();
                        int y_cord = (int) event.getRawY();




                        if (x_cord > windowwidth) {
                            x_cord = windowwidth;
                        }
                        if (y_cord > windowheight) {
                            y_cord = windowheight;
                        }

                        layoutParams.leftMargin = x_cord - 53;
                        layoutParams.topMargin = y_cord - 53;

                        gifImageView.setLayoutParams(layoutParams);

                        break;
                    default:
                        break;
                }


                return true;
            }
        });



    }
}

Xml 代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


 

       xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
    
       <FrameLayout
           android:id="@+id/frame_layout"
           android:layout_width="match_parent"
           android:layout_height="match_parent">
    
    
           <com.twilio.video.VideoTextureView
               android:id="@+id/video_view_top_right"
               android:layout_width="0dp"
               android:layout_height="0dp"
    
               app:layout_constraintBottom_toBottomOf="parent"
               app:layout_constraintEnd_toEndOf="parent"
               app:layout_constraintHorizontal_bias="1.0"
               app:layout_constraintStart_toStartOf="parent"
               app:layout_constraintTop_toTopOf="parent"
               app:layout_constraintVertical_bias="0.0" />
    
    
    
    
           <pl.droidsonroids.gif.GifImageView
               android:id="@+id/gift"
               android:layout_width="106dp"
               android:layout_height="106dp"
               android:layout_gravity="center"
               android:background="@drawable/like_gif"
               />
    
       </FrameLayout>
    
    
    
    
    </LinearLayout>

错误代码:

    2020-08-13 23:09:06.762 12686-12686/com.bariskarapelit.stajprojesi_1 E/AndroidRuntime: FATAL 
    EXCEPTION: main
    Process: com.bariskarapelit.stajprojesi_1, PID: 12686
    java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to 
    android.widget.FrameLayout$LayoutParams

FrameLayout 的 setOnTouchListener 方法失败。使用这种方法,我想在 FrameLayout 中显示您触摸的图像视图,但它给出了错误“android.widget.LinearLayout $ LayoutParams cannot be cast to android.widget.FrameLayout $ LayoutParams”。你能帮帮我吗?

1- 首先,如果你想改变“gifImageView”元素的布局参数,你应该使用它的布局参数。

2- 由于您的 gifImageView 在 FrameLayout 中,您应该使用 Frame Layout 参数而不是 Lineer Layout Params

所以改变 OnTouch Method 的相关部分应该会有帮助

FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)
    gifImageView.getLayoutParams();

3- 如果你想通过触摸移动整个 frameLayout 移动你应该改变相关部分:

frameLayout.setLayoutParams(layoutParams);