我的onClickListener有什么问题?

问题描述 投票:0回答:1

我试图编写一个简单的应用程序,显示一个彩色的圆圈,当圆圈离屏幕中心越远,它就会逐渐变黑。然而,每当我点击该应用程序时,它就会崩溃。当我删除onClick代码后,应用程序运行正常。我在logcat中能找到的唯一错误是。

Fatal signal 11 (SIGSEGV), code 1, fault addr 0x94 in tid 11955 (raphicstutorial).

而我却不知道该怎么处理。

代码如下。

package com.example.a2dgraphicstutorial;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
public class MyView extends View {
    private Canvas thisCanvas;
    private int colourID;
    private Paint fillPaint = new Paint();

    public MyView(Context context){
        super(context);
        colourID = 0;
        fillPaint.setStyle(Paint.Style.FILL);
    }

    public void drawCircle(){
        int x = getWidth();
        int y = getHeight();

        //Drawing the circles
        for (int i = 255;i >= 0;i--){
            //Determining colour
            if (colourID == 0){
                fillPaint.setColor(Color.argb(255,255-i,0,0));
            }
            else if (colourID == 1){
                fillPaint.setColor(Color.argb(255,0,255-i,0));
            }
            else if (colourID == 2){
                fillPaint.setColor(Color.argb(255,0,0,255-i));
            }

            thisCanvas.drawCircle(x/2,y/2,i,fillPaint);
        }

        //Cycling the colourID so the next circle will be a different colour
        colourID = (colourID + 1)%3;
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        thisCanvas = canvas;

        //Setting the background to be black
        fillPaint.setColor(Color.parseColor("#000000"));
        thisCanvas.drawPaint(fillPaint);

        //Drawing the first circle
        drawCircle();
    }

}
MyView theScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    theScreen = new MyView(this);
    theScreen.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            theScreen.drawCircle();
        }
    });

    setContentView(theScreen);
}
}
java android android-studio onclick onclicklistener
1个回答
0
投票

而不是覆盖 onCreate()

覆盖 onTouchEvent() 且听咔嚓一声。

@Override
public boolean onTouchEvent(MotionEvent event) {
    super.onTouchEvent(event);

    switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            //the finger is down do something.....

            return true;

        case MotionEvent.ACTION_UP:
            //the finger is up do something.....(this is a click)
            //to redraw
            invalidate();

            return true;
    }
    return false;
}

对了

要触发 onDraw(),点击后调用这个。

invalidate();

0
投票

从前我也遇到过类似的问题 解决的办法是把 "这个 "改为 "活动名称".

// replace this to theScreen = new MyView(this);
       theScreen = new MyView(MainActivity.class);

另一个解决办法是用"getBaseContext()"

// replace this to theScreen = new MyView(this);
           theScreen = new MyView(getBaseContext());

  1. 这个 该context返回该活动的当前上下文,属于该活动,该活动被销毁的话也会被销毁。
  2. getBaseContext() 是ContextWrapper的方法,它是 "Context的代理实现,简单地将其所有的调用委托给另一个Context。可以被子类化来修改行为,而不改变原Context。"

0
投票

当你在onDraw方法之外调用Canvas时,就会出现这个错误,试试这段代码。

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity {
    public class MyView extends View {
        private Canvas thisCanvas;
        private int colourID;
        private Paint fillPaint = new Paint();

    public MyView(Context context){
        super(context);
        colourID = 0;
        fillPaint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas){
        super.onDraw(canvas);
        thisCanvas = canvas;

        //Setting the background to be black
        fillPaint.setColor(Color.parseColor("#000000"));
        thisCanvas.drawPaint(fillPaint);

        int x = getWidth();
        int y = getHeight();

        //Drawing the circles
        for (int i = 255;i >= 0;i--){
            //Determining colour
            if (colourID == 0){
                fillPaint.setColor(Color.argb(255,255-i,0,0));
            }
            else if (colourID == 1){
                fillPaint.setColor(Color.argb(255,0,255-i,0));
            }
            else if (colourID == 2){
                fillPaint.setColor(Color.argb(255,0,0,255-i));
            }

            thisCanvas.drawCircle(x/2,y/2,i,fillPaint);
        }

        //Cycling the colourID so the next circle will be a different colour
        colourID = (colourID + 1)%3;
    }

}
MyView theScreen;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    theScreen = new MyView(this);
    theScreen.setOnClickListener(new OnClickListener(){
        @Override
        public void onClick(View v){
            theScreen.invalidate();
        }
    });
    setContentView(theScreen);
}

}

我移动了 drawCircle 方法的内部。onDraw 并改变 theScreen 不叫 drawCircle 呼叫 invalidate() 而不是,我不知道这是不是你想要的结果,但至少这是错误的问题。

© www.soinside.com 2019 - 2024. All rights reserved.