Error with setOnTouchListener [closed]

0

Eae, I've been getting the second error while running an app on AndroidStudio

/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.example.vinicius.login, PID: 3519
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vinicius.login/com.example.vinicius.login.inicio}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                  at android.app.ActivityThread.-wrap12(ActivityThread.java)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:154)
                  at android.app.ActivityThread.main(ActivityThread.java:6077)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnTouchListener(android.view.View$OnTouchListener)' on a null object reference
                  at com.example.vinicius.login.inicio.onCreate(inicio.java:40)
                  at android.app.Activity.performCreate(Activity.java:6664)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                  at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:154) 
                  at android.app.ActivityThread.main(ActivityThread.java:6077) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

From what I could see it points the error as line 40 where there is a button, I do not know if the error is only this but I await answers and helps, follow the code below

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;


public class inicio extends Activity{



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        /********************************/
         /*    Define os botões  */
        /********************************/
        Button led1 = (Button) findViewById(R.id.LED1);
        Button led2 = (Button) findViewById(R.id.LED2);
        Button led3 = (Button) findViewById(R.id.LED3);
        Button led4 = (Button) findViewById(R.id.LED4);
        Button led5 = (Button) findViewById(R.id.LED5);
        Button led6 = (Button) findViewById(R.id.LED6);

        /*******************************************************/
         /*  Seta um Onclick e Onchange  */
        /*******************************************************/

        led6.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button is led 3 */
                    new Background_get().execute("led6=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led6=0");
                }
                return true;
            }
        });

        led5.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button UP */
                    new Background_get().execute("led5=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led5=0");
                }
                return true;
            }
        });

        led4.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button BACK */
                    new Background_get().execute("led4=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led4=0");
                }
                return true;
            }
        });

        led3.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button RIGHT */
                    new Background_get().execute("led3=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led3=0");
                }
                return true;
            }
        });

        led2.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button LEFT */
                    new Background_get().execute("led2=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led2=0");
                }
                return true;
            }
        });

        led1.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    /* button FRONT */
                    new Background_get().execute("led1=1");
                } else if (event.getAction() == MotionEvent.ACTION_UP) {
                    new Background_get().execute("led1=0");
                }
                return true;
            }
        });

    }
}


class Background_get extends AsyncTask<String, Void, String> {
    @Override
    protected String doInBackground(String... params) {
        try {
                /* Conexão com o Raspberry PI 3, solicitação HTML de comando PHP servidor APACHE  */
            URL url = new URL("http://192.168.0.101/?" + params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();

            BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuilder result = new StringBuilder();
            String inputLine;
            while ((inputLine = in.readLine()) != null)
                result.append(inputLine).append("\n");

            in.close();
            connection.disconnect();
            return result.toString();

        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }
}

My XML is as follows

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#4F4F4F"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vinicius.login.inicio">

    <TextView
        android:text="LOGIN™"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView"
        android:textColor="#B5B5B5"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true" />

    <Button
        android:text="FRONT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="43dp" />

    <Button
        android:text="BACK"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED4"
        android:layout_below="@+id/LED2"
        android:layout_toRightOf="@+id/LED2"
        android:layout_toEndOf="@+id/LED2" />

    <Button
        android:text="RIGHT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED3"
        android:layout_above="@+id/LED4"
        android:layout_toRightOf="@+id/LED1"
        android:layout_toEndOf="@+id/LED1" />

    <Button
        android:text="LEFT"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED2"
        android:layout_marginTop="48dp"
        android:layout_alignTop="@+id/LED1"
        android:layout_toLeftOf="@+id/LED1"
        android:layout_toStartOf="@+id/LED1" />

    <Button
        android:text="DOWN"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED6"
        android:layout_above="@+id/textView"
        android:layout_alignLeft="@+id/LED5"
        android:layout_alignStart="@+id/LED5"
        android:layout_marginBottom="39dp" />

    <TextView
        android:text="XY"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#B5B5B5"
        android:layout_alignTop="@+id/LED2"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="13dp"
        android:id="@+id/textView2" />

    <Button
        android:text="UP"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/LED5"
        android:layout_above="@+id/LED6"
        android:layout_alignLeft="@+id/LED4"
        android:layout_alignStart="@+id/LED4"
        android:layout_marginBottom="18dp" />

    <TextView
        android:text="Z"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="#B5B5B5"
        android:id="@+id/textView3"
        android:layout_above="@+id/LED6"
        android:layout_centerHorizontal="true" />

    <ImageView
        android:layout_width="80dp"
        android:layout_height="50dp"
        app:srcCompat="@android:drawable/ic_menu_preferences"
        android:id="@+id/imageView2"
        android:layout_above="@+id/LED5"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="34dp" />

    <ImageView
        android:layout_width="20dp"
        android:layout_height="20dp"
        app:srcCompat="@android:drawable/ic_partial_secure"
        android:id="@+id/imageView4"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/LED3" />


</RelativeLayout>
    
asked by anonymous 19.04.2017 / 19:45

1 answer

0

As the friend mentioned above, he spoke setContentView, which "inflates" the layout XML file in JAVA so that UI objects can be instantiated. That's why you took the "null object" error, since JAVA did not find any reference to the button you tried to use.

    
20.04.2017 / 00:35