-6
我試圖改變活動時rotateRight方法exectued,但我發現NullPointerException異常意圖
顯示java.lang.NullPointerException改變活動時:試圖調用虛擬方法 「的java.lang .String android.content.Context.getPackageName()」上一個空 對象引用
不能完全確定,如果我碰到的正確位置的背景下,我已經創建了一個對象 - 請注意,我已刪除一些代碼。
主要遊戲活動類
public class Game extends Activity {
private NetwalkGrid grid;
public int[]mGridSolved;
private static Context context;
private static Context mContext;
private int numOfMoves;
static Intent intentlol;
NetWalkView mGameView;
Intent intent;
View v;
static int gDiff;
static String diffS;
public Game() {
//hello
}
@Override
public void onCreate(Bundle savedInstanceState) {
intent = getIntent();
gDiff = intent.getIntExtra("difficulty", 1);
diffS = intent.getStringExtra("difficultyDif");
mGameView = new NetWalkView(getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(mGameView);
context = Game.this; // Need to get Activity content
}
public Game(int gDifff) {
System.out.println(gDiff);
gDifff = gDiff;
switch (gDifff) {
case 1:
grid = new NetwalkGrid(4,3); // easy
break;
case 2:
grid = new NetwalkGrid(5,5); // medium
break;
case 3:
grid = new NetwalkGrid(8,7); // medium
break;
}
}
public void rotateRight(int col, int row) {
grid.rotateRight(col, row);
numOfMoves++;
checkForWin();
Intent intent = new Intent(this, MenuView.class);
startActivity(intent);
}
視圖類的遊戲活動
public class NetWalkView extends View {
public NetWalkView(Context context) {
super(context);
init();
mGestureDetector = new GestureDetector(context, new MyGestureListener());
}
Paint mGridPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint mBGPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
Paint textPaint = new Paint();
//Button home = new Button(this);
private Game game = new Game(1);
private GestureDetector mGestureDetector;
private void init() {
game.scrambleGrid(); // Scrambles the grid
textPaint.setColor(Color.BLACK);
textPaint.setTextAlign(Paint.Align.CENTER);
textPaint.setTextSize(100);
}
public boolean onTouchEvent (MotionEvent ev) {
this.mGestureDetector.onTouchEvent(ev);
return super.onTouchEvent(ev);
}
class MyGestureListener extends GestureDetector.SimpleOnGestureListener {
public boolean onDown(MotionEvent ev) {
return true;
}
public void onLongPress(MotionEvent ev) {
try {
game.rotateRight(columX, columY);
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Please click on the grid");
}
invalidate(); // To re draw the grid
}
}
切勿使用new運算符與活動派生類 – Selvin