0
2021-07-04 18:05:34.252 13413-13413/space.kozachok.a2048 E/AndroidRuntime: FATAL EXCEPTION: main
    Process: space.kozachok.a2048, PID: 13413
    java.lang.RuntimeException: Unable to start activity ComponentInfo{space.kozachok.a2048/space.kozachok.a2048.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.app.Activity.getResources()' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:223)
        at android.app.ActivityThread.main(ActivityThread.java:7656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.app.Activity.getResources()' on a null object reference
        at space.kozachok.a2048.MainActivity$SwipeTouchListener.<init>(MainActivity.java:322)
        at space.kozachok.a2048.MainActivity.onCreate(MainActivity.java:134)
        at android.app.Activity.performCreate(Activity.java:8000)
        at android.app.Activity.performCreate(Activity.java:7984)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
        at android.os.Handler.dispatchMessage(Handler.java:106) 
        at android.os.Looper.loop(Looper.java:223) 
        at android.app.ActivityThread.main(ActivityThread.java:7656) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 
2021-07-04 18:05:34.254 13413-13503/space.kozachok.a2048 D/HostConnection: HostComposition ext ANDROID_EMU_CHECKSUM_HELPER_v1 ANDROID_EMU_native_sync_v2 ANDROID_EMU_native_sync_v3 ANDROID_EMU_native_sync_v4 ANDROID_EMU_dma_v1 ANDROID_EMU_direct_mem ANDROID_EMU_host_composition_v1 ANDROID_EMU_host_composition_v2 ANDROID_EMU_vulkan ANDROID_EMU_deferred_vulkan_commands ANDROID_EMU_vulkan_null_optional_strings ANDROID_EMU_vulkan_create_resources_with_requirements ANDROID_EMU_YUV_Cache ANDROID_EMU_async_unmap_buffer ANDROID_EMU_vulkan_ignored_handles ANDROID_EMU_vulkan_free_memory_sync ANDROID_EMU_vulkan_shader_float16_int8 ANDROID_EMU_vulkan_async_queue_submit ANDROID_EMU_sync_buffer_data GL_OES_vertex_array_object GL_KHR_texture_compression_astc_ldr ANDROID_EMU_host_side_tracing ANDROID_EMU_async_frame_commands ANDROID_EMU_gles_max_version_2 
2021-07-04 18:05:34.256 13413-13503/space.kozachok.a2048 E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
2021-07-04 18:05:34.291 13413-13503/space.kozachok.a2048 D/EGL_emulation: eglCreateContext: 0xe90f15c0: maj 2 min 0 rcv 2
2021-07-04 18:05:34.292 13413-13503/space.kozachok.a2048 D/EGL_emulation: eglMakeCurrent: 0xe90f15c0: ver 2 0 (tinfo 0xba1d7a50) (first time)
2021-07-04 18:05:34.328 13413-13503/space.kozachok.a2048 E/chromium: [ERROR:gl_surface_egl.cc(549)] eglChooseConfig failed with error EGL_SUCCESS
public class SwipeTouchListener implements View.OnTouchListener {

        private static final String LOG_TAG = "SwipeTouchListener";
        private Activity activity;
        private int MIN_DISTANCE;
        private float downX;
        private float downY;

        public SwipeTouchListener(Activity _activity) {
            MainActivity = _activity;
            DisplayMetrics dm = MainActivity.getResources().getDisplayMetrics();
            MIN_DISTANCE = (int) (120.0f * dm.densityDpi / 160.0f + 0.5);
        }

        private void onRightToLeftSwipe() {

            Log.i(LOG_TAG, "Справа налево!");
        }

        private void onLeftToRightSwipe() {
            Log.i(LOG_TAG, "Слева направо!");

        }


        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    downX = event.getX();
                    downY = event.getY();
                    return true;
                }
                case MotionEvent.ACTION_UP: {
                    float upX = event.getX();
                    float deltaX = downX - upX;

                    // горизонтальный свайп
                    if (Math.abs(deltaX) > MIN_DISTANCE) { // если дистанция не меньше минимальной
                        // слева направо
                        if (deltaX < 0) {
                            this.onLeftToRightSwipe();
                            return true;
                        }
                        //справа налево
                        if (deltaX > 0) {
                            this.onRightToLeftSwipe();
                            return true;
                        }
                    }
                    return false;
                }
            }
            return false;
        }
    }

                public static int pxToDp(int px)
    {
        return (int) (px / Resources.getSystem().getDisplayMetrics().density);
    }
2
  • Где создается SwipeTouchListener и какие параметры передаете при создании объекта? 4 июл 2021 в 15:20
  • DisplayMetrics dm = getResources().getDisplayMetrics();
    – Style-7
    5 июл 2021 в 7:44

1 ответ 1

0

Зачем?

MainActivity = _activity;
DisplayMetrics dm = MainActivity.getResources().getDisplayMetrics();

Надо так

DisplayMetrics dm = _activity.getResources().getDisplayMetrics();

И зачем вы передаёте Activity? Нужно передать Context. И в итоге

public SwipeTouchListener(Context ctx) {
DisplayMetrics dm = ctx.getResources().getDisplayMetrics();
}

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge you have read our privacy policy.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.