-1

when I launch my app it crashes:

public abstract class MainActivity extends AppCompatActivity
        implements View.OnTouchListener, NavigationView.OnNavigationItemSelectedListener{

whitout abstract and View.OnTouchListener it works perfectly and. Why?

adrianoBP
  • 187
  • 6
  • 19

3 Answers3

2

You need to override the methods of View.OnTouchListener interface instead of making your activity as abstract so remove abstract

public class MainActivity extends AppCompatActivity
        implements View.OnTouchListener, NavigationView.OnNavigationItemSelectedListener{
   // other code and methods of NavigationView.OnNavigationItemSelectedListener

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        return true;
    }

}

Alternatively you can use Add unimplemented methods option from error help or press Ctrl + I to view the list of unimplemented methods

Note : abstract class cannot be instantiated so read Why can't an object of abstract class be created?

Android OS will create an object of your launcher activity internally or it's also done while using Intent to start an activity

Reference

Abstract Classes

Interface

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
-1

When you add implements View.OnTouchListener you have to actually implement it.

When you declare it as abstract you don't implement the needed method.

This is: onTouch(View v, MotionEvent event)

Info here: https://developer.android.com/reference/android/view/View.OnTouchListener.html

Just add:

@Override 
public onTouch(View v, MotionEvent event) {

}

And it will compile.

JonZarate
  • 841
  • 6
  • 28