Anila
Anila

Reputation: 1136

Android: cannot update textView text dynamically from onTouchEvent

I am working on an application that allows user to drag a dot on a screen and set the values of distance for example. What I want is to have half screen for drag function and the other half with gadgets (buttons and textViews). To do so I created a class extending SurfaceView, used a bitmap "dot" and the function onTouchEvent and in my xml file I referenced like following to my view:

 <test1.DragView
        android:id="@+id/view"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
     />
 <TextView
            android:id="@+id/textView"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
  />

Which gives me what I want. But now I want to dynamically update the position of the dot. To do so I added setText() function for my textView in my onTouchEvent function:

@Override
public boolean onTouchEvent(MotionEvent event) {
    x=(int)event.getX();
    y=(int)event.getY();
    bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.dot);
    if(x<0)
        x=0;
    if(x > width+(bitmap.getWidth()/2))   
        x=width+(bitmap.getWidth()/2);
    if(y <0)
        y=0;
    if(y > height/2)
        y=height/2;
    tv=(TextView) findViewById(R.id.textView); //I declared tv in the beginning of my class
    tv.setText(x);
    updateBall(); //it's a function that resets the position of the dot

    return true;
}

it gives me errors like

AndroidRuntime(845): FATAL EXCEPTION: main
AndroidRuntime(845): java.lang.NullPointerException
AndroidRuntime(845):    
at test1.DragView.onTouchEvent(DragView.java:87)
AndroidRuntime(845):    
at android.view.View.dispatchTouchEvent(View.java:5462)
AndroidRuntime(845):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:1953)

I cannot use my textView in my DragView class. Is it normal? I can provide more explications if needed.

EDIT: After using the solution of ReubenScratton I can acces my textView now but I'm getting the following error:

AndroidRuntime(672): FATAL EXCEPTION: main
AndroidRuntime(672): android.content.res.Resources$NotFoundException: String resource ID #0x109
AndroidRuntime(672):    at android.content.res.Resources.getText(Resources.java:247)
AndroidRuntime(672):    at android.widget.TextView.setText(TextView.java:3427)
AndroidRuntime(672):    at test1.DragView.onDraw(DragView.java:73)

Upvotes: 0

Views: 2142

Answers (1)

Anila
Anila

Reputation: 1136

I am answering my own question but note that I'm just putting together the solutions given by others (ReubenScratton and lazeR) so that if someone else is having the same problem he'll find the whole solution. So the solution: first of all instead of accessing my textView directly I had to use

 tv=(TextView) ((Activity)getContext()).findViewById(R.id.textView)

because if you use

tv=(TextView)findViewById();

You're using View.findViewById() which will only search through child views.

You want to use the Activity.findViewById() ReubenScratton

And for my second problem as I was using directly an int to the setText() function it wasn't working but thanks to lazeR's comment I noticed it and found the solution. Thanks to all those who helped me :).

Upvotes: 1

Related Questions