ivitySWITCH CASE MATTER(ANDROID)

时间:2014-04-08 13:35:18

标签: android

我从用户那里获取数字 他/她想反弹的球。然后我根据用户输入的数字使用开关盒写进一步的代码。 我需要在animationview构造函数之前调用setString()方法,以便变量“value”被初始化.Hence我把“animationview.setString(text);”在“animationview =(AnimationView)findViewById(R.id.animationView)之前;”它是否正确?...当我运行项目时,显示错误“不幸的是,应用程序已停止”..当我在setString()之前调用AnimationView构造函数时方法显示相同的错误..问题是变量值在调用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.setString(text);
        animationview=(AnimationView)findViewById(R.id.animationView);

    }

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

04-08 18:45:39.325: E/AndroidRuntime(670): FATAL EXCEPTION: main
04-08 18:45:39.325: E/AndroidRuntime(670): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.preliminarytest/com.example.preliminarytest.SecondActivity}: java.lang.NullPointerException
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread.access$600(ActivityThread.java:130)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.os.Looper.loop(Looper.java:137)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread.main(ActivityThread.java:4745)
04-08 18:45:39.325: E/AndroidRuntime(670):  at java.lang.reflect.Method.invokeNative(Native Method)
04-08 18:45:39.325: E/AndroidRuntime(670):  at java.lang.reflect.Method.invoke(Method.java:511)
04-08 18:45:39.325: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
04-08 18:45:39.325: E/AndroidRuntime(670):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-08 18:45:39.325: E/AndroidRuntime(670):  at dalvik.system.NativeStart.main(Native Method)
04-08 18:45:39.325: E/AndroidRuntime(670): Caused by: java.lang.NullPointerException
04-08 18:45:39.325: E/AndroidRuntime(670):  at com.example.preliminarytest.SecondActivity.onCreate(SecondActivity.java:25)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.Activity.performCreate(Activity.java:5008)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
04-08 18:45:39.325: E/AndroidRuntime(670):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
04-08 18:45:39.325: E/AndroidRuntime(670):  ... 11 more

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 :(得分:4)

您正以错误的顺序访问animationView。你做了

 animationview.setString(text);
 animationview=(AnimationView)findViewById(R.id.animationView);

但它应该是

 animationview=(AnimationView)findViewById(R.id.animationView);
 animationview.setString(text);

编辑。  我查看了你的AnimationView课程。只在View的构造函数中初始化类成员。您可以创建一个方法,例如initBall,并在其中放置构造函数中的开关/案例逻辑,并在分配{{1}后将此方法调用到构造函数中并调用setString }。这将是解决问题的更快捷方式。

答案 1 :(得分:0)

animationview.setString(text);
animationview=(AnimationView)findViewById(R.id.animationView);

这是倒置的

哦,我明白了,你在设置&#34;值&#34;之前尝试初始化你的视图,但是值尚未初始化。您需要设置默认值,或更改代码。