• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Need Help Printing to TextView

IIMON

Lurker
I'm extremely new to Java, but I still decided to make an android game.
It's a decision maker; you can toss a coin, roll a die, or....something else I'm planning ;)

Anyways, I'm having trouble with printing some text to a TextView. All I want to happen is that when a random number is rolled, it will either display "Heads" or "Tails."

This is all I've come up with, but every time I attempt to run it I'm getting a fatal error and the app closes.

Java:
package com.me.koteg.dmaker;

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

import java.util.Random;

public class MainActivity extends AppCompatActivity {

    [USER=1021285]@override[/USER]
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void generate(View view, TextView rd) {
        Random rand = new Random();

        int number = rand.nextInt(10);

        if (number >= 5)
            rd.setText("Heads");

        if (number < 5 )
            rd.setText("Tails");

        TextView myText = (TextView)findViewById(R.id.txtDisplay);
        String myString = String.valueOf(rd);
        myText.setText(myString);

    }
}

Any help would be appreciated!

EDIT: Her'es the stack trace from the Logcat:

Code:
01-05 07:34:50.474 16225-16225/com.me.koteg.dmaker I/art: Late-enabling -Xcheck:jni
01-05 07:34:50.511 16225-16231/com.me.koteg.dmaker I/art: Debugger is no longer active
01-05 07:34:50.512 16225-16225/com.me.koteg.dmaker W/System: ClassLoader referenced unknown path: /data/app/com.me.koteg.dmaker-2/lib/arm
01-05 07:34:50.617 16225-16246/com.me.koteg.dmaker D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-05 07:34:50.689 16225-16246/com.me.koteg.dmaker I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 10/21/15, 369a2ea, I96aee987eb
01-05 07:34:50.705 16225-16246/com.me.koteg.dmaker I/OpenGLRenderer: Initialized EGL, version 1.4
01-05 07:34:58.461 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:02.857 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:03.799 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:04.624 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:05.008 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:05.383 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:05.792 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:06.159 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:06.359 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:06.559 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:06.752 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:06.943 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:07.134 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:07.334 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:10.525 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:10.985 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:11.343 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:11.767 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:12.193 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:12.543 16225-16225/com.me.koteg.dmaker I/System.out: Tails
01-05 07:35:12.943 16225-16225/com.me.koteg.dmaker I/System.out: Heads
01-05 07:35:13.378 16225-16225/com.me.koteg.dmaker I/System.out: Heads

Also I changed the code a bit, but it still I still have no idea how to make it go to the TextView...

Java:
package com.me.koteg.dmaker;

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

import java.util.Random;

public class MainActivity extends AppCompatActivity {

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

    public void generate(View view) {
        Random rand = new Random();

        int number = rand.nextInt(10);

        if (number >= 5)
            System.out.println("Heads");

        if (number < 5 )
            System.out.println("Tails");




    }

}
 
Last edited:
Code:
package com.example.roll;







import java.util.Random;




import android.app.Activity;

import android.os.Bundle;

import android.os.Handler;

import android.util.Log;

import android.view.View;

import android.widget.TextView;







public class MainActivity extends Activity {




    private static final String CONSOLE_TRACE_LOG= "Console-Trace-Log";




    Random rand = null;










    //roll dice only 12 if 2 dice? 6 options each dice

    int rolldice = 0;







     TextView myText = null;

     String myString = "null";

 

 




    View view = null;




    private Handler myhandler = new Handler();

    int update =0;




    [USER=1021285]@override[/USER]

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

    

        myText = (TextView)findViewById(R.id.roll);

        myString = String.valueOf(rolldice);

        myText.setText(myString);

    

         myhandler.post(mUpdate);




}







    private Runnable mUpdate = new Runnable()

            {

        public void run()

        {

        

            update++;

        

            if(update == 3)

            {

                //will test it to see if it works here let the code do the dice roll for me.

                randomRoll();

            }

        

            if(update == 8 )

            {

                randomRoll();

            

            

                //will reset to make it go quicker then waiting 100 seconds

            

                update = 0;

            }

        

            Log.i(CONSOLE_TRACE_LOG, "Update: "+update );

        

            if (rolldice >= 5)

             {

                myText.setText("Heads");

                Log.i(CONSOLE_TRACE_LOG, "Heads: "+rolldice );

            

             }

         




             if (rolldice < 5 )

             {

                 myText.setText("Tails");

                 Log.i(CONSOLE_TRACE_LOG, "Tails: "+rolldice );

             

             }

        

            //A quick way to update using 100 layers before resets, I use it as the brain

            if(update > 100)

            {

                update = 0;

            }

            myhandler.postDelayed(getmUpdate(), 1000);

        

        }

    

    

    };




    public Runnable getmUpdate() {

        return mUpdate;

    }




    public void setmUpdate(Runnable mUpdate) {

        this.mUpdate = mUpdate;

    }




    public void randomRoll()

    {

        rand = new Random();

    

        rolldice = rand.nextInt(10);

    }







}
 
Last edited:
that's my thought on it! I did test it. but that's how about I would go about it. some thing like that.

it works did that help you any?
 
Last edited:
OP, please include the stack trace from your Logcat view. Then we can diagnose the problem.
Also please put your code in a code insert block. It's hard to read. Thanks.
 
OP, please include the stack trace from your Logcat view. Then we can diagnose the problem.
Also please put your code in a code insert block. It's hard to read. Thanks.
Uhhh, how do you put it in a code block? I only joined yesterday an don't really know anything....
 
When composing your reply, see the icons at the top of the box. Click on the insert icon, and select 'code'
 
If your application closed unexpectedly there would be an exception in the Logcat. What is it?
 

I'm not real sure what the issues is but I do know it was a real pain getting all set up when I first started downloading all the tools needed to create an app. youtube helped a lot getting set up and started! maybe something is missing on your end with the compiler or something how ever I made a video to show you on my end the code works.
 
Back
Top Bottom