gpt4 book ai didi

java - 如果语句没有使图像可见

转载 作者:行者123 更新时间:2023-12-01 09:06:08 25 4
gpt4 key购买 nike

我想做的是通过按下按钮使不可见的图像再次可见。

我尝试使用以下代码来做到这一点,但它不起作用。

如果我按下按钮,应用程序就会崩溃。我对 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.

最佳答案

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

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

您应该在 onCreate 方法中执行此操作。

关于java - 如果语句没有使图像可见,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41273703/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com