Textview settext ошибка

I know there are a lot of similar threads but I’ve gone through them and still can’t figure out the problem. My program reaches the Handler but it always returns the catch exception «Message isn’t handled.»

I declared the TextView private TextView chatbox;

Under onCreate I have:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpViews();
        setUpListener();
    }

where setUpViews() snippet looks like:

 private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    }

Handler:

public Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            try{
                chatbox.setText("Got it!");
            }catch(Exception e){
                Log.i("MYLOG", "Message was not handled.");
            }

        }
    };

Snippet in main.xml file:

<TextView
        android:id="@+id/chatbox"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

asked Aug 21, 2013 at 0:28

user2562568's user avatar

1

For future reference, I had multiple fragments, one for each tab in my ViewPager. One TextView had the same id in two fragments and that created a conflict. I didn’t realize I had the same id so everything seemed fine, no error message, but the text simply didn’t change. Therefore I’d recommend using unique names for the id.

Nikos Hidalgo's user avatar

answered Jan 23, 2015 at 21:46

Hudson's user avatar

HudsonHudson

8491 gold badge10 silver badges15 bronze badges

2

I hope you handler is running in UI Thread. Also try doing this: Assign your string to a variable and use that variable as it requires charsequence.

String temp = "Got it!";
chatbox.setText(temp);

answered Aug 21, 2013 at 0:36

Sushil's user avatar

SushilSushil

8,2503 gold badges39 silver badges71 bronze badges

You haven’t given us much to go on.

You should look at the exception stack trace, instead of printing a message to the console: e.printStackTrace();

But from here, if I had to guess, it looks like you’re either setting the TextView’s text off the main thread, or — and which appears unlikely based on what you’ve posted — your TextView has not been set and you have a null pointer exception.

answered Aug 21, 2013 at 1:12

Ken's user avatar

KenKen

30.9k34 gold badges116 silver badges155 bronze badges

In my case, I found there are a TextWatcher and a Filter linked to my Textview, which both contain a logic that prevents the updating of my Textview value.
So additionally to other solutions, you can check if your Textview is linked to any TextWatcher or Filter and then trace it.

Nikos Hidalgo's user avatar

answered Jan 2, 2019 at 10:47

Ahmed Nabil's user avatar

Ahmed NabilAhmed Nabil

17.5k12 gold badges61 silver badges88 bronze badges

when the textColor is the same as View’s background color ,the text also sees invisible ,so you can define a textColor to solve it

answered Mar 13, 2020 at 8:48

jiar wang's user avatar

jiar wangjiar wang

3602 silver badges12 bronze badges

just like this is ok!

private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    chatbox.setText("Got it!");
}

answered Aug 21, 2013 at 0:54

have you send a message?
for example:

            Message message=new Message();
        message.what=1;
        mHandler.sendMessage(message);

answered Aug 21, 2013 at 0:37

Su  Zhenpeng's user avatar

I know there are a lot of similar threads but I’ve gone through them and still can’t figure out the problem. My program reaches the Handler but it always returns the catch exception «Message isn’t handled.»

I declared the TextView private TextView chatbox;

Under onCreate I have:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpViews();
        setUpListener();
    }

where setUpViews() snippet looks like:

 private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    }

Handler:

public Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            try{
                chatbox.setText("Got it!");
            }catch(Exception e){
                Log.i("MYLOG", "Message was not handled.");
            }

        }
    };

Snippet in main.xml file:

<TextView
        android:id="@+id/chatbox"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

asked Aug 21, 2013 at 0:28

user2562568's user avatar

1

For future reference, I had multiple fragments, one for each tab in my ViewPager. One TextView had the same id in two fragments and that created a conflict. I didn’t realize I had the same id so everything seemed fine, no error message, but the text simply didn’t change. Therefore I’d recommend using unique names for the id.

Nikos Hidalgo's user avatar

answered Jan 23, 2015 at 21:46

Hudson's user avatar

HudsonHudson

8491 gold badge10 silver badges15 bronze badges

