简体   繁体   中英

How to make one ImageView invisible when another clickable ImageView is pressed?

I tried to make ImageView(bul1) disappear when ImageView(Seethrough) is pressed. I get a nullpointer error when i try to run this code. What is wrong with it?

JAVA code

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 code

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

You need to reconcile seethrough's onClickListener with its onClick XML attribute. I'd suggest removing this line from the xml:

android:onClick="next"

and placing the code inside your next method (if you have one)

public void next (View v){
    some code
}

behind or before your visibility checking if, whichever suits you more:

@Override
public void onClick(View v) { 

    //place some code here

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

    //or here

}

I think their is problem with out xml code, please try writing xml as follows,

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

If it return NullPointerExeption, I think your ImageView is Null, because

setContentView(R.layout.activity_main);

and activity_main.xml is not like contents of your post, check name of layout and try again.

I found out that I was supposed to declare the imageviews inside the method instead of before.

like this

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM