0

I cannot manage to display each item of a String in a Java/Android app. Here is my code :

        int i;
        Button changer;
        private EditText changerlettres;
        String choix;

        @SuppressLint("ClickableViewAccessibility")
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

        changerlettres =  (EditText)findViewById(R.id.changerlettres);

            changer = (Button)this.findViewById(R.id.changer);
            BtnClick();


    }
public void BtnClick() {
    changer.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            choix = changerlettres.getText().toString();

            for (int i = 0, n = choix.length(); i < n; i++) {
                char letter = choix.charAt(i);

System.out.println(letter); //Does not display anything
Toast.makeText(MainActivity.this,choix,Toast.LENGTH_SHORT).show();// Displays correctly
Toast.makeText(MainActivity.this,letter,Toast.LENGTH_SHORT).show(); // Crash of the app

What should be the best way to display the string items one by one ? Many thanks for your help

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
alhambra60
  • 11
  • 3

4 Answers4

2

The Reason why your app crashes is because you cannot show more than one Toast at a time. If you want to show multiple data, you need to threads to display one after the other or you can just concatenate both the strings and show them.

But if you just want to read the data for debugging purpose, you can log them into the console using Log.d() method.

Log.d("MainActivity", "onClick() called with: choix = [" + choix + "]");
Log.d("MainActivity", "onClick() called with: letter = [" + letter + "]");

hope this will help you..!

Vns Aditya
  • 445
  • 2
  • 13
1

see if this thread Why doesn't "System.out.println" work in Android? helps you.

Summary - "On the emulator and most devices System.out.println gets redirected to LogCat and printed using Log.i(). This may not be true on very old or custom Android versions." There is no console to send log messages to sysout in devices sometimes.

1

Fix crash app (toast)

Change this :

Toast.makeText(MainActivity.this,letter,Toast.LENGTH_SHORT).show();

to this :

Toast.makeText(MainActivity.this,String.valueOf(letter),Toast.LENGTH_SHORT).show();
Siros Baghban
  • 406
  • 4
  • 7
0

Try the code below:

Toast toast;
MyHandler myHandler;

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

    toast = Toast.makeText(this, "", Toast.LENGTH_SHORT);
    myHandler = new MyHandler(this);

    new Thread(new Runnable() {
        @Override
        public void run() {
            String choix = "hello world, orzangleli";
            myHandler.obtainMessage(0, choix).sendToTarget();
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 0, n = choix.length(); i < n; i++) {
                char letter = choix.charAt(i);
                Log.i("tag", String.valueOf(letter)); //Does not display anything
                myHandler.obtainMessage(1, String.valueOf(letter)).sendToTarget();
                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();

}

class MyHandler extends Handler{
    private WeakReference<Activity> activityWeakReference;
    MyHandler(Activity activity) {
        activityWeakReference = new WeakReference<Activity>(activity);
    }

    @Override
    public void handleMessage(Message msg) {
        super.handleMessage(msg);
        if (msg.what == 0) {
            String letter = (String) msg.obj;
            toast.setText(letter);
            toast.show();
        } else if (msg.what == 1) {
            String letter = (String) msg.obj;
            toast.setText(letter);
            toast.show();
        }
    }
}
orzangleli
  • 178
  • 7