【问题标题】:How to make one ImageView invisible when another clickable ImageView is pressed?当按下另一个可点击的 ImageView 时,如何使一个 ImageView 不可见?
【发布时间】:2026-02-13 10:05:01
【问题描述】:

我试图让 ImageView(bul1) 在 ImageView(Seethrough) 被按下时消失。尝试运行此代码时出现空指针错误。它有什么问题?

JAVA代码

public class MainActivity extends ActionBarActivity {

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

    final ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
    final ImageView view1 = (ImageView) findViewById(R.id.bul1);
    seethrough1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            if(view1.getVisibility() == View.VISIBLE)
            {
                view1.setVisibility(View.INVISIBLE);
            }

        }
    });

    }

XML 代码

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">
<ImageView
    android:layout_width="match_parent"
    android:layout_height="300dp"
    android:paddingBottom="6dp"
    android:src="@drawable/gun"
    android:clickable="true"
    android:id="@+id/Seethrough"
    android:onClick="next"
    />

    <ImageView
        android:layout_width="30dp"
        android:layout_height="wrap_content"
        android:src="@drawable/bullet"
        android:id="@+id/bul1"
        />
</LinearLayout>

【问题讨论】:

  • 能否也分享一下崩溃日志?
  • 引起:java.lang.NullPointerException
  • 我要的是你将从 adb logcat 获得的整个崩溃日志的堆栈
  • 检查两个图像是否存在于同一个视图中
  • 有人有什么想法吗?

标签: java android xml onclick imageview


【解决方案1】:

您需要协调 seethrough 的 onClickListener 与其 onClick XML 属性。我建议从 xml 中删除这一行:

android:onClick="next"

并将代码放在下一个方法中(如果有的话)

public void next (View v){
    some code
}

在您的能见度之后或之前检查是否更适合您:

@Override
public void onClick(View v) { 

    //place some code here

    if(view1.getVisibility() == View.VISIBLE){
    view1.setVisibility(View.INVISIBLE);
    }

    //or here

}

【讨论】:

    【解决方案2】:

    我认为他们的xml代码有问题,请尝试如下编写xml,

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
        <ImageView
            android:layout_width="match_parent"
            android:layout_height="300dp"
            android:paddingBottom="6dp"
            android:src="@drawable/gun"
            android:clickable="true"
            android:id="@+id/Seethrough"
            android:onClick="next"
        />
    
        <ImageView
            android:layout_width="30dp"
            android:layout_height="wrap_content"
            android:src="@drawable/bullet"
            android:id="@+id/bul1"
        />
    </LinearLayout>
    

    【讨论】:

      【解决方案3】:

      如果它返回 NullPointerExeption,我认为你的 ImageView 是 Null,因为

      setContentView(R.layout.activity_main);
      

      并且activity_main.xml 与您的帖子内容不一样,请检查布局名称并重试。

      【讨论】:

        【解决方案4】:

        我发现我应该在方法内部而不是之前声明图像视图。

        喜欢这个

        public void onClick(View v) {
         ImageView seethrough1 = (ImageView) findViewById(R.id.Seethrough);
        

        【讨论】:

          最近更新 更多