代码在一次之前工作,不再有效

时间:2015-04-21 18:34:30

标签: java android logcat android-logcat

所以这段代码之前运行了一次。然后我关闭了我的电脑,然后在我起床时进入睡眠状态并尝试运行代码我现在在logcat中遇到错误并且应用程序崩溃了。我根本没有改变代码?怎么会发生这种情况呢?

public class DrawingView extends View{



    public DrawingView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub

    }
    RectF rectf = new RectF(0, 0, 200, 0);

    private static final int w = 100;
    public static int lastColor = Color.BLACK;
    private final Random random = new Random();
    private final Paint paint = new Paint();
    private final int radius = 230;
    private final Handler handler = new Handler();
    public static int redColor = Color.RED;
    public static int greenColor = Color.GREEN;
    int randomWidth = 0;
    int randomHeight = 0;
    public static int addPoints = 0;


    private final Runnable updateCircle = new Runnable() {
        @Override 
        public void run() {
            lastColor = random.nextInt(2) == 1 ? redColor : greenColor;
            paint.setColor(lastColor);
            invalidate();
            handler.postDelayed(this, 1000);
        }
    };

    @Override 
    protected void onAttachedToWindow() {
        super.onAttachedToWindow();
        handler.post(updateCircle);
    }

    @Override 
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        handler.removeCallbacks(updateCircle);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // your other stuff here
        if(random == null){
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }else {
            randomWidth =(int) (random.nextInt(Math.abs(getWidth()-radius/2)) + radius/2f);
            randomHeight = (random.nextInt((int)Math.abs((getHeight()-radius/2 + radius/2f))));
        }

        canvas.drawCircle(randomWidth, randomHeight, radius, paint);

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if(event.getAction() == MotionEvent.ACTION_DOWN){

            int x = (int) event.getX();
            int y = (int) event.getY();

                if(isInsideCircle(x, y) ==  true){
                    //Do your things here
                        if(redColor == lastColor){

                            Intent i = new Intent(this.getContext(), YouFailed.class);
                            this.getContext().startActivity(i);

                        } else {

                            addPoints++;

                        }
                }else {

                }
        }
        return true;

}

public boolean isInsideCircle(int x, int y){
  if ((((x - randomWidth)*(x - randomWidth)) + ((y - randomHeight)*(y - randomHeight))) < ((radius)*(radius))){
    return true;
  }
  return false;    
}

}

这是logCat中的错误。它说它与View有关?和getParent()方法。我认为它可能与使用this而不是上下文有关,但我不确定java有时会让我感到困惑。

04-21 11:48:58.287: E/AndroidRuntime(2502): FATAL EXCEPTION: main
04-21 11:48:58.287: E/AndroidRuntime(2502): Process: com.Tripps.thesimplegame, PID: 2502
04-21 11:48:58.287: E/AndroidRuntime(2502): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.Tripps.thesimplegame/com.Tripps.thesimplegame.Main}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.view.View.getParent()' on a null object reference
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread.access$800(ActivityThread.java:144)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.os.Looper.loop(Looper.java:135)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread.main(ActivityThread.java:5221)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at java.lang.reflect.Method.invoke(Native Method)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at java.lang.reflect.Method.invoke(Method.java:372)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
04-21 11:48:58.287: E/AndroidRuntime(2502): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewParent android.view.View.getParent()' on a null object reference
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.view.ViewGroup.addViewInner(ViewGroup.java:3879)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.view.ViewGroup.addView(ViewGroup.java:3733)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.view.ViewGroup.addView(ViewGroup.java:3709)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:406)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:387)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.Activity.setContentView(Activity.java:2164)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at com.Tripps.thesimplegame.Main.onCreate(Main.java:48)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.Activity.performCreate(Activity.java:5933)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
04-21 11:48:58.287: E/AndroidRuntime(2502):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
04-21 11:48:58.287: E/AndroidRuntime(2502):     ... 10 more
很抱歉大家。谢谢你指出第48行,因为我没有为contentent视图创建对象

public class Main extends Activity {
    DrawingView v;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.requestWindowFeature(Window.FEATURE_NO_TITLE);
        this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);


        //LinearLayout layout1 = new LinearLayout (this);
        //FrameLayout game = new FrameLayout(this);
        DrawingView v = new DrawingView (this);

        //TextView myText = new TextView(this);

        //Button redCircle = new Button(this);



        //redCircle.setWidth(300);
        //redCircle.setText(DrawingView.addPoints);


        //layout1.addView(myText);
        //layout1.addView(redCircle); 
        //redCircle.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        //game.addView(myText);
        //game.addView(v);
        //game.addView(layout1);
        setContentView(v);
        //redCircle.setOnClickListener((OnClickListener) this);
    }
    public void onClick(View v) {
        Intent intent = new Intent(this, Main.class);
            startActivity(intent);

        // re-starts this activity from game-view. add this.finish(); to remove from stack
   }

    @Override
    public boolean onCreateOptionsMenu(Menu menu){
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }



}

0 个答案:

没有答案