简体   繁体   English

每当我在色轮外滑动时,我的应用程序就会崩溃

[英]my app crashes whenever i swipe outside the color wheel

This app basically lets you pick a color from the wheel and allows you to change the background page color of the app, however, whenever i try to swipe from the color wheel to the outside, the app crashes for some reason.The following is my code for the MainActivity.java file这个应用程序基本上可以让你从色轮中选择一种颜色,并允许你更改应用程序的背景页面颜色,但是,每当我尝试从色轮向外滑动时,应用程序就会由于某种原因崩溃。以下是我的MainActivity.java 文件的代码

 package com.example.colorpicker_assignment2;

import android.annotation.SuppressLint;
import android.graphics.Bitmap;
import android.graphics.Color;

import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;


public class MainActivity extends AppCompatActivity  {


        Bitmap map; //represent the color wheel on a circular grid composed of individual dots corresponding to our pixels
    @SuppressLint("ClickableViewAccessibility")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
  final ImageView color_wheel = findViewById(R.id.color_wheel); //find the color wheel
        final TextView color_results = findViewById(R.id.color_results); //find the text to print the color

    color_wheel.setDrawingCacheEnabled(true);
    color_wheel.buildDrawingCache(true);


        final ConstraintLayout layout = findViewById(R.id.constlayout); //define a contraint layout object to set the background color
//An interface to handle a touch event
        color_wheel.setOnTouchListener(new View.OnTouchListener(){
            @Override

            public boolean onTouch(View v, MotionEvent event)
            {
                if(event.getAction()==MotionEvent.ACTION_DOWN||event.getAction()==MotionEvent.ACTION_MOVE)
                {
                   map = color_wheel.getDrawingCache();
                    int pixel = map.getPixel((int)event.getX(),(int)event.getY());
                   //get the RGB values
                    int Red = Color.red(pixel); //assign the red pixel values collected to variable R
                    int Green = Color.green(pixel);//assign the green pixels collected to va
                    int Blue = Color.blue(pixel);

                    //get the hex values
                    String hexadecimal = "#"+Integer.toHexString(pixel);

                    //set background color
                    layout.setBackgroundColor(Color.rgb(Red,Green,Blue));

                    color_results.setText("RGB: "+Red+" ,"+Green+" ,"+Blue+"\nHEX: "+hexadecimal);
                }
                return true;
            }

        });
    }
}

The following is my activity_main.xml file:以下是我的 activity_main.xml 文件:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/constlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <!--Pick a color from this image -->

    <ImageView
        android:id="@+id/color_wheel"
        android:layout_width="352dp"
        android:layout_height="309dp"
        android:layout_marginTop="60dp"
        android:clickable="true"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:srcCompat="@drawable/wheel" />

    <TextView
        android:id="@+id/color_results"
        android:layout_width="249dp"
        android:layout_height="103dp"
        android:layout_marginTop="16dp"
        android:text="Hex: \nRGB:"
        android:textColor="#000"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.24"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView" />

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:text="@string/Backgroundcolorvalues"
        android:textSize="18sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/color_wheel" />

    <TextView
        android:id="@+id/title"
        android:layout_width="320dp"
        android:layout_height="58dp"
        android:layout_marginTop="4dp"
        android:text="@string/title"
        android:textSize="24sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.59"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

The is the error message that i get:这是我得到的错误消息:

E/InputEventReceiver: Exception dispatching input event.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.colorpicker_assignment2, PID: 24845
    java.lang.IllegalArgumentException: y must be < bitmap.height()
        at android.graphics.Bitmap.checkPixelAccess(Bitmap.java:1958)
        at android.graphics.Bitmap.getPixel(Bitmap.java:1863)
        at com.example.colorpicker_assignment2.MainActivity$1.onTouch(MainActivity.java:43)
        at android.view.View.dispatchTouchEvent(View.java:13411)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:3060)
        at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2755)
        at com.android.internal.policy.DecorView.superDispatchTouchEvent(DecorView.java:465)
        at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1849)
        at android.app.Activity.dispatchTouchEvent(Activity.java:3993)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:69)
        at com.android.internal.policy.DecorView.dispatchTouchEvent(DecorView.java:423)
        at android.view.View.dispatchPointerEvent(View.java:13674)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:5482)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5285)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4947)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5004)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4841)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4807)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4815)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4788)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7505)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7474)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7435)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:7630)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:188)
        at android.view.InputEventReceiver.nativeConsumeBatchedInputEvents(Native Method)
        at android.view.InputEventReceiver.consumeBatchedInputEvents(InputEventReceiver.java:178)
        at android.view.ViewRootImpl.doConsumeBatchedInput(ViewRootImpl.java:7581)
        at android.view.ViewRootImpl$ConsumeBatchedInputRunnable.run(ViewRootImpl.java:7654)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:966)
        at android.view.Choreographer.doCallbacks(Choreographer.java:790)
        at android.view.Choreographer.doFrame(Choreographer.java:718)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:951)
        at android.os.Handler.handleCallback(Handler.java:883)
        at android.os.Handler.dispatchMessage(Handler.java:100)
        at android.os.Looper.loop(Looper.java:214)
        at android.app.ActivityThread.main(ActivityThread.java:7356)
E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
I/Process: Sending signal. PID: 24845 SIG: 9
Disconnected from the target VM, address: 'localhost:8605', transport: 'socket'

This is the image for my color wheel:这是我的色轮的图像: 我的色轮图片

The clue is in the LogCat:线索在 LogCat 中:

java.lang.IllegalArgumentException: y must be < bitmap.height()

The event X and Y may become out of range when you swipe.当您滑动时, event X 和 Y 可能会超出范围。

So, one easy fix is to ignore Xs and Ys that are too large:因此,一个简单的解决方法是忽略太大的 Xs 和 Ys:

map = color_wheel.getDrawingCache();
// Add this
if (event.getX() >= map.getWidth() || event.getY() >= map.getHeight())
    return true;
// To here
int pixel = map.getPixel((int)event.getX(),(int)event.getY());

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 每当我的EditText为空并且按下按钮时,我的应用程序就会崩溃 - My app crashes whenever my EditText is empty and I press a button 每当我实现我的 CountDownTimer 代码时,我的移动应用程序就会崩溃 - My mobile app crashes whenever I implement my CountDownTimer code 每当我尝试更改活动时,我的应用就会崩溃 - My app crashes whenever i try to change activities 我的应用程序在启动时崩溃 - Android Studio - My App crashes whenever it is launched - Android Studio 每当我 go 进入活动时,该应用程序就会崩溃 - The app just crashes whenever I go into the activity 当我从一个片段滑动到另一个片段时,我的应用程序崩溃(错误:canScrollHorizo​​ntally()&#39; 在空对象引用上) - my app crashes when I swipe from one fragment to another (error: canScrollHorizontally()' on a null object reference) 每当我注销时应用程序崩溃,使用 firebase 进行身份验证 - App crashes whenever I log out, authentication with firebase 每当我在 andriod studio EditText 框中执行诸如添加子等于等任何操作时,我的应用程序都会崩溃 - whenever i perform any operations like add sub equals etc in andriod studio EditText box , my app crashes 每当我第一次启动该应用程序时,该应用程序崩溃,然后它会询问我用户权限 - Whenever I launch the app for the first time the app crashes and then it asks me the user permission 每当我运行我的Android应用程序时,它说“XYZ没有响应” - Whenever I am running my android app, it says “XYZ is not responding”
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM