controlwear / virtual-joystick-android

This library provides a very simple and ready-to-use custom view which emulates a joystick for Android.
Apache License 2.0
352 stars 123 forks source link

Attempt to invoke virtual method 'void JoystickView.setOnMoveListener(JoystickView.OnMoveListener)' on a null object reference #40

Closed jimmy2447 closed 2 years ago

jimmy2447 commented 2 years ago

I added JoystickView java file into my project and some few other things like mentioned here. I added following lines after .oncreate of my Main activity.

JoystickView joystick = (JoystickView) findViewById(R.id.joystickView);
        joystick.setOnMoveListener(new JoystickView.OnMoveListener() {
            @Override
            public void onMove(int angle, int strength) {

            }
        });

Not sure what is the problem here.

jimmy2447 commented 2 years ago

Ok it worked, sorry my fault, I didn't added the project like I supposed to do (I just copied JoystickView java file and other few things from example code and placed directly in my project).

Right way to do is, I placed "implementation 'io.github.controlwear:virtualjoystick:1.10.1'" inside dependencies in 'Build.gradle' file (There are two gradle files with this name, use the one which is inside 'app' directory). After that sync the gradle, next when you add JoystickView in your layout IDE may say it cannot find 'JoystickView' , in that case use the following code in your layout:

<io.github.controlwear.virtual.joystick.android.JoystickView
        android:id="@+id/joystickView"
        android:layout_width="266dp"
        android:layout_height="298dp"
        android:layout_marginStart="407dp"
        android:layout_marginTop="86dp" />