2

I hope you handler is running in UI Thread. Also try doing this: Assign your string to a variable and use that variable as it requires charsequence.

String temp = "Got it!";
chatbox.setText(temp);

answered Aug 21, 2013 at 0:36

Sushil's user avatar

SushilSushil

8,2503 gold badges39 silver badges71 bronze badges

You haven’t given us much to go on.

You should look at the exception stack trace, instead of printing a message to the console: e.printStackTrace();

But from here, if I had to guess, it looks like you’re either setting the TextView’s text off the main thread, or — and which appears unlikely based on what you’ve posted — your TextView has not been set and you have a null pointer exception.

answered Aug 21, 2013 at 1:12

Ken's user avatar

KenKen

30.9k34 gold badges116 silver badges155 bronze badges

In my case, I found there are a TextWatcher and a Filter linked to my Textview, which both contain a logic that prevents the updating of my Textview value.
So additionally to other solutions, you can check if your Textview is linked to any TextWatcher or Filter and then trace it.

Nikos Hidalgo's user avatar

answered Jan 2, 2019 at 10:47

Ahmed Nabil's user avatar

Ahmed NabilAhmed Nabil

17.5k12 gold badges61 silver badges88 bronze badges

when the textColor is the same as View’s background color ,the text also sees invisible ,so you can define a textColor to solve it

answered Mar 13, 2020 at 8:48

jiar wang's user avatar

jiar wangjiar wang

3602 silver badges12 bronze badges

just like this is ok!

private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    chatbox.setText("Got it!");
}

answered Aug 21, 2013 at 0:54

have you send a message?
for example:

            Message message=new Message();
        message.what=1;
        mHandler.sendMessage(message);

answered Aug 21, 2013 at 0:37

Su  Zhenpeng's user avatar

Why does changing the text for a TextView using the setText method not work sometimes? New Android developers sometimes fail to understand why changing the text does not appear to work. The text not updating also applies to other Views as well, such as the EditText and Button. Why this happens is explained in this article. This tutorial is also useful in helping to understand why the User Interface (UI) in your Android app might not be as responsive as expected. The underlying problem is the same. So if you are trying to find out why setText() on EditText, TextView, Button, etc. is not working as intended, or you UI is sluggish, then read on. The example source code includes a work around to help you out.

Android Logo

(To use the code in this tutorial it is assumed that Android Studio is installed, a basic app can be created and run, and the code in this article can be correctly copied into Android Studio. The example code can be changed to meet your own requirements. When entering code in Studio add import statements when prompted by pressing Alt-Enter.)

How to Change the Text on a TextView or EditText

When changing the TextView text the following code, or similar, becomes very familiar to all Android developers:

((TextView) findViewById(R.id.textView1)).setText("New Text");

For the above Java effectively creates an anonymous object and casts it as a TextView object. For those new to programming the above is equivalent to the following two lines but saves having to declare the TextView object.

TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText("New Text");

App Code Runs on a Single Thread and Responds to Events

When setText("New Text") is run the text is not updated immediately. Android is an event based system. Something happens on the device (the screen is touched, a key is pressed, a call comes in, etc.) and Android raises an event. An app is notified of an event and responds to it if required, often running the code that has been written. The app runs its code in a loop under the control of the Android Operating Systems (OS). This code loop is referred to as the app’s thread of execution. There is only one thread and it is responsible for both running the app code and updating the display. The setText call posts a message to update the display, so the update does not happen immediately. Once remaining app code has run the UI messages are processed. It is then that the text changes. A running app’s execution thread can be viewed as shown in this simplified diagram.

App Thread Loop

What this means is that changes to the UI can get delayed, and in some cases not appear to occur, when the running app code is doing a time intensive tasks. Intensive tasks include looping calculations, complex queries to large databases, accessing network resources, etc. A common scenario in not seeing the UI update is along these lines:

