2014-12-06 78 views
0

該應用程序崩潰第二我打開它...它說它由於NullPointerException崩潰,但我無法弄清楚爲什麼。應用程序崩潰....空指針異常

這是logcat的:

12-06 08:37:58.217: D/AndroidRuntime(1138): Shutting down VM 
12-06 08:37:58.217: W/dalvikvm(1138): threadid=1: thread exiting with uncaught exception ( (group=0xb2aeaba8) 
12-06 08:37:58.247: E/AndroidRuntime(1138): FATAL EXCEPTION: main 
12-06 08:37:58.247: E/AndroidRuntime(1138): Process: com.example.batandball, PID: 1138 
12-06 08:37:58.247: E/AndroidRuntime(1138): java.lang.RuntimeException: Unable to start activity  ComponentInfo{com.example.batandball/com.example.batandball.MainActivity}: java.lang.NullPointerException 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.os.Handler.dispatchMessage(Handler.java:102) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.os.Looper.loop(Looper.java:136) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at java.lang.reflect.Method.invokeNative(Native Method) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at java.lang.reflect.Method.invoke(Method.java:515) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at dalvik.system.NativeStart.main(Native Method) 
12-06 08:37:58.247: E/AndroidRuntime(1138): Caused by: java.lang.NullPointerException 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.example.batandball.brickArr.duplicate(brickArr.java:15) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.example.batandball.brickArr.<init>(brickArr.java:9) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.example.batandball.MainActivity.runable(MainActivity.java:29) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at com.example.batandball.MainActivity.onCreate(MainActivity.java:17) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.Activity.performCreate(Activity.java:5231) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
12-06 08:37:58.247: E/AndroidRuntime(1138):  ... 11 more 
12-06 08:38:03.467: I/Process(1138): Sending signal. PID: 1138 SIG: 9 

這是在MainActivity:

public class MainActivity extends Activity { 
private Paddle paddle; 

@SuppressLint("NewApi") @Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    runable(); 
} 

public void runable() 
{ 
    Point p=new Point(); 
    this.getWindowManager().getDefaultDisplay().getSize(p); 
    int screenW=p.x; 
    int screenH=p.y; 
    paddle=new Paddle(screenW/2,(float)(0.85*screenH),screenW, 30,135); 
    Ball ball=new Ball(screenW/2,screenH/3,20,screenW,screenH,paddle); 
    Brick brick=new Brick((float)0,(float)0,(float)(screenH/16),(float)(screenW/7)); 
    brickArr m=new brickArr(brick); 
    ballView bv=new ballView(this,ball); 
    PaddleView pv=new PaddleView(this,paddle); 
    BrickView brv=new BrickView(this,m.getB()); 
    gameView gv=new gameView(this); 
    gv.setbv(bv,pv,brv); 
    setContentView(gv); 
    ballMover ballmove=new ballMover(gv,ball,paddle); 
    PaddleMover paddlemove=new PaddleMover(gv,paddle); 
    brickMover brickmover=new brickMover(gv,m.getB(),ball); 
    brickmover.start(); 
    ballmove.start(); 
    paddlemove.start(); 

} 
public boolean onTouchEvent(MotionEvent event) 
{ 
    if (event.getX()>=this.paddle.left()-this.paddle.screenW/16&&event.getX() <=this.paddle.right()+this.paddle.screenW/16) 
     this.paddle.setdest(event.getX()); 
    return true; 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 
} 

這是brickArr方法:

public class brickArr { 
private Brick brick; 
private Brick[] b; 
public brickArr(Brick brick) { 
    this.brick=brick; 
    this.b=new Brick[36]; 
    duplicate(b); 
    } 
public void duplicate(Brick[] b) 
{ 
    for(int i=0;i<b.length-1;i++) 
    { 
     b[i].leftx=brick.weidth*(i%7); 
     b[i].topy=brick.height*(i/7); 
     b[i].height=brick.height; 
     b[i].weidth=brick.weidth; 
    } 
    b[35].leftx=0; 
    b[35].topy=0; 
    b[35].height=0; 
    b[35].weidth=0; 
    } 
public Brick[] getB() 
{ 
    return b; 
} 


} 

對不起,如果它是一個愚蠢的問題,但我真的不知道有什麼問題。

+1

'this.b =新磚[36];'這創建一個數組,可以持有36個磚塊對象,但仍需要初始化它們(請記住,對象的默認值爲空)。 – 2014-12-06 13:59:41

+0

@omer:如果您的Brick數組在類中使用,則不應將其作爲參數發送給您的函數。你的'b'已經爲全班所知,所以刪除你的參數'duplicate' – Pphoenix 2014-12-06 14:01:25

+0

是的,但我在重複(b)方法 – omer 2014-12-06 14:02:26

回答

1

您磚數組元素是未初始化,即bricks[i]

更改您的for循環來初始化它喜歡:

for(int i=0;i<b.length-1;i++) 
{ 
    b[i] = new Brick();//call appropriate c'tor what is defined by you. 
    b[i].leftx=brick.weidth*(i%7); 
    b[i].topy=brick.height*(i/7); 
    b[i].height=brick.height; 
    b[i].weidth=brick.weidth; 
}