尝试绘制一条线时出现NullPointerException - android

时间:2014-01-11 17:46:34

标签: java android

我正在开发一款安卓游戏,直到我现在需要包含动画为止。

基本上我需要画2点之间的一条线。我在主要相对布局中有1个相对布局(持有表格布局)。

有9个按钮,它们悬停在上面并创建一个序列,我需要创建一条跟随手指从一个点到下一个点的线(使用块坐标序列)。我将块的顺序存储在一个arraylist中 - 如下所示:[1,2,3,6,5,4,7,8,9]。所以我必须从第一个块到第二个块绘制线,依此类推。

现在我使用.drawLine()方法介绍动画。但每次它崩溃时都会出现NullPointerException。

我是新手,我做错了还是错过了什么?否则怎么办?

以下是游戏类:

public class Game extends Activity implements OnTouchListener{

TextView memorize, menu;
ImageButton b1, b2, b3, b4, b5, b6, b7, b8, b9;
Rect b1R, b2R, b3R, b4R, b5R, b6R, b7R, b8R, b9R;
int x, y;
ArrayList<String> arr = new ArrayList<String>();
ArrayList<String> fin = new ArrayList<String>();
ArrayList<String> fin2 = new ArrayList<String>();
Intent i;
String LEVEL;
int data;
String mainSequence, gameSequence;
Paint paint1, paint2, paint3, paint4, paint5, paint6, paint7, paint8, paint9;
Canvas canvas = new Canvas();
int b1X, b1Y;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.ingame);

    Bundle extras = getIntent().getExtras();
    LEVEL = extras.getString("level");

    DB ourdb = new DB(this);
    data = 0;
    ourdb.open();
    data = ourdb.getSequence(LEVEL);
    ourdb.close();

    setup();
}

private void setup(){
    // TextViews
    memorize = (TextView) findViewById(R.id.tvMemorize);
    menu = (TextView) findViewById(R.id.tvInGameMenu);

    // ImageButtons
    b1 = (ImageButton) findViewById(R.id.ib1);
    b2 = (ImageButton) findViewById(R.id.ib2);
    b3 = (ImageButton) findViewById(R.id.ib3);
    b4 = (ImageButton) findViewById(R.id.ib4);
    b5 = (ImageButton) findViewById(R.id.ib5);
    b6 = (ImageButton) findViewById(R.id.ib6);
    b7 = (ImageButton) findViewById(R.id.ib7);
    b8 = (ImageButton) findViewById(R.id.ib8);
    b9 = (ImageButton) findViewById(R.id.ib9);

    // onclick listeners

    // ontouch listeners
    b1.setOnTouchListener(this);
    b2.setOnTouchListener(this);
    b3.setOnTouchListener(this);
    b4.setOnTouchListener(this);
    b5.setOnTouchListener(this);
    b6.setOnTouchListener(this);
    b7.setOnTouchListener(this);
    b8.setOnTouchListener(this);
    b9.setOnTouchListener(this);

    // paint
    paint1 = new Paint();
    paint1.setARGB(255, 8, 45, 0);
    paint2 = new Paint();
    paint3 = new Paint();
    paint4 = new Paint();
    paint5 = new Paint();
    paint6 = new Paint();
    paint7 = new Paint();
    paint8 = new Paint();
    paint9 = new Paint();
}

@Override
protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    finish();
}

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
    // TODO Auto-generated method stub
    try {
        Thread.sleep(16);
    } catch (InterruptedException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    x = (int) arg1.getRawX();
    y = (int) arg1.getRawY();

    if(b1R.contains(x, y)){
        arr.add("1");
//          Log.d("block", "1");
        b1.setImageResource(R.drawable.checkpoint_hover);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b2R.contains(x,y)){
        arr.add("2");
//          Log.d("block", "2");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint_hover);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b3R.contains(x,y)){
        arr.add("3");
//          Log.d("block", "3");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint_hover);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b4R.contains(x,y)){
        arr.add("4");
//          Log.d("block", "4");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint_hover);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b5R.contains(x,y)){
        arr.add("5");
//          Log.d("block", "5");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint_hover);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b6R.contains(x,y)){
        arr.add("6");
//          Log.d("block", "6");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint_hover);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b7R.contains(x,y)){
        arr.add("7");
//          Log.d("block", "7");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint_hover);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b8R.contains(x,y)){
        arr.add("8");
//          Log.d("block", "8");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint_hover);
        b9.setImageResource(R.drawable.checkpoint);
    }
    if(b9R.contains(x,y)){
        arr.add("9");
//          Log.d("block", "9");
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint_hover);
    }

    sort();

    if(fin.toString() != null){
        Log.d("log", fin.toString());
    }



    if(arg1.getAction() == MotionEvent.ACTION_UP){
        paintLine(x,y);
        b1.setImageResource(R.drawable.checkpoint);
        b2.setImageResource(R.drawable.checkpoint);
        b3.setImageResource(R.drawable.checkpoint);
        b4.setImageResource(R.drawable.checkpoint);
        b5.setImageResource(R.drawable.checkpoint);
        b6.setImageResource(R.drawable.checkpoint);
        b7.setImageResource(R.drawable.checkpoint);
        b8.setImageResource(R.drawable.checkpoint);
        b9.setImageResource(R.drawable.checkpoint);
        checkSequence();
        arr.clear();
        fin.clear();
    }

    return false;
}

private void paintLine(int x2, int y2) {
    // TODO Auto-generated method stub
//      Log.d("x2", ""+x2);
//      Log.d("y2", ""+y2);
    canvas.drawLine(78, 362, 100, 400, paint1);
}

private void checkSequence() {
    // TODO Auto-generated method stub

    mainSequence = "";

    for(String s : fin){
        mainSequence += s;
    }

    fin.clear();

    mainSequence = mainSequence.replace("null", "");

    gameSequence = ""+data;

    Log.d("main sequence", mainSequence);
    Log.d("game sequence", gameSequence);

    if(mainSequence.equals(gameSequence)){
        Log.d("game", "win");
    }
}

private void sort() {
    // TODO Auto-generated method stub
    for(int i=0; i<arr.size(); i++){
        if(!fin.contains(arr.get(i))){
            fin.add(arr.get(i));
        }
    }
}

@Override
public void onWindowFocusChanged (boolean hasFocus){
    super.onWindowFocusChanged(hasFocus);
    if(hasFocus){

        b1R = new Rect(b1.getLeft(), b1.getTop()+300, b1.getRight(), b1.getBottom()+300);
        b2R = new Rect(b2.getLeft(), b2.getTop()+300, b2.getRight(), b2.getBottom()+300);
        b3R = new Rect(b3.getLeft(), b3.getTop()+300, b3.getRight(), b3.getBottom()+300);
        b4R = new Rect(b4.getLeft(), b4.getTop()+420, b4.getRight(), b4.getBottom()+420);
        b5R = new Rect(b5.getLeft(), b5.getTop()+420, b5.getRight(), b5.getBottom()+420);
        b6R = new Rect(b6.getLeft(), b6.getTop()+420, b6.getRight(), b6.getBottom()+420);
        b7R = new Rect(b7.getLeft(), b7.getTop()+540, b7.getRight(), b7.getBottom()+540);
        b8R = new Rect(b8.getLeft(), b8.getTop()+540, b8.getRight(), b8.getBottom()+540);
        b9R = new Rect(b9.getLeft(), b9.getTop()+540, b9.getRight(), b9.getBottom()+540);

        b1X = b1R.width()/2;
        b1Y = b1R.height()/2;

        Log.d("b1X" , ""+b1X);
        Log.d("b1Y" , ""+b1Y);
    }
}

}

我哪里错了? 提前致谢

更新

我的LogCat:

01-11 19:52:34.384: E/AndroidRuntime(797): FATAL EXCEPTION: main
01-11 19:52:34.384: E/AndroidRuntime(797): java.lang.NullPointerException
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.graphics.Canvas.drawLine(Canvas.java:809)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.docime.vamoose.patternz.Game.paintLine(Game.java:264)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.docime.vamoose.patternz.Game.onTouch(Game.java:242)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.View.dispatchTouchEvent(View.java:3762)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:936)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1671)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.app.Activity.dispatchTouchEvent(Activity.java:2086)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1655)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1785)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.os.Handler.dispatchMessage(Handler.java:99)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.os.Looper.loop(Looper.java:123)
01-11 19:52:34.384: E/AndroidRuntime(797):  at android.app.ActivityThread.main(ActivityThread.java:4627)
01-11 19:52:34.384: E/AndroidRuntime(797):  at java.lang.reflect.Method.invokeNative(Native Method)
01-11 19:52:34.384: E/AndroidRuntime(797):  at java.lang.reflect.Method.invoke(Method.java:521)
01-11 19:52:34.384: E/AndroidRuntime(797):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
01-11 19:52:34.384: E/AndroidRuntime(797):  at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
01-11 19:52:34.384: E/AndroidRuntime(797):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:3)

你没有初始化画布,这就是你得到空指针异常的原因:

 canvas.drawLine(78, 362, 100, 400, null);

像这样初始化画布:

Bitmap canvasBitmap = Bitmap.createBitmap(w, h, Bitmap.Config.ARGB_8888);
canvas = new Canvas(canvasBitmap);

并创建piant对象以绘制具有一些颜色和线条大小的行:

Paint drawPaint=new Paint();
drawPaint.setAntiAlias(true);
drawPaint.setStrokeWidth(20);
drawPaint.setStyle(Paint.Style.STROKE);
drawPaint.setStrokeJoin(Paint.Join.ROUND);
drawPaint.setStrokeCap(Paint.Cap.ROUND);

然后绘制你的行:

canva.drawLine(78, 362, 100, 400, drawPaint);

答案 1 :(得分:2)

尝试将Paint对象初始化为Paint paint = new Paint();,而不是使其为空。

来自http://developer.android.com/reference/android

public void drawLine (float startX, float startY, float stopX, float stopY, Paint paint)

Parameters
startX  The x-coordinate of the start point of the line
startY  The y-coordinate of the start point of the line
paint   The paint used to draw the line