MainActivity.java
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import com.example.app38.databinding.ActivityMainBinding;
import android.view.ViewGroup;
import android.view.animation.AnimationUtils;
import android.widget.ImageSwitcher;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class MainActivity extends AppCompatActivity implements ViewSwitcher.ViewFactory {
LinearLayout linearLayoutHorizontal;
ImageSwitcher imgSwitcher;
private ActivityMainBinding binding;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
setSupportActionBar(binding.toolbar);
linearLayoutHorizontal = findViewById(R.id.linearLayoutHorizontal);
imgSwitcher = findViewById(R.id.imgSwitcher);
imgSwitcher.setFactory(MainActivity.this);
imgSwitcher.setInAnimation(AnimationUtils.loadAnimation(MainActivity.this,
android.R.anim.slide_in_left));
imgSwitcher.setOutAnimation(AnimationUtils.loadAnimation(MainActivity.this,
android.R.anim.fade_out));
for (int index = 0; index < Animal.animalImages.length; index++) {
final int i = index;
ImageView imageView = new ImageView(MainActivity.this);
imageView.setImageResource(Animal.animalImages[index]);
letsSetlayoutParamsForImageView(imageView);
imageView.setPadding(100,100,100,100);
imageView.setOnClickListener(view -> {
imgSwitcher.setImageResource(Animal.animalImages[i]);
Toast.makeText(MainActivity.this, "This is "+ Animal.animalNames[i], Toast.LENGTH_SHORT).show();
});
linearLayoutHorizontal.addView(imageView);
}
}
public void letsSetlayoutParamsForImageView(ImageView imageView) {
imageView.setLayoutParams(new LinearLayout.LayoutParams(1000,1000));
}
@Override
public View makeView() {
ImageView imgView = new ImageView(MainActivity.this);
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return imgView;
}
}
Animal.java
public class Animal {
static int[] animalImages = { R.drawable.bear, R.drawable.bird, R.drawable.cat, R.drawable.cow,
R.drawable.dolphin, R.drawable.fish, R.drawable.fox, R.drawable.horse, R.drawable.lion, R.drawable.tiger};
static String[] animalNames = {"Bear","Bird","Cat","Cow","Dolphin","Fish","Fox","Horse","Lion","Tiger"};
}
content_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/linearLayoutHorizontal"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</HorizontalScrollView>
<ImageSwitcher
android:id="@+id/imgSwitcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ImageSwitcher>
</LinearLayout>
</ScrollView>
</LinearLayout>
</RelativeLayout>
Logcat
致命异常:main进程:com.example.app38,PID:14499java.lang.RuntimeException:无法启动活动ComponentInfo{com.example.app38/com.example.app38.MainActivity}:java.lang.ClassCastException:android.view.ViewGroup$LayoutParams无法强制转换为android.widget.FrameLayout$LayoutParam在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:3635(在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:3792(在android.app.servertransaction.LaunchActivityItem.exexecute(LaunchActivityItem.java:103(在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135(在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95(在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:2210(在android.os.Handler.dispatchMessage(Handler.java:106(在android.os.Looper.roopOnce(Looper.java:201(在android.os.Looper.loop(Looper.java:288(在android.app.ActivityThread.main(ActivityThreads.java:7839(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1003(导致原因:java.lang.ClassCastException:android.view.ViewGroup$LayoutParams无法转换为android.widget.FrameLayout$LayoutParam在android.widget.ViewSwitcher.getainView(ViewSwitcher.java:86(在android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:104(网址:com.example.app38.MainActivity.onCreate(MainActivity.java:33(在android.app.Activity.performCreate(Activity.java:8051(在android.app.Activity.performCreate(Activity.java:8031(在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329(在android.app.ActivityThread.performLaunchActivity(ActivityThreads.java:3608(在android.app.ActivityThread.handleLaunchActivity(ActivityThreads.java:3792(在android.app.servertransaction.LaunchActivityItem.exexecute(LaunchActivityItem.java:103(在android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135(在android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95(在android.app.ActivityThread$H.handleMessage(ActivityThreads.java:2210(在android.os.Handler.dispatchMessage(Handler.java:106(在android.os.Looper.roopOnce(Looper.java:201(在android.os.Looper.loop(Looper.java:288(在android.app.ActivityThread.main(ActivityThreads.java:7839(位于java.lang.reflect.Method.ioke(本机方法(网址:com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548(网址:com.android.internal.os.ZygoteInit.main(ZygoteNit.java:1003(
第33行的代码行:
imgSwitcher.setFactory(MainActivity.this(;
有人能为我的问题提出答案吗。
更改
imgView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
至
imgView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));