如何从activityA&#39的按钮 - Android中获取popup imageview图像的setimageresource

时间:2016-03-17 21:47:09

标签: java android android-studio

目前我的活动有很多按钮,每个按钮弹出一个弹出窗口,用于this site 每个按钮都有自己的图像可供查看,我想更改图像而不是创建单独的弹出窗口。

ActivityA

Button backbtn;
public ImageView image;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_hiraganaactivity);

    final Button btnOpenPopup = (Button)findViewById(R.id.abutton);
    btnOpenPopup.setOnClickListener(new Button.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            LayoutInflater layoutInflater
                    = (LayoutInflater) getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);
            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);

            Button btnDismiss = (Button) popupView.findViewById(R.id.close);
            btnDismiss.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(btnOpenPopup, 30, -250);

        }
    });

    final Button ibutton = (Button)findViewById(R.id.ibutton);
    ibutton.setOnClickListener(new Button.OnClickListener(){

        @Override
        public void onClick(View arg0) {
            image = (ImageView) findViewById(R.id.imageview);
            image.setImageResource(R.drawable.hiraflash_i); 
            LayoutInflater layoutInflater
                    = (LayoutInflater)getBaseContext()
                    .getSystemService(LAYOUT_INFLATER_SERVICE);

            View popupView = layoutInflater.inflate(R.layout.popup, null);
            final PopupWindow popupWindow = new PopupWindow(
                    popupView,
                    LayoutParams.WRAP_CONTENT,
                    LayoutParams.WRAP_CONTENT);


            Button btnDismiss = (Button)popupView.findViewById(R.id.close);
            btnDismiss.setOnClickListener(new Button.OnClickListener() {

                @Override
                public void onClick(View v) {
                    popupWindow.dismiss();
                }
            });

            popupWindow.showAsDropDown(btnOpenPopup, 30, -250);


        }});

    backbtn = (Button) findViewById(R.id.backhira);
    backbtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            startActivity(new Intent(HiraganaActivity.this, Study_Menu.class));
            finish();
        }
    });


}

单击Attaton时,弹出窗口会正常显示默认设置图像。 但是,单击ibutton时,应用程序崩溃。

编辑: HiraganaActivity.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:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context=".HiraganaActivity"
tools:showIn="@layout/activity_hiraganaactivity"
android:background="@drawable/hiraganabg"
>


<Button
    style="?android:attr/buttonStyleSmall"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop = "@drawable/adraw"
    android:id="@+id/abutton"
    android:layout_marginTop="100dp"
    android:layout_marginLeft="9dp"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textSize="20sp"
    android:text="a"
    android:typeface="normal" />

<Button
    style="?android:attr/buttonStyleSmall"
    android:textAllCaps="false"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:drawableTop = "@drawable/idraw"
    android:text="i"
    android:id="@+id/ibutton"
    android:layout_alignTop="@+id/abutton"
    android:layout_toRightOf="@+id/abutton"
    android:layout_toEndOf="@+id/abutton"
    android:textSize="20sp"
    android:typeface="normal"
    android:layout_marginLeft="1dp" />

   <Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Back"
    android:id="@+id/backhira"
    android:textStyle="bold|italic"
    android:textSize="15sp"
    android:layout_marginBottom="26dp"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="false"
    android:layout_alignParentRight="false"
    android:layout_centerHorizontal="true"
    android:backgroundTint="#b9f600"
    android:textColor="#0414a3" />

和Popup.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#00546e">

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:layout_margin="2dp"
    android:background="#929292">
    >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:layout_marginTop="30dp"
        android:layout_marginLeft="40dp"
        android:layout_marginRight="40dp"
        android:layout_marginBottom="20dp">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/aflashhira"
            android:id="@+id/imageview" />
        <Button
            android:id="@+id/close"
            android:layout_marginTop="10dp"
            android:layout_marginLeft="70dp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Close" />

    </LinearLayout>
</LinearLayout>

来自日志:

 FATAL EXCEPTION: main
                                                                         java.lang.NullPointerException
                                                                             at com.example.afropoker.kanaapp.HiraganaActivity$2.onClick(HiraganaActivity.java:56)

1 个答案:

答案 0 :(得分:0)

充气后弹出视图

 View popupView = layoutInflater.inflate(R.layout.popup, null);

使用 -

获取imageview
ImageView img = (ImageView)popupView.findViewById(R.id.img);// whatever is the id of your ImageView in your popup layout 
img.setBackgroundResource(R.id.background_res); //any resource id which you want to set as a background

使用上述机制,您可以随时更改图像背景