I am a complete beginner at Android and have taken up a project to learn. I am having great difficulty using my own custom view and implementing ScaleGestureDetector. At the moment I am getting this on the screen when I attempt to open gameScreen via an intent from another activity: Unfortunately, com.games.michael.* has stopped.
In the LogCat:
01-16 07:31:39.200 22726-22726/com.games.michael.treasureseeker
D/AndroidRuntime﹕ Shutting down VM
01-16 07:31:39.200 22726-22726/com.games.michael.treasureseeker W/dalvikvm﹕
threadid=1: thread exiting with uncaught exception
(group=0x40aa4228)
01-16 07:31:39.260 22726-22726/com.games.michael.treasureseeker
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.games.michael.treasureseeker/com.games.michael.treasureseeker.GameScreen}: android.view.InflateException: Binary XML file line #27: Error inflating class com.games.michael.treasureseeker.MapView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240)
Activity gameScreen.java:
package com.games.michael.treasureseeker;
import android.app.Activity;
import android.os.Bundle;
import java.util.Random;
public class GameScreen extends Activity {
private MapView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game_screen);
}
}
Custom View to be used in activity MapView.java:
import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ScaleGestureDetector;
import android.widget.ImageView;
public class MapView extends ImageView {
private ScaleGestureDetector SGD;
private float scale = 1.f;
public MapView(Context context) {
super(context);
SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
}
public MapView(Context context, AttributeSet attrs) {
super(context, attrs);
SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
}
public MapView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
SGD = new ScaleGestureDetector(getContext(), new ScaleListener());
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
SGD.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
private class ScaleListener extends
ScaleGestureDetector.SimpleOnScaleGestureListener {
@Override
public boolean onScale(ScaleGestureDetector detector) {
scale *= detector.getScaleFactor();
scale = Math.max(0.1f, Math.min(scale, 5.0f));
//matrix.setScale(scale, scale);
//iv.setImageMatrix(matrix);*/
invalidate();
return true;
}
}
}
XML layout implemented by gameScreen.java named game_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:id="@+id/gameBackground">
<TextView
android:id="@+id/directions"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="@color/white"
android:background="@color/black"
android:paddingLeft="6dip"
android:paddingRight="6dip"
/>
<com.games.michael.treasureseeker.MapView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/imageView"
android:src="@drawable/pirate_map_2"
android:layout_below="@+id/directions"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Android Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.games.michael.treasureseeker" >
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="18"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
>
<activity
android:name="com.games.michael.treasureseeker.MainActivity"
android:configChanges="orientation|keyboardHidden|screenSize"
android:screenOrientation="portrait"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GameScreen"
android:screenOrientation="landscape">
</activity>
<activity android:name=".ResultsScreen"
android:screenOrientation="portrait">
</activity>
</application>
</manifest>
attrs_map_view.xml:
<resources>
<declare-styleable name="MapView">
<attr name="exampleString" format="string" />
<attr name="exampleDimension" format="dimension" />
<attr name="exampleColor" format="color" />
<attr name="exampleDrawable" format="color|reference" />
</declare-styleable>
</resources>
There is a lot of content here but this is simple stuff for all of you gurus. Any help would be greatly appreciated. Thank you.
Seems like you've forgotten to add the package com.games.michael.treasureseeker;
at the top of MapView.java
file.