2017-02-19 69 views
-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 
     } 
    } 
+1

切勿使用new运算符与活动派生类 – Selvin

回答

-2

使用活动情境,而不是这个。

用途:

Intent intent = new Intent(Game.this, MenuView.class); 
     startActivity(intent); 

相反的:

Intent intent = new Intent(this, MenuView.class); 
     startActivity(intent);