2016-12-27 68 views
0

我使用Android Studio创建游戏。到目前为止,一切都还好,但是今天运行我的应用程序后,它开始活动后崩溃。我搜索了很长时间的错误,但我没有找到任何东西。开始MatheGame活动后Android游戏崩溃

MatheGame.java:

import android.graphics.Point; 
import android.os.Handler; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.Display; 
import android.view.KeyEvent; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.WindowManager; 
import android.widget.FrameLayout; 
import android.widget.ImageView; 
import android.widget.TextView; 
import java.util.Timer; 
import java.util.TimerTask; 

public class MatheGame extends AppCompatActivity { 

private TextView scoreLabel; 
private TextView startLabel; 
private ImageView box; 
ImageView orange; 

// Size 
private int frameHeight; 
private int boxSize; 
private int screenWidth; 
private int screenHeight; 

// Position 
private int boxY; 
private int orangeX; 
private int orangeY; 

// Speed 
private int boxSpeed; 
private int orangeSpeed; 


// Score 
private int score = 0; 


// Initialize Class 
private Handler handler = new Handler(); 
private Timer timer = new Timer(); 


// Status Check 
private boolean action_flg = false; 
private boolean start_flg = false; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    scoreLabel = (TextView) findViewById(R.id.scoreLabel); 
    startLabel = (TextView) findViewById(R.id.startLabel); 
    box = (ImageView) findViewById(R.id.box); 
    orange = (ImageView)findViewById(R.id.orange); 


    // Get screen size. 
    WindowManager wm = getWindowManager(); 
    Display disp = wm.getDefaultDisplay(); 
    Point size = new Point(); 
    disp.getSize(size); 

    screenWidth = size.x; 
    screenHeight = size.y; 


    boxSpeed = Math.round(screenHeight/60); 
    orangeSpeed = Math.round(screenWidth/60); 

    orange.setX(-80); 
    orange.setY(-80); 


    scoreLabel.setText("Score : 0"); 

} 


public void changePos() { 

    hitCheck(); 

    // Orange 
    orangeX -= orangeSpeed; 
    if (orangeX < 0) { 
     orangeX = screenWidth + 20; 
     orangeY = (int) Math.floor(Math.random() * (frameHeight - orange.getHeight())); 
    } 
    orange.setX(orangeX); 
    orange.setY(orangeY); 


    // Move Box 
    if (action_flg == true) { 
     // Touching 
     boxY -= boxSpeed; 
    } else { 
     // Releasing 
     boxY += boxSpeed; 
    } 
    // Check box position. 
    if (boxY < 0) boxY = 0; 
    if (boxY > frameHeight - boxSize) boxY = frameHeight - boxSize; 
    box.setY(boxY); 
    scoreLabel.setText("Score : " + score); 
} 


public void hitCheck() { 

    // Orange 
    int orangeCenterX = orangeX + orange.getWidth()/2; 
    int orangeCenterY = orangeY + orange.getHeight()/2; 

    if (0 <= orangeCenterX && orangeCenterX <= boxSize && 
      boxY <= orangeCenterY && orangeCenterY <= boxY + boxSize) { 
     score += 10; 
     orangeX = -10; 
    } 

} 

public boolean onTouchEvent(MotionEvent me) { 


    if (start_flg == false) { 
     start_flg = true; 


     FrameLayout frame = (FrameLayout) findViewById(R.id.frame); 
     frameHeight = frame.getHeight(); 
     boxY = (int)box.getY(); 
     boxSize = box.getHeight(); 



     startLabel.setVisibility(View.GONE); 


     timer.schedule(new TimerTask() { 

      @Override 

      public void run() { 

       handler.post(new Runnable() { 
        @Override 
        public void run() { 
         changePos(); 
        } 

       }); 

      } 

     }, 0, 20); 

    } else { 

     if (me.getAction() == MotionEvent.ACTION_DOWN) { 

      action_flg = true; 

     } else if (me.getAction() == MotionEvent.ACTION_UP) { 

      action_flg = false; 
     } 
    } 
    return true; 
} 

@Override 
public boolean dispatchKeyEvent(KeyEvent event) { 

    if (event.getAction() == KeyEvent.ACTION_DOWN) { 
     switch (event.getKeyCode()) { 

      case KeyEvent.KEYCODE_BACK: 
       return true; 
     } 
    } 
    return super.dispatchKeyEvent(event); 
} 

} 

activity_mathe_game.xml:

<?xml version="1.0" encoding="utf-8"?> 
<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="com.eitisoft.mathetrainer.MatheGame"> 

<TextView 
    android:id="@+id/scoreLabel" 
    android:layout_width="match_parent" 
    android:layout_height="50dp" 
    android:text="Score : 300" 
    android:textSize="18sp" 
    android:paddingLeft="10dp" 
    android:gravity="center_vertical"/> 

<FrameLayout 
    android:id="@+id/frame" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <TextView 
     android:id="@+id/startLabel" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Tap to Start" 
     android:textSize="30sp" 
     android:layout_gravity="center_horizontal" 
     android:layout_marginTop="130dp"/> 

    <ImageView 
     android:id="@+id/box" 
     android:layout_width="50dp" 
     android:layout_height="50dp" 
     android:src="@drawable/box" 
     android:layout_gravity="center_vertical"/> 

    <ImageView 
     android:id="@+id/orange" 
     android:layout_width="20dp" 
     android:layout_height="20dp" 
     android:src="@drawable/punkt"/> 


</FrameLayout> 


</RelativeLayout> 

错误:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setX(float)' on a null object reference 
               at com.eitisoft.mathetrainer.MatheGame.onCreate(MatheGame.java:85) 

回答

1

你设置错了布局文件。所以ImageView是空的,如果你打算在空引用上使用任何属性,那么它会崩溃。更新您的onCreate

setContentView(R.layout.activity_mathe_game); 

而不是

setContentView(R.layout.activity_main); 
+0

OMG ......这样一个简单的错误...谢谢:) –

+0

欢迎计算器。如果它解决了您的问题,您可以接受此答案。 – Rahul