我有自定义视图和2张图像视图。 我需要将自定义视图放在imageviews之间,因为它们会重叠,然后自定义视图将作为动画移动。
但是当我把它们放在下面的xml中时,我得到一个“ClassCastException:android.widget.ImageView无法转换为......”
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
tools:context=".FinalActivity" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="776dp"
android:layout_height="1154dp"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/oven_alt" />
<com.aktuna.game.pizzadaisy.myviewx
android:id="@+id/xview"
android:layout_width="400dp"
android:layout_height="400dp"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="776dp"
android:layout_height="1154dp"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/oven_ust" />
</RelativeLayout>
这是我得到的错误:
06-16 08:27:01.761: E/AndroidRuntime(23183): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.game/com.test.game.FinalActivity}: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.test.game.myviewx
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2955)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3030)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread.-wrap11(Unknown Source:0)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.os.Handler.dispatchMessage(Handler.java:105)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.os.Looper.loop(Looper.java:164)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread.main(ActivityThread.java:6938)
06-16 08:27:01.761: E/AndroidRuntime(23183): at java.lang.reflect.Method.invoke(Native Method)
06-16 08:27:01.761: E/AndroidRuntime(23183): at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
06-16 08:27:01.761: E/AndroidRuntime(23183): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
06-16 08:27:01.761: E/AndroidRuntime(23183): Caused by: java.lang.ClassCastException: android.widget.ImageView cannot be cast to com.test.game.myviewx
06-16 08:27:01.761: E/AndroidRuntime(23183): at com.aktuna.game.pizzadaisy.FinalActivity.onCreate(FinalActivity.java:49)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.Activity.performCreate(Activity.java:7174)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
06-16 08:27:01.761: E/AndroidRuntime(23183): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2908)
06-16 08:27:01.761: E/AndroidRuntime(23183): ... 9 more
这是自定义视图的类:
public class myviewx extends View
{
public myviewx(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public myviewx(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public myviewx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
float density = getResources().getDisplayMetrics().density;
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.drawable.base1);
Bitmap brocoli = BitmapFactory.decodeResource(res, R.drawable.brocoli);
Bitmap mint = BitmapFactory.decodeResource(res, R.drawable.mint);
Random rand = new Random();
int x=500;
int y=500;
int radius=250;
Paint paint=new Paint();
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x,x, radius, paint);
paint.setColor(Color.parseColor("#CD000C"));
canvas.drawRect (250,250, 750, 750, paint);
canvas.drawBitmap(bitmap, 0, 0, paint);
int xx[]=new int[15];;
int yy[]=new int[15];;
boolean dur=false;
for (int i=0; i<10; i++) {
dur=false;
int x1 = rand.nextInt(500) + 250;
int y1 = rand.nextInt(500) + 250;
x1=750;
y1=750;
canvas.drawBitmap(brocoli, x1-(brocoli.getWidth()/density), y1-(brocoli.getHeight()/density), paint);
x1=250;
y1=250;
canvas.drawBitmap(brocoli, x1-(brocoli.getWidth()/density), y1-(brocoli.getHeight()/density), paint);
}
}
}
public class FinalActivity extends Activity {
myviewx z1;
public void adjustFontScale(Configuration configuration) {
if (configuration.fontScale > 1.20) {
configuration.fontScale = (float) 1.20;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
adjustFontScale(getResources().getConfiguration());
setContentView(R.layout.activity_final);
z1 = (myviewx) findViewById(R.id.xview);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
ObjectAnimator animation = ObjectAnimator.ofFloat(z1, "translationX", 100f);
animation.setDuration(5000);
animation.start();
}
}
}
这真的不可能吗? 我该怎么办?
答案 0 :(得分:2)
请尝试使用此代码,并确保自定义视图路径,特别是在xml中。
package com.androidtestapp;
import android.animation.ObjectAnimator;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.DisplayMetrics;
import android.view.WindowManager;
public class FinalActivity extends Activity {
myviewx z1;
public void adjustFontScale(Configuration configuration) {
if (configuration.fontScale > 1.20) {
configuration.fontScale = (float) 1.20;
DisplayMetrics metrics = getResources().getDisplayMetrics();
WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
wm.getDefaultDisplay().getMetrics(metrics);
metrics.scaledDensity = configuration.fontScale * metrics.density;
getBaseContext().getResources().updateConfiguration(configuration, metrics);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
adjustFontScale(getResources().getConfiguration());
setContentView(R.layout.activity_main);
z1 = (myviewx) findViewById(R.id.xview);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
if(hasFocus){
ObjectAnimator animation = ObjectAnimator.ofFloat(z1, "translationX", 100f);
animation.setDuration(5000);
animation.start();
}
}
}
=============================================== ===========================
package com.androidtestapp;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;
public class myviewx extends View
{
public myviewx(Context context) {
super(context);
// TODO Auto-generated constructor stub
}
public myviewx(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
}
public myviewx(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
float density = getResources().getDisplayMetrics().density;
Resources res = getResources();
Bitmap bitmap = BitmapFactory.decodeResource(res, R.mipmap.ic_launcher_foreground);
Bitmap brocoli = BitmapFactory.decodeResource(res, R.mipmap.ic_launcher_foreground);
Bitmap mint = BitmapFactory.decodeResource(res, R.mipmap.ic_launcher_foreground);
Random rand = new Random();
int x=500;
int y=500;
int radius=250;
Paint paint=new Paint();
// Use Color.parseColor to define HTML colors
paint.setColor(Color.parseColor("#CD5C5C"));
canvas.drawCircle(x,x, radius, paint);
paint.setColor(Color.parseColor("#CD000C"));
canvas.drawRect (250,250, 750, 750, paint);
canvas.drawBitmap(bitmap, 0, 0, paint);
int xx[]=new int[15];;
int yy[]=new int[15];;
boolean dur=false;
for (int i=0; i<10; i++) {
dur=false;
int x1 = rand.nextInt(500) + 250;
int y1 = rand.nextInt(500) + 250;
x1=750;
y1=750;
canvas.drawBitmap(brocoli, x1-(brocoli.getWidth()/density), y1-(brocoli.getHeight()/density), paint);
x1=250;
y1=250;
canvas.drawBitmap(brocoli, x1-(brocoli.getWidth()/density), y1-(brocoli.getHeight()/density), paint);
}
}
}
=============================================== ================================
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5sp"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView2"
android:layout_width="776dp"
android:layout_height="1154dp"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher" />
<com.androidtestapp.myviewx
android:id="@+id/xview"
android:layout_width="400dp"
android:layout_height="400dp"
/>
<ImageView
android:id="@+id/imageView1"
android:layout_width="776dp"
android:layout_height="1154dp"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@mipmap/ic_launcher_round" />
</RelativeLayout>
答案 1 :(得分:0)
我尝试你的代码和自定义视图类给了我空指针错误我解决了它。 所以你提到的没有任何ClassCastException 您希望在顶部和两个图像视图之间自定义图像视图 因此,请尝试按如下所示更改xml布局
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/pax_icon" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_marginLeft="0px"
android:layout_marginTop="0px"
android:layout_gravity="bottom"
android:scaleType="fitXY"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="@drawable/preferred_airline_icon" />
<com.demoapplication.customView.myviewx
android:id="@+id/xview"
android:layout_width="400dp"
android:layout_gravity="center"
android:layout_height="400dp"
/>
</FrameLayout>
通过使用FrameLayout,我们可以获得彼此之上的视图 因此,请尝试按照上面的布局更改视图的顺序
答案 2 :(得分:0)
删除您的R文件,然后重新编译。 那应该解决它。 R文件是自动生成的,所以不用担心。它位于/project/gen/package_name/R.java
下