2017-06-29 1 views
0

Kann jemand helfen? Ich habe versucht, eine Parallax-Bildansicht in Android-Studio auszuführen, aber eine Ausnahmebedingungsnachricht erhalten, die besagt "Versuch, virtuelle Methode auf eine Null-Objektreferenz aufzurufen". Es stellt sogar klar, dass es sich um eine NullPointerException handelt. Die Nachricht wurde blau hervorgehoben, wo sie aufgetreten zu sein scheint.Versuch, virtuelle Methode für eine Nullobjektverweis (NullpointerException) aufzurufen

Dies ist in Zeile:

backgrounds.add(new Background(this.context, screenWidth, screenHeight, "skyline", 0, 80, 50)); 

in meiner ParallaxView Klasse.

Und an der Linie:

setContentView(parallaxView); 

in meiner ParallaxActivity Klasse.

Also, ich erwarte, dass dies ist, wo die Ausnahme auftritt. Ich kopiert und eingefügt die vollständige Ausnahmemeldung und die Klassen unter:

06-28 20:11:48.297 2420-2420/com.hfad.parallaxproject E/AndroidRuntime: FATAL EXCEPTION: main 
Process: com.hfad.parallaxproject, PID: 2420 
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.hfad.parallaxproject/com.hfad.parallaxproject 
.ParallaxActivity}: java.lang.NullPointerException: 
Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference  
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                  
Caused by: java.lang.NullPointerException: 
Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference 
at com.hfad.parallaxproject.ParallaxView.<init>(ParallaxView.java:49) 
at com.hfad.parallaxproject.ParallaxActivity.onCreate(ParallaxActivity.java:25) 
at android.app.Activity.performCreate(Activity.java:6662) 
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:111 
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599 
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
at android.app.ActivityThread.-wrap12(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6077) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)  

Dies sind die Klassen, die die beiden Fehler in aufgetreten: 1.

package com.hfad.parallaxproject; 

import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Rect; 
import android.view.SurfaceHolder; 
import android.view.SurfaceView; 
import java.util.ArrayList; 

