单击显示按钮时弹出窗口崩溃,并且上一个窗口中的所有按钮都出现在弹出窗口中

时间:2019-06-04 10:30:49

标签: java android android-layout

当我点击指示灯时,应用程序崩溃了。同样,主窗口中的所有按钮也会显示在弹出窗口中,请参见下面的代码。我正在发布Java代码和相关的xml代码。

Java代码有点长,但是它包含与我的问题有关的所有主要内容。

            ImageButton closeButton;
            TextView txt;
            LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
            LinearLayout.LayoutParams params1 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
            ImageView p = (ImageView) findViewById(R.id.imageview);
            RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rl_custom_layout);
            float i;
            float j;
            i=p.getX();
            j=p.getY();
            Context context=getApplicationContext();
            final PopupWindow mPopupWindow;
            TextView text = (TextView) findViewById(R.id.text1);
            text.setText("i: "+i+" j: "+j);
            if ((i>=85) && (i<=423) && (j>=229) && (j<=1805)){
                if(item1.equals("Light")){
                    LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
                    View customView = inflater.inflate(R.layout.activity_main, parent, false);
                    mPopupWindow = new PopupWindow(
                            customView,
                            ViewGroup.LayoutParams.MATCH_PARENT,
                            ViewGroup.LayoutParams.MATCH_PARENT
                    );
                    if(Build.VERSION.SDK_INT>=21){
                        mPopupWindow.setElevation(5.0f);
                    }

                    closeButton = (ImageButton) customView.findViewById(R.id.buttons);
                    closeButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View view) {
                            // Dismiss the popup window
                            mPopupWindow.dismiss();
                        }
                    });
                    txt = (TextView) customView.findViewById(R.id.txt);
                    txt.setText("You are in Room1");
                    relativeLayout.addView(closeButton, params1);
                    relativeLayout.addView(txt, params1);
                    mPopupWindow.showAtLocation(relativeLayout, Gravity.CENTER,0,0);

                    text.setText("You are in Room1");
                }
                else{
                    text.setText("FU:"+ item1);
                }

运行应用程序时所有按钮均显示的xml代码,是否需要为其创建单独的xml文件?

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
tools:context=".MainActivity"
android:background="@drawable/house_plan">

<TextView
    android:layout_width="wrap_content"
    android:id="@+id/text1"
    android:layout_height="wrap_content"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Get Wifi Info"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageview"
    android:src="@drawable/position_cursor"
    tools:layout_editor_absoluteX="400dp"
    tools:layout_editor_absolutelY="600dp"/>

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="F++"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Button
    android:id="@+id/button2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="F+*"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Button
    android:id="@+id/button3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="F*+"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Button
    android:id="@+id/button4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B--"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Button
    android:id="@+id/button5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B-*"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Button
    android:id="@+id/button6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="B*-"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:entries="@array/item_arrays"
    android:prompt="@string/item_list"
    tools:layout_editor_absoluteX="0dp"
    tools:layout_editor_absoluteY="0dp"/>
<RelativeLayout
    android:id="@+id/rl_custom_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="2dp">

    <ImageButton
        android:id="@+id/buttons"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/ic_close_white_24dp"
        android:layout_alignParentEnd="true"
        android:layout_alignParentRight="true"
        android:background="@null"/>

    <TextView
        android:layout_width="wrap_content"
        android:id="@+id/txt"
        android:layout_height="wrap_content"/>

</RelativeLayout>

0 个答案:

没有答案