我是android的新手,并且使用Eclipse(基本java和Netbeans中的Background)
我只是关注基本的游戏音色:
并出现以下错误:
活动无法解析为某种类型。
((Activity)getContext()).finish();
上面是它出错的代码,我试过看谷歌,这里也没有真正的帮助,或者至少我明白了。
以下是全班:
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) {
}
}
我读过这可能意味着我的Manifest不对,下面是XML。
<?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>
提前感谢您的帮助!
答案 0 :(得分:2)
好像你没有导入Activity
类。将此行添加到您的import语句(在class
声明上方):
import android.app.Activity;