public class ParallaxView extends SurfaceView implements Runnable { 

ArrayList<Background> backgrounds; 

private volatile boolean running; 
private Thread gameThread = null; 

//For drawing 
private Paint paint; 
private Canvas canvas; 
private SurfaceHolder ourHolder; 

//Holds a reference to the Activity 
Context context; 

//Control the fps 
long fps = 60; 

//Screen resolution 
int screenWidth; 
int screenHeight; 

ParallaxView(Context context, int screenWidth, int screenHeight) { 
    super(context); 

    this.context = context ; 

    this.screenWidth = screenWidth; 
    this.screenHeight = screenHeight; 

    //Initialize our drawing objects 
    ourHolder = getHolder(); 
    paint = new Paint(); 

    //load the background data into the Background objects and 
    //place them in our GameObject arraylist 

    backgrounds.add(new Background(this.context, screenWidth, screenHeight, "skyline", 0, 80, 50)) ; 
    backgrounds.add(new Background(this.context, screenWidth, screenHeight, "grass", 70, 110, 200)) ; 
    //Add more backgrounds here 
} 

private void drawBackground(int position) { 

    //Make a copy of the relevant background 
    Background bg = backgrounds.get(position) ; 

    //define what portion of images to capture and 
    //what coordinates of screen to draw them at 

    //For the regular bitmap 
    Rect fromRect1 = new Rect(0, 0, bg.width - bg.xClip, bg.height); 
    Rect toRect1 = new Rect(bg.xClip, bg.startY, bg.width, bg.endY); 

    //For the reversed background 
    Rect fromRect2 = new Rect(bg.width - bg.xClip, 0, bg.width, bg.height); 
    Rect toRect2 = new Rect(0, bg.startY, bg.xClip, bg.endY) ; 

    //draw the two background bitmaps 
    if(!bg.reversedFirst) { 
     canvas.drawBitmap(bg.bitmap, fromRect1, toRect1, paint) ; 
     canvas.drawBitmap(bg.bitmapReversed, fromRect2, toRect2, paint); 
    } else { 
     canvas.drawBitmap(bg.bitmap, fromRect2, toRect2, paint) ; 
     canvas.drawBitmap(bg.bitmapReversed, fromRect1, toRect1, paint) ; 
    } 

} 

@Override 
public void run() { 

    while(running) { 
     long startFrameTime = System.currentTimeMillis(); 

     update(); 

     draw(); 

     //Calculate the fps this frame 
     long timeThisFrame = System.currentTimeMillis() - startFrameTime; 
     if (timeThisFrame >= 1) { 
      fps = 1000/timeThisFrame; 
     } 
    } 
} 

private void update() { 
    //Update all the background positions 
    for(Background bg : backgrounds) { 
     bg.update(fps) ; 
    } 
} 

private void draw() { 
    if(ourHolder.getSurface().isValid()) { 
     //First we lock the area of memory we will be drawing to 
     canvas = ourHolder.lockCanvas(); 

     //draw a background color 
     canvas.drawColor(Color.argb(255, 0, 3, 70)); 

     //Draw the background parallax 
     drawBackground(0) ; 

     //Draw the rest of the game 
     paint.setTextSize(60); 
     paint.setColor(Color.argb(255, 255, 255, 255)); 
     canvas.drawText("I am a plane", 350, screenHeight/100 * 5, paint); 
     paint.setTextSize(220); 
     canvas.drawText("I'm a train", 50, screenHeight/100 * 80, paint); 

     drawBackground(1) ; 
     //Draw the foreground parallax 

     //Unlock and draw the scene 
     ourHolder.unlockCanvasAndPost(canvas); 
    } 

} 

//Clean up our thread if the game is stopped 
public void pause() { 
    running = false; 
    try{ 
     gameThread.join(); 
    } catch (InterruptedException e) { 
     //Error 
    } 
} 

//Make a new thread and start it 
//Execution moves to our run method 

public void resume() { 
    running = true; 
    gameThread = new Thread(this); 
    gameThread.start() ; 
} 
} //End of ParallaxView Class 

Und 2.

package com.hfad.parallaxproject; 

import android.app.Activity; 
import android.graphics.Point; 
import android.os.Bundle; 
import android.view.Display; 

public class ParallaxActivity extends Activity { 

//Our object to handle the View 
private ParallaxView parallaxView; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    //Get a Display object to access screen details 
    Display display = getWindowManager().getDefaultDisplay(); 

    //Load the resolution into a Point object 
    Point resolution = new Point(); 
    display.getSize(resolution); 

    //And finally set the view for our game 
    parallaxView = new ParallaxView(this, resolution.x, resolution.y); 

    //Make our parallaxView the view for the Activity 
    setContentView(parallaxView); 
} 

//If the Activity is paused make sure to pause our thread 
@Override 
protected void onPause() { 
    super.onPause(); 
    parallaxView.pause(); 
} 

//If the Activity is resumed make sure to resume our thread 
@Override 
protected void onResume() { 
    super.onResume(); 
    parallaxView.resume(); 
} 
} 
+0

initialisieren, indem 'Arraylist Hintergründe = new Arraylist <>();' – Sanoop

+0

Problem gelöst! Danke, Sanoop, DeKaNszn und GhostCat! – KingSpnk

Antwort

0

Sie vergessen, ArrayList zu initialisieren.

ArrayList<Background> backgrounds = new ArrayList<>(); 
+2

Nicht mein Downvote; aber eine Erklärung: Sie wissen wahrscheinlich nicht, dass grundsätzlich alle NPE-Fragen schnell "dupliziert" werden. Deshalb ist es mehr oder weniger entmutigt, auf sie zu antworten. – GhostCat

Verwandte Themen