-1
public class MainActivity extends Activity{ 

TextView datumText;
GestureDetector gestureDetector;


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

    // Gestures
    datumText = (TextView) findViewById(R.id.datumText);
    datumText.setText("ma");
    gestureDetector = new GestureDetector(this, new GestureListener());

    datumText.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            gestureDetector.onTouchEvent(event);
            return true;
        }
    });
}
}

class GestureListener implements GestureDetector.OnGestureListener{

MainActivity mainActivity = new MainActivity();

public GestureListener(){

}

@Override
public boolean onDown(MotionEvent e) {
    return false;
}

@Override
public void onShowPress(MotionEvent e) {

}

@Override
public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
    return false;
}

@Override
public void onLongPress(MotionEvent e) {

}

@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    mainActivity.datumText.setText("di");
    return true;
}
}

I want to change the text on

TextView datumText;

whenever I do the onFling motion on the TextView on-screen. But I am getting a NullPointerException and I think it is because I am trying to call the setText() method on a null object.

Thanks in advance!

EDIT: Forgot to mention I use a MainActivity class and a second class. EDIT2: I know what the NullPointerException is caused by but I don't know how to solve it.

GeeSplit
  • 53
  • 3
  • 12
  • Looks like you have a problem with your xml file – Jens Jan 16 '16 at 20:40
  • what do you mean by Second Activity? – Shahar Jan 16 '16 at 20:52
  • I have a MainActivity class, where the onCreate method is at and a different class called GestureListener for the gestures. I followed this to use the gesture on a TextView: http://stackoverflow.com/questions/25594826/gesturedetector-bound-to-a-textview-in-a-fragment – GeeSplit Jan 16 '16 at 20:57
  • Never mind, got it working! I placed the GestureListener class in the wrong place. – GeeSplit Jan 19 '16 at 06:51

2 Answers2

0

You are creating a New Instance of your MainActivity and without inflating any Layout to that, you are trying to access a TextView that don't even exist in that newly created MainActivity Instance. Just change your onFling() to this

  @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    datumText.setText("di"); // you don't need to create instance of your MainActivity to access the TextView
    return true; }
Mukesh Rana
  • 4,051
  • 3
  • 27
  • 39
0

dont create a new Instance of MainActivity like that: MainActivity mainActivity = new MainActivity();

You can pass the TextView itself if you like.

class GestureListener implements GestureDetector.OnGestureListener{

...
...
TextView tv;

public void setTextView(Textview tv){
this.tv = tv;
}
...
}

and then do something like

  @Override public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    tv.setText("di"); 
    return true; }
Shahar
  • 3,692
  • 4
  • 25
  • 47