((TextView) findViewById(R.id.textView1)).setText("Starting Processing");
boolean processing = true;
int number_processed=0;
do {
    /* Do some processing.
     * processing set false
     * when finished*/
    ++number_processed;
    ((TextView) findViewById(R.id.textView1)).setText( Integer.toString(number_processed)+ " Completed.");
} while(processing);
((TextView) findViewById(R.id.textView1)).setText( "Finished");

What happens is that the Finished message is seen but the progress messages are not.

Clogging Up the App Thread

It is easy to reproduce this scenario by creating a new simple app, dropping a Button on to the default activity_main.xml and then adding the above code to the button’s onClick with a call to sleep() in the loop. Here is an example layout with a Button and TextView:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <Button android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:onClick="ButtonClick"
        android:text="Button" />
    <TextView android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_marginLeft="14dp"
        android:layout_toRightOf="@+id/button1"
        android:text="Click to Start"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

Simple Android App

And the code for the MainActivity.java file. The onClick attribute in the layout is used to wire up the event handler:

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

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

    public void ButtonClick(View arg0){
        ((TextView) findViewById(R.id.textView1)).setText("Starting Processing");
        boolean processing = true;
        int number_processed=0;
        do {
            /* Do some processing. */
            try {
                Thread.sleep(500);//Simulate intensive code
                if(++number_processed>6)
                    processing=false;
            } catch (InterruptedException e) {
                e.printStackTrace();
                processing=false;
            }
            /* processing set false
             * when finished*/
            ((TextView) findViewById(R.id.textView1)).setText( Integer.toString(number_processed)+ " Completed.");
        } while(processing);
        ((TextView) findViewById(R.id.textView1)).setText( "Finished");
    }
}

If the UI is prevented from updating for more than about five seconds then what is known as an ANR (Application Not Responding) error occurs. The user may think the app has crashed completely and force close it, and then probably remove it from the device. Humans can detect systems responding down to tens of milliseconds, at about one tenth of a second pauses begin to degrade the experience of using a program. If no feedback is given for a few seconds users can become annoyed. It is easy to get the no response error with the above code, increase the number for the test in the if statement from six to twelve, if(++number_processed>12), run the app and hit the Button several times.

Application Not Responding

A Helper Class Is Available to Run Another Thread

What is needed is a way to process the UI messages so that the display updates and still runs the intensive code. This is done by getting the app to define another execution thread on which the intensive code is executed. This stops the main thread from appearing to lock up when UI updates can be delay. This diagram show the idea:

Android App with Additional Thread

Android has a built in class to make running intensive code easy, the AsyncTask, it is covered in the Tek Eye tutorial article The Android AsyncTask Class Helps Avoid ANRs. Whenever a section of code is going to take some time to execute, maybe a second or more, then the use of AsyncTask is recommended. However, it can appear overkill when all you want to do is update a TextView to provide feedback before executing some code that may take a second or two.

Example Code That Has Variable Execution Time

The following example is going to be improved to provide user feedback without using AsyncTask. Good for simple feedback scenarios. Though AsyncTask is better for anything beyond a simple feedback message. The code in this example is going to check whether or not a long integer is a primary number or not. (The Java BigInteger class is available for this but the routine here is used as an example.)

A prime number is any number that can be divided by itself and the number one, so the sequence begins 2, 3, 5, 7, 11, 13, 17, 19, 23, etc. and goes on for infinity. Routines to perform a primality test are well established and are usually based upon the Sieve of Eratosthenes.This function was derived from the script at Prime Curios! Primality Test web page.

public static boolean isPrime(long N) {
  // Trial divide the positive integer N by the primes from 2
  // Returns true if a prime divisor found, or false if none found
  if (N%2 == 0) return false;//Eliminates evens
  if (N%3 == 0) return false;//Eliminates multiples of three
  // No need to go past the square root of our number (see Sieve of Eratosthenes)
  long Stop = (long) Math.sqrt(N);
  // Okay, lets "wheel factor" alternately adding 2 and 4
  long di=2;
  for(long i=5; i<=Stop; i+=di, di=6-di) {
    if (N%i == 0) return false;
  };
  return true;
}

