+ Rispondi al Thread
Visualizzazione dei risultati da 1 a 3 su 3

Discussione: Problema cambiare immagine visualizzata su ImageView

  1. #1
    L'avatar di devildragon
    devildragon non è in linea Scolaretto
    Luogo
    Sarzana (SP)
    Post
    105

    Question Problema cambiare immagine visualizzata su ImageView

    Buongiorno,
    durante il caricamento dell'app ho la necessità di far comparire dei gettoni nella Splashscreen per fare in modo che l'utente mentre attende giustamente non si annoia.
    L'app crasha senza un apparente motivo perchè a quanto ho capito dice che il ImageView è nullo mentre in realta esiste.
    sull'activity sono presenti 10 ImageView, ognuno chiamato coin01 etc.
    l'errore visualizzato in android studio è:

    E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gendiripal.mysticwar, PID: 25563
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gendiripal.mysticwar/com.example.gendiripal.mysticwar.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2984)
    at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:3045)
    at android.app.ActivityThread.-wrap14(ActivityThread.java)
    at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1642)
    at android.os.Handler.dispatchMessage(Handler.java:10 2)
    at android.os.Looper.loop(Looper.java:154)
    at android.app.ActivityThread.main(ActivityThread.jav a:6776)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:1518)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:1408)
    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference
    at com.example.gendiripal.mysticwar.MainActivity.onCr eate(MainActivity.java:42)
    at android.app.Activity.performCreate(Activity.java:6 956)
    at android.app.Instrumentation.callActivityOnCreate(I nstrumentation.java:1126)
    at android.app.ActivityThread.performLaunchActivity(A ctivityThread.java:2927)
    at android.app.ActivityThread.handleLaunchActivity(Ac tivityThread.java:3045)*
    at android.app.ActivityThread.-wrap14(ActivityThread.java)*
    at android.app.ActivityThread$H.handleMessage(Activit yThread.java:1642)*
    at android.os.Handler.dispatchMessage(Handler.java:10 2)*
    at android.os.Looper.loop(Looper.java:154)*
    at android.app.ActivityThread.main(ActivityThread.jav a:6776)*
    at java.lang.reflect.Method.invoke(Native Method)*
    at com.android.internal.os.ZygoteInit$MethodAndArgsCa ller.run(ZygoteInit.java:1518)*
    at com.android.internal.os.ZygoteInit.main(ZygoteInit .java:1408)*
    Application terminated.

    Questo è il codice:

    codice:
    package com.example.gendiripal.mysticwar;
    
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageView;
    import android.widget.TextView;
    
    import java.util.Random;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    
            /*Random Number01*/
            Random rand01 = new Random();
            int numb01 = rand01.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number01 = String.valueOf(numb01);
            if (Number01.equals("1")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_1_soul);
            } else if (Number01.equals("2")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_2_souls);
            } else if (Number01.equals("3")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_5_souls);
            } else if (Number01.equals("4")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_10_souls);
            } else if (Number01.equals("5")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_20_souls);
            } else if (Number01.equals("6")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_50_souls);
            } else if (Number01.equals("7")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_100_souls);
            } else if (Number01.equals("8")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_200_souls);
            } else if (Number01.equals("9")) {
                ImageView img01= (ImageView) findViewById(R.id.coin01);
                img01.setImageResource(R.drawable.moneta_500_souls);
            }
            /*Random Number02*/
            Random rand02 = new Random();
            int numb02 = rand02.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number02 = String.valueOf(numb02);
            /*Random Number03*/
            Random rand03 = new Random();
            int numb03 = rand03.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number03 = String.valueOf(numb03);
            /*Random Number04*/
            Random rand04 = new Random();
            int numb04 = rand04.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number04 = String.valueOf(numb04);
            /*Random Number05*/
            Random rand05 = new Random();
            int numb05 = rand05.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number05 = String.valueOf(numb05);
            /*Random Number06*/
            Random rand06 = new Random();
            int numb06 = rand06.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number06 = String.valueOf(numb06);
            /*Random Number07*/
            Random rand07 = new Random();
            int numb07 = rand07.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number07 = String.valueOf(numb07);
            /*Random Number08*/
            Random rand08 = new Random();
            int numb08 = rand08.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number08 = String.valueOf(numb08);
            /*Random Number09*/
            Random rand09 = new Random();
            int numb09 = rand09.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number09 = String.valueOf(numb09);
            /*Random Number10*/
            Random rand10 = new Random();
            int numb10 = rand10.nextInt(10)+1; // Gives n such that 0 <= n < 10
            String Number10 = String.valueOf(numb10);
    
            setContentView(R.layout.activity_main);
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(MainActivity.this, Start.class));
                    finish();
                }
            }, 50000);
    
        }
    
    
    }
    Ovviamente il codice è ottimizzabile, l'intenzione è far apparire 10 gettoni dal valore pseudo-random come potete capire dal bocco if.

    Aspettando un vostro aiuto.

    Distinti saluti

  2. #2
    L'avatar di devildragon
    devildragon non è in linea Scolaretto
    Luogo
    Sarzana (SP)
    Post
    105
    Risolto, nel pomeriggio posto la soluzione per altri che possono incappare nello stesso problema.

  3. #3
    L'avatar di devildragon
    devildragon non è in linea Scolaretto
    Luogo
    Sarzana (SP)
    Post
    105
    Come promesso posto la soluzione, il problema nasceva dall'errato posizionamento di setContent.

    codice:
    package com.example.gendiripal.mysticwar;
    
    import android.content.Intent;
    import android.content.pm.ActivityInfo;
    import android.os.Handler;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.util.DisplayMetrics;
    import android.view.Window;
    import android.view.WindowManager;
    import android.widget.ImageView;
    
    import java.util.Random;
    
    public class MainActivity extends AppCompatActivity {
    
        int accumulator01 = 0;
        int accumulator02 = 0;
        int accumulator03 = 0;
        int accumulator04 = 0;
        int accumulator05 = 0;
        int accumulator06 = 0;
        int accumulator07 = 0;
        int accumulator08 = 0;
        int accumulator09 = 0;
        int accumulator10 = 0;
        int salary = 0;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(R.layout.activity_main);
    
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay()
                    .getMetrics(metrics);
            int width = metrics.widthPixels;
            int height = metrics.heightPixels;
    
            /*Random Number01*/
            Random rand01 = new Random();
            int numb01 = rand01.nextInt(10)+1; // Gives n such that 0 <= n < 10
            Random randomX01 = new Random();
            int X01 = randomX01.nextInt(width)+1;
            Random randomY01 = new Random();
            int Y01 = randomY01.nextInt(height)+1;
            if (numb01 == 1) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon001souls);
                accumulator01 =  1;
            } else if (numb01 == 2) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon002souls);
                accumulator01 =  2;
            } else if (numb01 == 3) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon005souls);
                accumulator01 =  5;
            } else if (numb01 == 4) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon010souls);
                accumulator01 =  10;
            } else if (numb01 == 5) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon020souls);
                accumulator01 =  20;
            } else if (numb01 == 6) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon050souls);
                accumulator01 =  50;
            } else if (numb01 == 7) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon100souls);
                accumulator01 =  100;
            } else if (numb01 == 8) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon200souls);
                accumulator01 =  200;
            } else if (numb01 == 9) {
                ((ImageView)findViewById(R.id.coin01)).setImageResource(R.drawable.mon500souls);
                accumulator01 =  500;
            }
            /*Random Number02*/
            Random rand02 = new Random();
            int numb02 = rand02.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb02 == 1) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon001souls);
                accumulator02 =  1;
            } else if (numb02 == 2) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon002souls);
                accumulator02 =  2;
            } else if (numb02 == 3) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon005souls);
                accumulator02 =  5;
            } else if (numb02 == 4) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon010souls);
                accumulator02 =  10;
            } else if (numb02 == 5) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon020souls);
                accumulator02 =  20;
            } else if (numb02 == 6) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon050souls);
                accumulator02 =  50;
            } else if (numb02 == 7) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon100souls);
                accumulator02 =  100;
            } else if (numb02 == 8) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon200souls);
                accumulator02 =  200;
            } else if (numb02 == 9) {
                ((ImageView)findViewById(R.id.coin02)).setImageResource(R.drawable.mon500souls);
                accumulator02 =  500;
            }
            /*Random Number03*/
            Random rand03 = new Random();
            int numb03 = rand03.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb03 == 1) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon001souls);
                accumulator03 =  1;
            } else if (numb03 == 2) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon002souls);
                accumulator03 =  2;
            } else if (numb03 == 3) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon005souls);
                accumulator03 =  5;
            } else if (numb03 == 4) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon010souls);
                accumulator03 =  10;
            } else if (numb03 == 5) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon020souls);
                accumulator03 =  20;
            } else if (numb03 == 6) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon050souls);
                accumulator03 =  50;
            } else if (numb03 == 7) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon100souls);
                accumulator03 =  100;
            } else if (numb03 == 8) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon200souls);
                accumulator03 =  200;
            } else if (numb03 == 9) {
                ((ImageView)findViewById(R.id.coin03)).setImageResource(R.drawable.mon500souls);
                accumulator03 =  500;
            }
            /*Random Number04*/
            Random rand04 = new Random();
            int numb04 = rand04.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb04 == 1) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon001souls);
                accumulator04 =  1;
            } else if (numb04 == 2) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon002souls);
                accumulator04 =  2;
            } else if (numb04 == 3) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon005souls);
                accumulator04 =  5;
            } else if (numb04 == 4) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon010souls);
                accumulator04 =  10;
            } else if (numb04 == 5) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon020souls);
                accumulator04 =  20;
            } else if (numb04 == 6) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon050souls);
                accumulator04 =  50;
            } else if (numb04 == 7) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon100souls);
                accumulator04 =  100;
            } else if (numb04 == 8) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon200souls);
                accumulator04 =  200;
            } else if (numb04 == 9) {
                ((ImageView)findViewById(R.id.coin04)).setImageResource(R.drawable.mon500souls);
                accumulator04 =  500;
            }
            /*Random Number05*/
            Random rand05 = new Random();
            int numb05 = rand05.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb05 == 1) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon001souls);
                accumulator05 =  1;
            } else if (numb05 == 2) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon002souls);
                accumulator05 =  2;
            } else if (numb05 == 3) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon005souls);
                accumulator05 =  5;
            } else if (numb05 == 4) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon010souls);
                accumulator05 =  10;
            } else if (numb05 == 5) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon020souls);
                accumulator05 =  20;
            } else if (numb05 == 6) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon050souls);
                accumulator05 =  50;
            } else if (numb05 == 7) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon100souls);
                accumulator05 =  100;
            } else if (numb05 == 8) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon200souls);
                accumulator05 =  200;
            } else if (numb05 == 9) {
                ((ImageView)findViewById(R.id.coin05)).setImageResource(R.drawable.mon500souls);
                accumulator05 =  500;
            }
            /*Random Number06*/
            Random rand06 = new Random();
            int numb06 = rand06.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb06 == 1) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon001souls);
                accumulator06 =  1;
            } else if (numb06 == 2) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon002souls);
                accumulator06 =  2;
            } else if (numb06 == 3) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon005souls);
                accumulator06 =  5;
            } else if (numb06 == 4) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon010souls);
                accumulator06 =  10;
            } else if (numb06 == 5) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon020souls);
                accumulator06 =  20;
            } else if (numb06 == 6) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon050souls);
                accumulator06 =  50;
            } else if (numb06 == 7) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon100souls);
                accumulator06 =  100;
            } else if (numb06 == 8) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon200souls);
                accumulator06 =  200;
            } else if (numb06 == 9) {
                ((ImageView)findViewById(R.id.coin06)).setImageResource(R.drawable.mon500souls);
                accumulator06 =  500;
            }
            /*Random Number07*/
            Random rand07 = new Random();
            int numb07 = rand07.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb07 == 1) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon001souls);
                accumulator07 =  1;
            } else if (numb07 == 2) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon002souls);
                accumulator07 =  2;
            } else if (numb07 == 3) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon005souls);
                accumulator07 =  5;
            } else if (numb07 == 4) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon010souls);
                accumulator07 =  10;
            } else if (numb07 == 5) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon020souls);
                accumulator07 =  20;
            } else if (numb07 == 6) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon050souls);
                accumulator07 =  50;
            } else if (numb07 == 7) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon100souls);
                accumulator07 =  100;
            } else if (numb07 == 8) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon200souls);
                accumulator07 =  200;
            } else if (numb07 == 9) {
                ((ImageView)findViewById(R.id.coin07)).setImageResource(R.drawable.mon500souls);
                accumulator07 =  500;
            }
            /*Random Number08*/
            Random rand08 = new Random();
            int numb08 = rand08.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb08 == 1) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon001souls);
                accumulator08 =  1;
            } else if (numb08 == 2) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon002souls);
                accumulator08 =  2;
            } else if (numb08 == 3) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon005souls);
                accumulator08 =  5;
            } else if (numb08 == 4) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon010souls);
                accumulator08 =  10;
            } else if (numb08 == 5) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon020souls);
                accumulator08 =  20;
            } else if (numb08 == 6) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon050souls);
                accumulator08 =  50;
            } else if (numb08 == 7) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon100souls);
                accumulator08 =  100;
            } else if (numb08 == 8) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon200souls);
                accumulator08 =  200;
            } else if (numb08 == 9) {
                ((ImageView)findViewById(R.id.coin08)).setImageResource(R.drawable.mon500souls);
                accumulator08 =  500;
            }
            /*Random Number09*/
            Random rand09 = new Random();
            int numb09 = rand09.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb09 == 1) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon001souls);
                accumulator09 =  1;
            } else if (numb09 == 2) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon002souls);
                accumulator09 =  2;
            } else if (numb09 == 3) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon005souls);
                accumulator09 =  5;
            } else if (numb09 == 4) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon010souls);
                accumulator09 =  10;
            } else if (numb09 == 5) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon020souls);
                accumulator09 =  20;
            } else if (numb09 == 6) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon050souls);
                accumulator09 =  50;
            } else if (numb09 == 7) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon100souls);
                accumulator09 =  100;
            } else if (numb09 == 8) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon200souls);
                accumulator09 =  200;
            } else if (numb09 == 9) {
                ((ImageView)findViewById(R.id.coin09)).setImageResource(R.drawable.mon500souls);
                accumulator09 =  500;
            }
            /*Random Number10*/
            Random rand10 = new Random();
            int numb10 = rand10.nextInt(10)+1; // Gives n such that 0 <= n < 10
            if (numb10 == 1) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon001souls);
                accumulator10 =  1;
            } else if (numb10 == 2) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon002souls);
                accumulator10 =  2;
            } else if (numb10 == 3) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon005souls);
                accumulator10 =  5;
            } else if (numb10 == 4) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon010souls);
                accumulator10 =  10;
            } else if (numb10 == 5) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon020souls);
                accumulator10 =  20;
            } else if (numb10 == 6) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon050souls);
                accumulator10 =  50;
            } else if (numb10 == 7) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon100souls);
                accumulator10 =  100;
            } else if (numb10 == 8) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon200souls);
                accumulator10 =  200;
            } else if (numb10 == 9) {
                ((ImageView)findViewById(R.id.coin10)).setImageResource(R.drawable.mon500souls);
                accumulator10 =  500;
            }
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(MainActivity.this, Start.class));
                    finish();
                }
            }, 5000);
    
        }
    
    
    }
    Come potrete notare, nel vecchio codice (in mariera errata) veniva posizionato prima del timer. Ora non sò dirvi perchè android studio ritenesse le imageView null ma facendo questo cambio di posizione e cambiando i nomi delle immagini ho risolto. moneta, gettone ed altro nel nome dell'immagine li riteneva errati e di conseguenza li ho rinominati ma non erano i nomi a far crashare l'app.

    codice:
    setContentView(R.layout.activity_main);
    
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    startActivity(new Intent(MainActivity.this, Start.class));
                    finish();
                }
            }, 50000);
    mentre adesso:

    codice:
    requestWindowFeature(Window.FEATURE_NO_TITLE);
            getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(R.layout.activity_main);
    
    Alla prossima

+ Rispondi al Thread

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi