切换案例问题(Android)

时间:2014-04-08 12:11:02

标签: android

我正在创建弹跳球的项目,其中我从用户输入数字 然后我根据用户输入的数字使用开关盒来编写更多代码。当我没有使用开关盒并执行项目而没有根据球的数量从用户那里获取输入我想要反弹它运作良好..但与开关案例它产生问题..当我打开屏幕应该显示球弹跳它显示错误“不幸的是,应用程序已停止”...这是代码部分,它产生问题..

package com.example.preliminarytest;
import android.content.Context;

import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Handler;
import android.util.AttributeSet;
import android.app.Activity;
import android.view.View;
import android.widget.Toast;
public class AnimationView extends View{
  private final int FRAME_RATE=15;
  private Paint paint;
  private Handler h;
  Ball myball;
  Ball greenball;
  Ball redball;
  Ball cyanball;
  Ball yellowball;
  Ball greyball;
  Ball magentaball;
  Ball grey;
  int value;
  public void setString(int value)
  {
     this.value = value;
     System.out.println("value="+value);
  }

    public AnimationView(Context context,AttributeSet attrs) {
        super(context,attrs);
        // TODO Auto-generated constructor stub

        h=new Handler();
        paint=new Paint();
        paint.setColor(Color.BLUE);
        switch(value)
        {
        case 1:
            myball=new Ball(100,100,Color.BLUE,50);
            myball.setDX(10);
            myball.setDY(10);
            break;
        case 2:
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            break;
        case 3:
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            redball=new Ball(50,400,Color.RED,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            redball.setDX(10);
            redball.setDY(10);
            break;
        case 4:
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            redball=new Ball(50,400,Color.RED,50);
            cyanball=new Ball(100,300,Color.CYAN,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            redball.setDX(10);
            redball.setDY(10);
            cyanball.setDX(10);
            cyanball.setDY(10);
            break;
        case 5:
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            redball=new Ball(50,400,Color.RED,50);
            cyanball=new Ball(100,300,Color.CYAN,50);
            yellowball=new Ball(10,300,Color.YELLOW,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            redball.setDX(10);
            redball.setDY(10);
            cyanball.setDX(10);
            cyanball.setDY(10);
            yellowball.setDX(10);
            yellowball.setDY(10);
            break;
        case 6: 
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            redball=new Ball(50,400,Color.RED,50);
            cyanball=new Ball(100,300,Color.CYAN,50);
            yellowball=new Ball(10,300,Color.YELLOW,50);
            magentaball=new Ball(150,300,Color.MAGENTA,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            redball.setDX(10);
            redball.setDY(10);
            cyanball.setDX(10);
            cyanball.setDY(10);
            yellowball.setDX(10);
            yellowball.setDY(10);
            magentaball.setDX(10);
            magentaball.setDY(10);
            break;
        case 7:
            myball=new Ball(100,100,Color.BLUE,50);
            greenball=new Ball(200,200,Color.GREEN,50);
            redball=new Ball(50,400,Color.RED,50);
            cyanball=new Ball(100,300,Color.CYAN,50);
            yellowball=new Ball(10,300,Color.YELLOW,50);
            magentaball=new Ball(150,300,Color.MAGENTA,50);
            grey=new Ball(100,150,Color.GRAY,50);
            myball.setDX(10);
            myball.setDY(10);
            greenball.setDX(10);
            greenball.setDY(10);
            redball.setDX(10);
            redball.setDY(10);
            cyanball.setDX(10);
            cyanball.setDY(10);
            yellowball.setDX(10);
            yellowball.setDY(10);
            magentaball.setDX(10);
            magentaball.setDY(10);
            grey.setDX(10);
            grey.setDY(10);
            break;
            default:System.out.println("invalid value");


        }



    }
    protected void onDraw(Canvas c)
    {
        switch(value)
        {
        case 1:
            myball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            break;
        case 2:
            myball.bounce(c);
            greenball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            break;
        case 3:
            myball.bounce(c);
            greenball.bounce(c);
            redball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            break;
        case 4:
            myball.bounce(c);
            greenball.bounce(c);
            redball.bounce(c);
            cyanball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            break;
        case 5:
            myball.bounce(c);
            greenball.bounce(c);
            redball.bounce(c);
            cyanball.bounce(c);
            yellowball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
            break;
        case 6:

            myball.bounce(c);
            greenball.bounce(c);
            redball.bounce(c);
            cyanball.bounce(c);
            yellowball.bounce(c);
            magentaball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            c.drawCircle(magentaball.getX(), magentaball.getY(),magentaball.getRadius(), magentaball.getPaint());
            break;

        case 7:
            myball.bounce(c);
            greenball.bounce(c);
            redball.bounce(c);
            cyanball.bounce(c);
            yellowball.bounce(c);
            magentaball.bounce(c);
            greyball.bounce(c);
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            c.drawCircle(yellowball.getX(), yellowball.getY(),yellowball.getRadius(), yellowball.getPaint());
            c.drawCircle(myball.getX(), myball.getY(),myball.getRadius(), myball.getPaint());
            c.drawCircle(greenball.getX(), greenball.getY(),greenball.getRadius(), greenball.getPaint());
            c.drawCircle(redball.getX(), redball.getY(),redball.getRadius(), redball.getPaint());
            c.drawCircle(cyanball.getX(), cyanball.getY(),cyanball.getRadius(), cyanball.getPaint());
            c.drawCircle(magentaball.getX(), magentaball.getY(),magentaball.getRadius(), magentaball.getPaint());
            c.drawCircle(greyball.getX(), greyball.getY(),greyball.getRadius(), greyball.getPaint());
            break;
        default:System.out.println("invalid value");
        }

h.postDelayed(r, FRAME_RATE);

    }
    private Runnable r=new Runnable()
    { public void run()
    { invalidate();
    }
    };
    }

这是Logcat上显示的错误

04-08 17:37:55.288: E/AndroidRuntime(685): FATAL EXCEPTION: main
04-08 17:37:55.288: E/AndroidRuntime(685): java.lang.NullPointerException
04-08 17:37:55.288: E/AndroidRuntime(685):  at com.example.preliminarytest.AnimationView.onDraw(AnimationView.java:162)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13458)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13342)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13340)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13461)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.widget.FrameLayout.draw(FrameLayout.java:467)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13342)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13340)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.drawChild(ViewGroup.java:2929)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2799)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.View.draw(View.java:13461)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.widget.FrameLayout.draw(FrameLayout.java:467)
04-08 17:37:55.288: E/AndroidRuntime(685):  at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2183)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl.drawSoftware(ViewRootImpl.java:2258)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl.draw(ViewRootImpl.java:2153)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2021)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1832)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.Choreographer.doCallbacks(Choreographer.java:555)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.Choreographer.doFrame(Choreographer.java:525)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.os.Handler.handleCallback(Handler.java:615)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.os.Looper.loop(Looper.java:137)
04-08 17:37:55.288: E/AndroidRuntime(685):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-08 17:37:55.288: E/AndroidRuntime(685):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 17:37:55.288: E/AndroidRuntime(685):  at java.lang.reflect.Method.invoke(Method.java:511)
04-08 17:37:55.288: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-08 17:37:55.288: E/AndroidRuntime(685):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-08 17:37:55.288: E/AndroidRuntime(685):  at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

有一些null,因为它在logcat输出中清楚地写了。 AnimationView.java第162行的NullPointerException。请检查此行。可能是你正在使用一些从未初始化的变量。

答案 1 :(得分:0)

嘿,你在onDraw()方法中得到NullPonterException。您可以执行以下操作以避免应用程序崩溃。

protected void onDraw(Canvas c)
{
if(value>0){
Log.e("Value","Value ="+value);
    switch(value)
    {
     // your code.
    }
}
else{
Log.e("Exception","Null Value");
}

这可以避免您的应用崩溃。