For most numbers the routine executes extremely quickly, however, a long integer can have up to 19 digits. If a 19 digit number is a prime then the inner loop can run several million times and a long pause in the program occurs. Here is the layout for the primality testing app.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    <EditText android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="5dp"
        android:layout_marginTop="5dp"
        android:hint="Type a number"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:maxLength="19"
        android:inputType="number"
        android:selectAllOnFocus="true" />
    <Button android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/editText1"
        android:layout_below="@+id/editText1"
        android:layout_marginTop="10dp"
        android:onClick="CheckPrimeClick"
        android:text="Is It a Prime?" />
    <TextView android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/button2"
        android:layout_below="@+id/button2"
        android:layout_marginTop="10dp"
        android:hint="Click 'Is It a Prime?'"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>

Primality Test Running

And the code for the Is Prime app’s MainActivity.java:

package com.example.isprime;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    long lngNum;//store the number to check
    EditText et;//Allow user to type a long
    TextView tv;//Show result of test
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // get a reference to the EditText and TextView
        et = (EditText) findViewById(R.id.editText1);
        tv = (TextView) findViewById(R.id.textView2);
    }

    public void CheckPrimeClick(View arg0) {
        String number = et.getText().toString();
        try {
            lngNum=Long.parseLong(number);
        }catch(Exception ex){
            tv.setText("Error " + ex.getMessage() + " testing " + number);
            return;
        }
        if(isPrime(lngNum)){
            tv.setText(et.getText() + " IS a prime.");
        }else{
            tv.setText(et.getText() + " IS NOT a prime.");
        }
    }

    /* Test for prime numbers
     * http://primes.utm.edu/curios/includes/primetest.php
     * also look at Java BigInteger isProbablePrime
     */
    public static boolean isPrime(long N) {
        // Trial divide the positive integer N by the primes from 2
        // Returns true if a prime divisor found, or false if none found
        if (N%2 == 0) return false;//Eliminates evens
        if (N%3 == 0) return false;//Eliminates multiples of three
        // No need to go past the square root of our number (see Sieve of Eratosthenes)
        long Stop = (long) Math.sqrt(N);
        // Okay, lets "wheel factor" alternately adding 2 and 4
        long di=2;
        for(long i=5; i<=Stop; i+=di, di=6-di) {
            if (N%i == 0) return false;
        };
        return true;
    }
}

When running the primality test app most numbers typed in fail the test immediately. Most numbers are not primes, and there is no problem with the response of the app. Likewise for small prime numbers, such as the eggshell number 77345993. Why eggshell? Well if that number is typed into an old desktop calculator with a Liquid Crystal Display (LCD) and the calculator is turned upside down, then it sort of reads EGGSHELL. Now try a really big prime number, a web search will reveal plenty, how about nineteen ones:

  • 1111111111111111111 — Yes, strangely nineteen ones is a big number and a prime number as well.

Try it in the app and notice that it takes a few seconds for the routine to determine that it is a prime number. If tv.setText("Checking please wait.") is added at the beginning of CheckPrimeClick the same problem as the sleep example occurs. The UI update is blocked by the looping code.

Use A Timer and Handler To Provide Feedback

A useful solution to this problem without using the AsyncTask class is to introduce a very small delay between calling tv.setText and running the isPrime routine. During this delay the main thread continues and thus gets to up date the interface. The delay cannot be a Java sleep delay because that stops the program execution dead. Instead a Timer is used. When the timer’s TimerTask is run after the small delay it sends an empty message that is caught by a Handler and the Handler’s Callback then executes the call to isPrime. A Timer is delared using Timer timer=new Timer(). The Android in built Handler class is used (and the associated Handler.Callback to save the need to implement one). The Callback is declared and code calling isPrime moved into it:

Handler.Callback callback = new Handler.Callback() {
    public boolean handleMessage(Message msg) {
        if(isPrime(lngNum)){
            tv.setText(et.getText() + " IS a prime.");
        }else{
            tv.setText(et.getText() + " IS NOT a prime.");
        }
        return true;
    }
};

The Handler can be newed the onCreate to use the Callback, handler = new Handler(callback). The TimerTask simply posts an empty message using the Handler.

class SmallDelay extends TimerTask {
    public void run() {
        handler.sendEmptyMessage(0);
    }
}

The click handler schedules the delay with timer.schedule(new SmallDelay(), 100). The result is that the UI gets the chance to update whilst the code that was previously blocking the update still executes (after being kicked off by the timer). A straightforward solution for when a quick UI update is required when code can potentially hog the main thread for a short while.

Primality Test Checking

Here is the complete code code for the MainActivity.java with the Timer implemented:

package com.example.isprime;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity {
    long lngNum;//store the number to check
    EditText et;//Allow user to type a long
    TextView tv;//Show result of test
    Timer timer=new Timer();//Used for a delay to provide user feedback
    Handler handler;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // get a reference to the EditText and TextView
        et = (EditText) findViewById(R.id.editText1);
        tv = (TextView) findViewById(R.id.textView2);
        handler = new Handler(callback);
    }

    public void CheckPrimeClick(View arg0) {
        String number = et.getText().toString();
        tv.setText("Checking please wait.");
        try {
            lngNum=Long.parseLong(number);
        }catch(Exception ex){
            tv.setText("Error " + ex.getMessage() + " testing " + number);
            return;
        }
        timer.schedule(new SmallDelay(), 100);
    }

    /* Test for prime numbers
     * http://primes.utm.edu/curios/includes/primetest.php
     * also look at Java BigInteger isProbablePrime
     */
    public static boolean isPrime(long N) {
        // Trial divide the positive integer N by the primes from 2
        // Returns true if a prime divisor found, or false if none found
        if (N%2 == 0) return false;//Eliminates evens
        if (N%3 == 0) return false;//Eliminates multiples of three
        // No need to go past the square root of our number (see Sieve of Eratosthenes)
        long Stop = (long) Math.sqrt(N);
        // Okay, lets "wheel factor" alternately adding 2 and 4
        long di=2;
        for(long i=5; i<=Stop; i+=di, di=6-di) {
            if (N%i == 0) return false;
        };
        return true;
    }

    Handler.Callback callback = new Handler.Callback() {
        public boolean handleMessage(Message msg) {
            if(isPrime(lngNum)){
                tv.setText(et.getText() + " IS a prime.");
            }else{
                tv.setText(et.getText() + " IS NOT a prime.");
            }
            return true;
        }
    };

    class SmallDelay extends TimerTask {
        public void run() {
            handler.sendEmptyMessage(0);
        }
    }
}

Do not forget that for more complex UI feedback, including showing an active progress bar, then the AsyncTask solution is better.

See Also

  • Download the final code for this example, available in is-prime-app.zip
  • See the Android AsyncTask Class Helps Avoid ANRs article for an AsyncTask example.
  • See the Android Example Projects page for more sample projects with source code.
  • For a full list of all the articles in Tek Eye see the full site alphabetical Index.

Archived Comments

Fredo Velasco in January 2018 said: This whole thing was super confusing to me until I realized that the most important part of all this is:

The setText call posts a message to update the display, so the update does not happen immediately. Once remaining app code has run the UI messages are processed. It is then that the text changes.

Which means that, if you call setText(), a message is posted to be fulfilled WHEN CODE AFTER THE CALL IS FINISHED. So if you have code that would take a while, right after your UI updating code, like finding a prime number, or setting other views etc, the setText() call will not be fulfilled until that other code is run.

So to have instantly updating UI, you should:

  1. Update your UI (by calling setText() etc) like you’re already doing, and then
  2. Run the rest of the code after a small pause (with Handler.postDelayed() in Kotlin, or solution stated right here) or on a different thread with AsyncTask() or RxJava etc.

