2015-09-20 100 views
0

我想構建一個新手應用程序,從屏幕頂部刪除比薩餅主題的數組,並且用戶應該避免它們,但由於某種原因,我在logcat中收到NullPointerException和應用程序崩潰..它說它發生在第一個Set()當我試圖把價值的point.y沒有任何意義,因爲我正在初始化他們在約束。Android應用程序空指針異常

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但我不能找到它,所以我會幫助並欣賞:) 感謝提前。

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

在這裏發表您的堆棧跟蹤,你在logcat中得到 – cafebabe1991

回答

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; 
}   
+0

謝謝,我以前瞭解每個4年,忘了:) – sagi