I have an action that invokes a custom view when a button is clicked. The custom view works fine until I try to add an ImageView to it. I tried this in my xml, which is called in my main action using setContentView:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.mypackage.mycustomview android:id="@+id/fbv" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/pistolView" android:src="@drawable/pistol" android:layout_width="wrap_content" android:layout_height="wrap_content"/> </com.mypackage.mycustomview>
I get a ClassCastException when I click on a button in my main activity that triggers this custom view. All I want to do is make a clickable image inside my user view.
ImageView displays and clicks fine if I put it in my main activity and main.xml as follows:
<Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Play Vs CPU" /> <ImageView android:id="@+id/pistolView" android:src="@drawable/pistol" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
I'm really bad at xml layouts in android, so I have no idea what I'm missing here. Any help is appreciated.
here is the mycustomview class:
package com.mypackage; import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.content.res.Resources; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.os.Handler; import android.os.Message; import android.text.format.Time; import android.util.AttributeSet; import android.util.Log; import android.view.KeyEvent; import android.view.LayoutInflater; import android.view.View; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class mycustomview extends View{ final Paint mPaint = new Paint(); private Context mContext; private Resources res = getResources(); private GameControls _controls; private GameJoystick _joystick; private GameJoystick Rjoystick; Paint paint = new Paint(); private long currTime; private Time time = new Time(); private Hero mHero; private Gun mGun; private Bitmap heroBit; private float possibleX; private float possibleY; private float lazerX; private float lazerY; public ArrayList<Wall> wallList = new ArrayList<Wall>(); private Canvas mCanvas; private Bitmap splat; private Bitmap pistolBit; private Bitmap building; private ImageView pistol; private int i = 0; private int w = 0; Wall wall; private RefreshHandler mRedrawHandler = new RefreshHandler(); class RefreshHandler extends Handler { @Override public void handleMessage(Message msg) {
source share