Cannot resolves Symbol

Asked

Viewed 1,077 times

1

I’m following the tutorial (Android Studio 2.3 Development Essentials) but this giving this error

cannot resolve Symbol

in that part of the code

ConstraintLayout myLayout =
            (ConstraintLayout)findViewById(R.id.activity_motion_event);

this is the action_motion_event.xml file-

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="aprendendo.com.motionevent.MotionEventActivity">


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="16dp"
        android:text="@string/touch_one_status"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="32dp"
        android:text="@string/touch_two_status"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/textView1" />
</android.support.constraint.ConstraintLayout>

this is the code of Motioneventactivity

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_motion_event);


    ConstraintLayout myLayout =
            (ConstraintLayout)findViewById(R.id.activity_motion_event);

    myLayout.setOnTouchListener(
            new ConstraintLayout.OnTouchListener(){
                public boolean onTouch(View v,
                                       MotionEvent m){
                    handleTouch(m);
                    return true;
                }
            }
    );

}
void handleTouch(MotionEvent m){
    TextView textView1 = (TextView)findViewById(R.id.textView1);
    TextView textView2 = (TextView)findViewById(R.id.textView2);

    int pointerCount = m.getPointerCount();

    for (int i = 0; i < pointerCount; i++){
        int x = (int) m.getX(i);
        int y = (int) m.getY(i);
        int id = m.getPointerId(i);
        int action = m.getActionMasked();
        int actionIndex = m.getActionIndex();
        String actionString;

        switch (action){
            case MotionEvent.ACTION_DOWN:
                actionString = "DOWN";
                break;
            case MotionEvent.ACTION_UP:
                actionString = "UP";
                break;
            case MotionEvent.ACTION_POINTER_DOWN:
                actionString = "PNTR DOWN";
                break;
            case MotionEvent.ACTION_POINTER_UP:
                actionString = "PNTR UP";
                break;
            case MotionEvent.ACTION_MOVE:
                actionString = "MOVE";
                break;
            default:
                actionString = "";
        }
        String touchStatus = "Action: " + actionString + " Index: " +
                actionIndex + " ID: " + id + " X: " + x + " Y: " + y;

        if (id == 0)
            textView1.setText(touchStatus);
        else
            textView2.setText(touchStatus);
    }
}

}

  • Yo! You asked in the right place! But in the wrong language... Translate for us, Please? :)

  • Sorry, I thought you were at Stockoverflow in English .

1 answer

0


You did not set the parameter "id" with the value "activity_motion_event" in the XML Constraintlayout tag.

  • Thank you very much! the tutorial does not mention this, but I did it here and it worked

  • It is because in the findViewById() method you always need to pass an ID that must exist in the XML file, otherwise it returns null and will mess up the application. This goes for any view you work on in java. In your case, as you are trying to pass the name "activity_motion_event", then the view has to have that same ID in XML (it could be any other name). Hugs and good luck.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.