Don’t use Thread.sleep() because that won’t allow the UI thread to fulfill the UI updating code, it will just halt all execution; what you want to do is to post the message to update UI (with setText() etc.), and give the UI thread extra time to get the message, service the message request and update the UI before it has to start working on the code after the setText() call.

Dan at Tek Eye in January 2018 said: Yes, exactly. Perfect comment.

Author:  Published:  Updated:  

I don’t know why I am getting this error in Android Studio, this is quite a simple command that I am executing. The code is provided below. Any help is greatly appreciated. I am getting the error in the last line of code.

 String[] sadThought = {"a", "sad", "Thought"};
 String[] sadRandom ={(sadThought[new Random().nextInt(sadThought.length)])};

    TvSad.setText(sadRandom);

asked Jul 23, 2015 at 15:29

user5145575's user avatar

2

Simply use this

TvSad.setText(sadThought[new Random().nextInt(sadThought.length)]);

answered Jul 23, 2015 at 15:32

Sanjeev's user avatar

SanjeevSanjeev

4,2753 gold badges28 silver badges37 bronze badges

7

You dont have to create a separate array to get a random string from your sadThought array. You can try:

TvSad.setText(sadThought[new Random().nextInt(sadThought.length)]);

Also, make sure your TvSad is an instance of textView or EditText or other widgets that support .setText(..) method.

answered Jul 23, 2015 at 15:35

MetaSnarf's user avatar

MetaSnarfMetaSnarf

5,8673 gold badges25 silver badges41 bronze badges

Я знаю, что существует много подобных потоков, но я прошел через них и до сих пор не могу понять проблему. Моя программа достигает Handler, но всегда возвращает исключение catch. Сообщение не обрабатывается.

Я объявил TextView private TextView chatbox;

В onCreate у меня есть:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        setUpViews();
        setUpListener();
    }

где фрагмент setUpViews() выглядит следующим образом:

 private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    }

Handler:

public Handler mHandler = new Handler(Looper.getMainLooper()){
        @Override
        public void handleMessage(Message msg){
            try{
                chatbox.setText("Got it!");
            }catch(Exception e){
                Log.i("MYLOG", "Message was not handled.");
            }

        }
    };

Снимок в файле main.xml:

<TextView
        android:id="@+id/chatbox"
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

Поделиться

Источник

6 ответов

Если кто-то из бедных душ Гугл после этого вопроса в будущем, я, наконец, узнал об этом. У меня было несколько фрагментов, по одному для каждой вкладки в моем ViewPager, где TextView имел один и тот же идентификатор в двух фрагментах и ​​поэтому попал в конфликт. Я не понимал, что у меня такой же идентификатор, поэтому все казалось прекрасным, никаких сообщений об ошибке, но текст просто не изменился. Просто измените идентификатор.

Hudson

Поделиться

Вы не дали нам многого для продолжения.

Вы должны посмотреть трассировку стека исключений, вместо того, чтобы печатать сообщение на консоли: e.printStackTrace();

Но отсюда, если бы я должен был догадаться, похоже, что вы либо устанавливаете текст TextView из основного потока, либо — и который кажется маловероятным на основе того, что вы опубликовали, — ваш TextView не был установлен и у вас есть исключение с нулевым указателем.

SK9

Поделиться

В моем случае
Я обнаружил, что есть TextWatcher и Filter связанный с моим Textview
Которые оба содержат логику, препятствующую обновлению моего значения Textview
Так в дополнение к другим решениям
Вы можете проверить, связано ли ваше Textview с каким-либо TextWatcher или Filter затем проследить его.

ahmednabil88

Поделиться

как это хорошо!

private void setUpViews() {
    chatbox = (TextView) findViewById(R.id.chatbox);
    chatbox.setText("Got it!");
}

user1615651

Поделиться

Я надеюсь, что ваш обработчик работает в потоке пользовательского интерфейса. Также попробуйте сделать это: присвойте свою строку переменной и используйте эту переменную, поскольку она требует charsequence.

