自定义视图导致我的 activity 打不开
Custom View causing my activity not to open
我是 Android 的完全初学者,并且已经开始学习一个项目。我在使用自己的自定义视图和实施 ScaleGestureDetector 时遇到很大困难。目前,当我尝试通过另一个 activity 的意图打开 gameScreen 时,我在屏幕上看到了这个:
不幸的是,com.games.michael.* 已经停止。
在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);
}
}
将在 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 布局由 gameScreen.java 实现,名为 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 清单:
<?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>
这里有很多内容,但这对你们所有的专家来说都是简单的东西。任何帮助将不胜感激。谢谢。
您似乎忘记在 MapView.java
文件的顶部添加 package com.games.michael.treasureseeker;
。
我是 Android 的完全初学者,并且已经开始学习一个项目。我在使用自己的自定义视图和实施 ScaleGestureDetector 时遇到很大困难。目前,当我尝试通过另一个 activity 的意图打开 gameScreen 时,我在屏幕上看到了这个: 不幸的是,com.games.michael.* 已经停止。
在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);
}
}
将在 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 布局由 gameScreen.java 实现,名为 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 清单:
<?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>
这里有很多内容,但这对你们所有的专家来说都是简单的东西。任何帮助将不胜感激。谢谢。
您似乎忘记在 MapView.java
文件的顶部添加 package com.games.michael.treasureseeker;
。