Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: cannot update textView text dynamically from onTouchEvent
    primarykey
    data
    text
    <p>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:</p> <pre><code> &lt;test1.DragView android:id="@+id/view" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; &lt;TextView android:id="@+id/textView" android:layout_width="fill_parent" android:layout_height="fill_parent" /&gt; </code></pre> <p>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:</p> <pre><code>@Override public boolean onTouchEvent(MotionEvent event) { x=(int)event.getX(); y=(int)event.getY(); bitmap =BitmapFactory.decodeResource(getResources(), R.drawable.dot); if(x&lt;0) x=0; if(x &gt; width+(bitmap.getWidth()/2)) x=width+(bitmap.getWidth()/2); if(y &lt;0) y=0; if(y &gt; 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; } </code></pre> <p>it gives me errors like </p> <pre><code>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) </code></pre> <p>I cannot use my textView in my DragView class. Is it normal? I can provide more explications if needed.</p> <p>EDIT: After using the solution of ReubenScratton I can acces my textView now but I'm getting the following error:</p> <pre><code>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) </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload