开关盒未运行

时间:2014-04-08 14:12:24

标签: android

我从用户那里获取数字 他/她想反弹的球。然后我根据用户输入的数字使用开关盒写进一步的代码。 当我运行项目时,会显示错误"不幸的是,应用程序已停止"当我尝试转移到activity_two时。问题是变量'值'在调用switch或其他一些问题之前没有初始化?并且解决方案是什么......我已经尝试了很多东西......

SecondActivity.java

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;

public class SecondActivity extends Activity {
    AnimationView animationview;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent i = getIntent(); 
        int text = i.getIntExtra("TextBox",3); 
        System.out.println(text);
        setContentView(R.layout.activity_two);
            animationview=(AnimationView)findViewById(R.id.animationView);
        animationview.setString(text);


    }

    @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;
    }

}

AnimationView.java

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错误消息

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

activity_two.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

   <com.example.preliminarytest.AnimationView
       android:id= "@+id/animationView"
       android:layout_width="match_parent"
    android:layout_height="match_parent"/>

</LinearLayout>

2 个答案:

答案 0 :(得分:1)

  

问题是变量&#39;值&#39;在调用switch

之前没有初始化

间接地,是的。您的logcat在NullPointerException中的某个位置清楚地显示了AnimationView.java第164行的onDraw()value本身不能抛出NullPointerException,因为它是基本类型并且具有默认值(0)。

虽然您没有指定这是哪一行,但我猜测它是myball.bounce(c);

构造函数中的开关有一个默认情况,用于打印&#34;无效值&#34;。你在logcat中看到了吗?

由于在构造函数被调用之前你不可能有setString(),所以当你在构造函数中打开它时,value不可避免地会变为0,因此永远不会初始化myball

答案 1 :(得分:0)

在构造函数中,在通过调用setString()初始化之前,您显然正在访问值。那将生成一个空指针异常!将带有所有初始化逻辑的switch语句移动到一个在setString()内调用的函数中。如果还没有调用setString(),请确保onDraw()做了一些良性的事情。