Master Sketchiggle
Master Sketchiggle

Reputation: 93

Android - Activity cannot be resolved to a type

I'm new to android, and using Eclipse (Background in basic java and Netbeans)

I'm just following a basic game tute on:

http://www.javacodegeeks.com/

And have the following error:

Activity cannot be resolved to a type.

((Activity)getContext()).finish();

Above is the code it is having an error on, I've tried look on Google and here also will no real help, or at least that I understood.

Here is the whole class:

package Sketchy.Game.WarSoNawty;

import android.util.Log;
import android.content.Context;
import android.graphics.Canvas;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class MainGamePanel extends SurfaceView implements
    SurfaceHolder.Callback {

private static final String TAG = MainGamePanel.class.getSimpleName();

private MainThread thread;

public MainGamePanel(Context context) {
    super(context);
    //adding the callback (this) to the surface holder to intercept events
    getHolder().addCallback(this);

    //create game loop
    thread = new MainThread(getHolder(), this);

    //make the GamePanel focusable so it can handle events
    setFocusable(true);
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    thread.setRunning(true);
    thread.start();

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    boolean retry = true;
    while (retry) {
        try {
            thread.join();
            retry = false;
    }
    catch (InterruptedException e) {
        //try again shutting down the thread
    }
    }
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    if (event.getAction() == MotionEvent.ACTION_DOWN) {
        if(event.getY() > getHeight() - 50) {
            thread.setRunning(false);
            ((Activity)getContext()).finish();
        }
        else {
            Log.d(TAG, "Coords: x=" +event.getX() + ",y=" + event.getY());
        }
    }
    return super.onTouchEvent(event);
}

@Override
protected void onDraw(Canvas canvas) {

}

}

I've read that is could mean my Manifest isn't right, below is the XML for that.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="Sketchy.Game.WarSoNawty"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".WarSoNawty"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Thanks in advance for any help!

Upvotes: 1

Views: 8771

Answers (1)

Hadi Satrio
Hadi Satrio

Reputation: 4292

Seems like you haven't imported the Activity class. Add this line to your import statements (just above the class declaration) :

import android.app.Activity;

Upvotes: 2

Related Questions