如果声明没有使图像可见

时间:2016-12-21 23:39:41

标签: java android if-statement random imageview

我想要做的是按一个按钮再次看到不可见的图像。

我尝试使用以下代码执行此操作,但它不起作用。

如果按下按钮,应用程序崩溃。我是java的新手我通过查看youtube来学习代码。

这是我的java文件

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

这是activity_main.xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
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>

这是我得到的错误

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.

1 个答案:

答案 0 :(得分:1)

您必须使用ImageView方法将findViewById绑定到引用,如下所示:

reward1 = (ImageView) findViewById(R.id.reward1);

您应该使用onCreate方法执行此操作。