Unknoledge crash app ImageView

  • Replies:7
Gennaro Diego
  • Forum posts: 10

Sep 23, 2018, 12:16:56 AM via Website

Hello,
I will make a Splash screen with a small game, the person between wait the loading can press 10 coins show inside the screen.
The coins are with different value... 1-2-5-10 ecc. and for choose the value I use pseudo-random numbers.
The problem born when I try change the original image loaded by Android when create the ImageView (ids: coin01...coin10).
I used the codes with I found inside the forum and Google but without solve the problem.
I lost more hours and I hope found people can help me.

Logcat:

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.

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);

}

}

Obviously the code is optimized, the intention is to make 10 tokens appear pseudo-random value as you can understand from the if.

Waiting for your help.

With best regards

Reply
James Watson
  • Forum posts: 1,584

Sep 23, 2018, 2:03:14 AM via Website

The ImageView img01 is just a local variable in that {}.

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Sorin
Reply
Gennaro Diego
  • Forum posts: 10

Sep 23, 2018, 2:20:21 AM via Website

Hello mister James Watson,
yes img01 is a local variable, inside the main activity have ten ImageView with ids coin01 ecc.

What I do for replace the image inside ImageView (coin01) by Java code based on Random nnumbers?

I lost more hours without solution. (thinking)

Helpful?
Sorin
Reply
James Watson
  • Forum posts: 1,584

Sep 23, 2018, 3:25:54 PM via Website

Why not to step debug to check if image01 or R.drawable.moneta_1_souls is null?

Download size < 0.15 MB. But also accurate enough, ad-free & free.
The minimalist app available on Play Store: https://goo.gl/ws42fN
Blog: https://okblackcafe.blogspot.com Your 5-star is appreciated.

Helpful?
Sorin
Reply
Gennaro Diego
  • Forum posts: 10

Sep 23, 2018, 6:08:13 PM via Website

Good question,
I try use this function, I select left side and appear the red dot but the app crash and I cannot see the value. Sure I wrong the method but I think it same with Visual basic compiler (red dot, run app and the software stop were is positioned the debug dot and I can read the value of variables). It's right?
I'm a beginner with android, it's easy for my wrong the code :'(

Helpful?
Sorin
Reply
Gennaro Diego
  • Forum posts: 10

Sep 23, 2018, 6:13:01 PM via Website

It'is possible with the name of images create problem?
I know with android studio don't accept image name with capital letter and/or space but the signal _ isn't possible use?
Because the code for change the image it's correct... maybe (angel)

Helpful?
Sorin
Reply
Gennaro Diego
  • Forum posts: 10

Sep 25, 2018, 8:27:51 AM via Website

Solved, the problem born with setContent position, afternoon I post the good code for help other people with same problem.

Helpful?
Sorin
Reply
Gennaro Diego
  • Forum posts: 10

Sep 25, 2018, 1:22:02 PM via Website

As the solution was promised, the problem arose from the incorrect positioning of setContent.

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);

}

}

As you can see, in the old code (in wrong mariera) was placed before the timer. Now I can not tell you why android studio thought the imageView null but doing this change of position and changing the names of the images I solved. coin, token and other in the name of the image considered them wrong and consequently I renamed them but were not the names to crash the app.

**setContentView(R.layout.activity_main);**

    new Handler().postDelayed(new Runnable() {
        @Override
        public void run() {
            startActivity(new Intent(MainActivity.this, Start.class));
            finish();
        }
    }, 50000);

while now:

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);**

See you next time (cool)

Helpful?
Sorin
Reply