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
FATAL EXCEPTION: main Process: com.example.app38, PID: 14499 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app38/com.example.app38.MainActivity}: java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3635) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003) Caused by: java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams at android.widget.ViewSwitcher.obtainView(ViewSwitcher.java:86) at android.widget.ViewSwitcher.setFactory(ViewSwitcher.java:104) at com.example.app38.MainActivity.onCreate(MainActivity.java:33) at android.app.Activity.performCreate(Activity.java:8051) at android.app.Activity.performCreate(Activity.java:8031) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1329) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3608) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3792) at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:103) at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2210) at android.os.Handler.dispatchMessage(Handler.java:106) at android.os.Looper.loopOnce(Looper.java:201) at android.os.Looper.loop(Looper.java:288) at android.app.ActivityThread.main(ActivityThread.java:7839) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
The line of code at line 33:
imgSwitcher.setFactory(MainActivity.this);
Can any one suggest a answer for my problem.
CodePudding user response:
change
imgView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
to
imgView.setLayoutParams(new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT));