自定义视图导致我的活动无法打开

时间:2015-07-26 01:46:23

标签: android android-custom-view

我是Android的初学者,已经开始学习项目。我使用自己的自定义视图和实现ScaleGestureDetector很困难。当我尝试通过另一个活动的意图打开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)

活动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);
    }
}

要在活动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;
        }
    }
}

由gameScreen.java实现的名为game_screen.xml的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>

这里有很多内容,但这对你们大家来说都很简单。任何帮助将不胜感激。谢谢。

1 个答案:

答案 0 :(得分:1)

好像您忘记在package com.games.michael.treasureseeker;文件的顶部添加MapView.java