mercredi 21 décembre 2016

If statement are not making Images visible

What I want to do is that a image that was invisible becomes visible again by pressing a button. I tried to do that with the following code but it doesn't work. If I press the button the app crashes. I'm very new at java I learned the codes by looking at youtube

This is my java file

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


import java.util.Random;

public class MainActivity extends AppCompatActivity {
public ImageView reward1;
public ImageView reward2;
public ImageView reward3;
public ImageView reward4;

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



}
public void generate (View view) {
    Random rand= new Random();
    int generatedNumber = rand.nextInt(999)+1;

    if(generatedNumber >=1 && generatedNumber <= 500){
        reward1.setVisibility(View.VISIBLE);
    }

    else if (generatedNumber >=501 && generatedNumber <= 600){
        reward2.setVisibility(View.VISIBLE);
    }

    else if(generatedNumber >=601 && generatedNumber <= 650){
        reward3.setVisibility(View.VISIBLE);
    }

    else if (generatedNumber >=651 && generatedNumber <= 1000){
        reward4.setVisibility(View.VISIBLE);
    }
}
}

This is activity_main.xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://ift.tt/nIICcg"
xmlns:app="http://ift.tt/GEGVYd"
xmlns:tools="http://ift.tt/LrGmb4"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="serchgoodswing.gamblegame.MainActivity">


<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="165dp"
    android:id="@+id/button"
    android:onClick="generate" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/a"
    android:id="@+id/reward1"
    android:layout_marginBottom="75dp"
    android:layout_alignBottom="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:visibility="invisible"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/b"
    android:id="@+id/reward2"
    android:layout_alignBottom="@+id/button"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="11dp"
    android:layout_marginStart="11dp"
    android:layout_marginBottom="58dp"
    android:visibility="invisible"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/c"
    android:id="@+id/reward3"
    android:layout_alignBottom="@+id/reward1"
    android:visibility="invisible"/>

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:srcCompat="@drawable/d"
    android:id="@+id/reward4"
    android:layout_alignBottom="@+id/reward2"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:visibility="invisible"/>
   </RelativeLayout>

These are the error that I get FATAL EXCEPTION: main Process: serchgoodswing.caseopening, PID: 2427 java.lang.IllegalStateException: Could not execute method for android:onClick at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293) at android.view.View.performClick(View.java:5637) at android.view.View$PerformClick.run(View.java:22429) at android.os.Handler.handleCallback(Handler.java:751) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method) at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288) at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setVisibility(int)' on a null object reference at serchgoodswing.caseopening.MainActivity.generate(MainActivity.java:36) at java.lang.reflect.Method.invoke(Native Method)  at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)  at android.view.View.performClick(View.java:5637)  at android.view.View$PerformClick.run(View.java:22429)  at android.os.Handler.handleCallback(Handler.java:751)  at android.os.Handler.dispatchMessage(Handler.java:95)  at android.os.Looper.loop(Looper.java:154)  at android.app.ActivityThread.main(ActivityThread.java:6119)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)  Application terminated.




Aucun commentaire:

Enregistrer un commentaire