[英]ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Java codes: 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 Codes: 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>
Error Code:错误代码:
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's setOnTouchListener method failed. FrameLayout 的 setOnTouchListener 方法失败。 With this method, I wanted to show imageview where you touched in FrameLayout, but it gave the error "android.widget.LinearLayout $ LayoutParams cannot be cast to android.widget.FrameLayout $ LayoutParams".使用这种方法,我想显示您在 FrameLayout 中触摸的 imageview,但它给出了错误“android.widget.LinearLayout $ LayoutParams cannot be cast to android.widget.FrameLayout $ LayoutParams”。 Can you help me?你能帮助我吗?
1- First of all if you want to change "gifImageView" element's layout params you should take its layout params. 1-首先,如果你想改变“gifImageView”元素的布局参数,你应该使用它的布局参数。
2- As your gifImageView is inside FrameLayout you should use Frame Layout params rather than Lineer Layout Params 2-由于您的 gifImageView 在 FrameLayout 内,您应该使用 Frame Layout params 而不是 Lineer Layout Params
So changing OnTouch Method's related part with this should help所以用这个改变 OnTouch 方法的相关部分应该会有所帮助
FrameLayout.LayoutParams layoutParams = (FrameLayout.LayoutParams)
gifImageView.getLayoutParams();
3- if you want to move your whole frameLayout move with the touch you should change the related part with this: 3-如果您想通过触摸移动整个 frameLayout 移动,您应该使用以下内容更改相关部分:
frameLayout.setLayoutParams(layoutParams);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.