誰でも助けてもらえますか?アンドロイドスタジオで視差画像ビューを実行しようとしましたが、「ヌルオブジェクト参照で仮想メソッドを呼び出そうとしました」という例外メッセージが表示され続けます。それは、それがNullPointerExceptionであることを明らかにする。青色で強調表示されたメッセージは、発生したようです。ヌルオブジェクト参照(NullpointerException)で仮想メソッドを呼び出そうとしました
これはラインである:私のParallaxViewクラスで
backgrounds.add(new Background(this.context, screenWidth, screenHeight, "skyline", 0, 80, 50));
。
そしてラインで:私のParallaxActivityクラスで
setContentView(parallaxView);
。
これは例外が発生する場所です。私はコピー&ペーストフル例外メッセージを、以下のクラス: 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
そして 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();
}
}
:
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)
これらは2つのエラーがで発生したクラスです
は 'ArrayListの背景で初期化=新しいArrayListを<>();' –
Sanoop
問題を解決します!ありがとうSanoop、DeKaNszn、そしてGhostCat! – KingSpnk