Custom View won't inflate properly

130 views Asked by At

Trying to build an android game with a map (right now we're just trying to make it draw a circle though) and the view we created won't inflate properly, the app keeps crashing

The activity

package edu.virignia.cs2110.dlf4mf.ghostapocalypse;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;    
import android.view.MenuItem;

public class GameActivity extends Activity {

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_game);
      MapView mv = (MapView)findViewById(R.id.map_view);

   }
}

The activity layout

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   tools:context="${relativePackage}.${activityClass}" >

   <MapView
       android:id="@+id/map_view"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"/>

</RelativeLayout>

The custom view

package edu.virignia.cs2110.dlf4mf.ghostapocalypse;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.SurfaceView;
import android.view.View;

public class MapView extends SurfaceView {

    private final Paint mPaint = new Paint();

    public MapView(Context context) {
        super(context);
        init();
    }

    public MapView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public MapView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        int h = 500;
        int w = 500;
        Canvas grid = new Canvas(Bitmap.createBitmap(h,w, Bitmap.Config.ARGB_8888));
        draw(grid);
    }

    @Override
    protected void onDraw(Canvas canvas) {
         super.onDraw(canvas);
         mPaint.setStyle(Paint.Style.FILL);
         int c = R.color.material_deep_teal_500;
         mPaint.setColor(c);
         canvas.drawCircle(250, 250, 150, mPaint);
    }

}
1

There are 1 answers

0
Kevin Coppock On BEST ANSWER

Replace <MapView in your XML layout with <edu.virignia.cs2110.dlf4mf.ghostapocalypse.MapView. The class name for a custom view must be fully-qualified.