String temp = "Got it!";
chatbox.setText(temp);

Sushil

Поделиться

отправить сообщение?
например:

            Message message=new Message();
        message.what=1;
        mHandler.sendMessage(message);

Su Zhenpeng

Поделиться

Ещё вопросы

  • 1PropertyChangedEvent запускается несколько раз
  • 0Simple_html_dom: как удалить все элементы со значением атрибута, кроме первого?
  • 0Как вычесть месяцы из даты и справиться с переполнением?
  • 0векторная позиция c ++ установлена в 1 вместо 0
  • 0Google API получает ярлыки сообщений и вложенные ярлыки
  • 0Запретить ввод, пока не закончится первое действие?
  • 1Агент Python 3 Paramiko SSH переадресовывать через хост с удаленной командой на третьем хосте
  • 0Директива общего доступа между приложениями в AngularJS
  • 0Несколько, если операторы не работают
  • 1что означает и использует оператор -> в граалях?
  • 1Выполнить добавление / операцию с данными из Firebase Firestore Android
  • 1Исключение, которое выдается, когда переменные экземпляра равны нулю
  • 1Asterisk Forbidden Error
  • 1Не использовать Entity Framework для моделей
  • 1Highcharts — столбцы гистограммы слишком тонкие, слишком много серий
  • 0Функция заголовка php перенаправляет только на локальные страницы
  • 0Два деления в одной строке
  • 0Пункты меню с разной шириной
  • 1Ошибка регистрационного ключа при использовании «extra»
  • 1Javascript ES6 Получить JSON из URL (без jQuery)
  • 0JS не может передать значение элементу ввода текста родителя [странное поведение]
  • 0mmap и выравнивание страниц данных — это увеличивает производительность?
  • 1Где находится MVC в веб-API?
  • 0Laravel Digital Ocean Ошибка в обработчике исключений
  • 1div видимый false на стороне сервера с использованием класса div в c #
  • 1Вызов хранимой процедуры от Lightswitch
  • 0Создать простой список задач J Query
  • 1Преобразование миллисекунд в объект даты UTC с часовым поясом UTC
  • 0MySQL Connector не работает с псевдонимами таблиц
  • 0Как показать изображение и контент из базы данных на страницу PHP?
  • 0.htaccess не перенаправляет со старого URL на новый URL 3
  • 0Ошибка MySQL: 1251 не может добавить внешний ключ
  • 1Пропустить конвертацию сущностей при загрузке строки yaml (используя PyYAML)
  • 0метод виджета не вызывается
  • 1Не удается сохранить нового пользователя с Express & Mongoose
  • 0Каковы отношения веб-фреймворков и других
  • 1Кассандра, прочитайте значение карты типа столбца <>, используя ByteBufferUtil (org.apache.cassandra.utils)
  • 1Попытка понять сложность пространства вывода каскадных строк
  • 0холст изображение на php
  • 0Изменение фона тела при наведении на ссылку
  • 0Авто нажмите кнопку выбора файла для HTML-формы
  • 1Как я могу вставить магазин в панель инструментов панели?
  • 0Программа SFML 2.1 работает нормально в режиме отладки, но вылетает в режиме выпуска
  • 0Добавление коробки количества к списку товаров magento
  • 1Google Cloud Messaging — не зарегистрирован на Android 9.0 Pie
  • 0push () в глубокий массив
  • 0Странное поведение file_get_contents
  • 0Не удается заставить этот модульный тест (на помощнике) работать без исключений — AngularJS, Karma, Jasmine
  • 0Angularjs не публиковать скрытые значения
  • 1java.io.NotSerializableException :: ObjectOutputStream: метод writeObject

Сообщество Overcoder

Понравилась статья? Поделить с друзьями:
  • Textnow ошибка при регистрации
  • Teso ошибка 302
  • Teso ошибка 301 не удалось подключиться к серверу
  • Teso ошибка 210 не удалось загрузить манифест обновления
  • Teso ошибка 210 an error occurred