Android应用程序Null指针异常

时间:2015-09-20 14:29:03

标签: java android nullpointerexception

我正在尝试构建一个新手应用程序,它从屏幕顶部删除一组披萨主题,用户应该避免它们,但由于某种原因,我在logcat中得到NullPointerException并且应用程序崩溃了。 。 它说它发生在firstSet()中,当我试图将值放入point.y时没有意义,因为我在约束器中初始化它们。

package com.example.secondapp;

import java.util.Random;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.view.View;

public class EnemyTopics extends View {
private Drawable[] topic;
private Point[] cords;
private int width,height,screenHgt,screenWid,ClosestTopic,score;
private int tomatos,olives,mushroom,onion;
private UserCharacter character;
private Context context;

@SuppressLint("NewApi")
public EnemyTopics(Context context,int wid,int hgt,int screenHgt,int screenWid,UserCharacter uc) {
    super(context);
    this.cords = new Point[20];
    for(Point point : cords) {
        point = new Point();
        point.x = 0;
        point.y = 0;
    }           
    this.width = wid;
    this.height = hgt;
    this.topic = new Drawable[20];
    this.tomatos = R.drawable.tomatos;
    this.olives = R.drawable.olives;
    this.mushroom = R.drawable.mushroom;
    this.onion = R.drawable.onion;
    this.ClosestTopic = 0;
    this.screenHgt = screenHgt;
    this.screenWid = screenWid;
    this.character = uc;
    this.score = 0;
    this.context = context;
    this.firstSet();
    this.setBounds();
}

public int maxLeft(int position) {
    return this.cords[position].x - (this.width/2);
}
public int maxRight(int position) {
    return this.cords[position].x + (this.width/2);
}
public int maxTop(int position) {
    return this.cords[position].y - (this.height/2);
}
public int maxBottom(int position) {
    return this.cords[position].y + (this.height/2);
}
public void setBounds() {
    int pos = 0;
    for (Drawable topics : this.topic) {
        topics.setBounds(maxLeft(pos), maxTop(pos), maxRight(pos), maxBottom(pos));
        pos++;
    }
}
public void moveTopics() {
    int pos = 0;
    for (Point p : this.cords) {
        if(maxBottom(pos) <= screenHgt) {
            p.y++;
        }
        else {
            p.y = -200;
        }
        pos++;
    }
    if(maxTop(ClosestTopic) > character.maxBottom()) {
        ClosestTopic++;
        score++;
    }
    if(ClosestTopic <= 19)
        ClosestTopic = 0;
}
public void checkHit() {
    if(maxBottom(ClosestTopic) >= character.maxTop() && maxTop(ClosestTopic) <= character.maxBottom() && maxLeft(ClosestTopic) <= character.maxRight() && maxRight(ClosestTopic) >= character.maxLeft()) {
        score = 0;
        this.firstSet();
        character.restart();
    }
}
@SuppressLint("NewApi")
public void firstSet() {
    Random r = new Random();
    int random = r.nextInt(4);
    int pos = 0;
    for (Point p : cords) {
        p.y = (0 - pos*15);
        p.x = screenWid/2;
        switch (random) {
        case 0:
            topic[pos]= context.getResources().getDrawable(tomatos,null);   
            break;
        case 1:
            topic[pos]= context.getResources().getDrawable(olives,null);    
            break;
        case 2:
            topic[pos]= context.getResources().getDrawable(mushroom,null);  
            break;
        case 3:
            topic[pos]= context.getResources().getDrawable(onion,null); 
            break;
        default:
            topic[pos]= context.getResources().getDrawable(tomatos,null);   
        }
        pos++;
        random = r.nextInt(4);
    }
}
public void draw (Canvas canvas) {
    for (Drawable topics : this.topic) {
        topics.draw(canvas);
    }
}

}

我禁用了移动它们的线程,这意味着问题是在构造函数,firstSet或setBound上,但我无法找到它,所以我会appriciate帮助:) 提前谢谢。

logcat:
09-20 14:13:53.725: E/AndroidRuntime(1154): FATAL EXCEPTION: main
09-20 14:13:53.725: E/AndroidRuntime(1154): Process: com.example.secondapp, PID: 1154
09-20 14:13:53.725: E/AndroidRuntime(1154): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.secondapp/com.example.secondapp.MainActivity}: java.lang.NullPointerException: Attempt to write to field 'int android.graphics.Point.y' on a null object reference
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread.-wrap11(ActivityThread.java)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.os.Handler.dispatchMessage(Handler.java:102)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.os.Looper.loop(Looper.java:148)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread.main(ActivityThread.java:5417)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at java.lang.reflect.Method.invoke(Native Method)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
09-20 14:13:53.725: E/AndroidRuntime(1154): Caused by: java.lang.NullPointerException: Attempt to write to field 'int android.graphics.Point.y' on a null object reference
09-20 14:13:53.725: E/AndroidRuntime(1154):     at com.example.secondapp.EnemyTopics.firstSet(EnemyTopics.java:96)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at com.example.secondapp.EnemyTopics.<init>(EnemyTopics.java:42)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at com.example.secondapp.MainActivity.onCreate(MainActivity.java:19)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.Activity.performCreate(Activity.java:6237)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
09-20 14:13:53.725: E/AndroidRuntime(1154):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
09-20 14:13:53.725: E/AndroidRuntime(1154):     ... 9 more

1 个答案:

答案 0 :(得分:5)

使用这种循环不能将对象分配给数组。 point是引用的副本,不会将其复制回数组。

this.cords = new Point[20];
for(Point point : cords) {
    point = new Point();  
    point.x = 0;
    point.y = 0;
}

使用“老式”方式:

this.cords = new Point[20];
for(int i = 0; i < cords.length; ++i ) {
    cords[i] = new Point();  
    cords[i].x = 0;
    cords[i].y = 0;
}