java - 通过单击 PopUpWindow 上的按钮打开 Activity (Android Studio)

标签 java android

我正在尝试执行标题所示的操作,但由于某种原因,当我单击按钮时应用程序崩溃了。

它显示的错误是:

java.lang.IllegalStateException: Could not find method show(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'btn_1'

我有一个 MainActivity,其中包含一个从按钮打开弹出窗口的方法(这似乎工作正常,但我编写它只是为了以防万一)并且是:

公共(public)类 MainActivity 扩展 AppCompatActivity{

private PopupWindow popUpWindow;
private LayoutInflater layoutInflater;
private RelativeLayout relativeLayout;

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

    relativeLayout = (RelativeLayout) findViewById(R.id.relative);

}

public void newWindowPopup(View view){
    DisplayMetrics dm = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(dm);

    int wide = dm.widthPixels;
    int height = dm.heightPixels;

    layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.activity_popwindow, null);
    popUpWindow = new PopupWindow(container, (int)(wide*.7), (int)(height*.25), true);
    popUpWindow.setAnimationStyle(R.style.PopupAnimation);
    popUpWindow.showAtLocation(relativeLayout, Gravity.CENTER, 0, 0);

    container.setOnTouchListener(new View.OnTouchListener(){
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            popUpWindow.dismiss();
            return true;
        }
    });

}

此 Activity 的布局:

<?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/relative"
    android:background="#A85757"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ScrollView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="30dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <GridLayout
            android:layout_width="300dp"
            android:layout_height="wrap_content"
            android:columnCount="1">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="horizontal">

                <Button
                    android:id="@+id/btnFrases"
                    android:layout_width="10dp"
                    android:layout_height="wrap_content"
                    android:layout_weight="1"
                    android:onClick="newWindowPopup"
                    android:text="Button 1"
                    android:textColor="#000000" />

            </LinearLayout>

        </GridLayout>

    </ScrollView>

</RelativeLayout>

从我创建的 popwindow 类中的另一个按钮打开 Activity 的代码,这似乎是问题所在:

公共(public)类 popwindow 扩展 AppCompatActivity {

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

    Button p_button=findViewById(R.id.btn_1);

    p_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent showActivity = new Intent(getApplicationContext(), Main2Activity.class);
            startActivity(showActivity);
        }
    });
}


public void show(View view) {
    Intent showActivity = new Intent(getApplicationContext(), Main2Activity.class);
    startActivity(showActivity);
}

}

此 Activity 的布局:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/colorPrimary">


    <LinearLayout
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:orientation="horizontal">

        <Button
            android:id="@+id/btn_1"
            android:layout_width="10dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="EEEEE"
            android:textColor="#000000" />


    </LinearLayout>

</RelativeLayout>

最后,我尝试从此弹出窗口(Main2Activity)打开的 Activity 还没有代码,它只是一个带有 TextView 的空 Activity 。

View 方法与具有 onClick 属性的按钮相关联,因此这不是问题。如果您能提供帮助,非常感谢。

最佳答案

首先在popwindow布局上添加一个按钮。

 <Button  
        android:id="@+id/button"  
        android:layout_width="wrap_content"  
        android:layout_height="wrap_content"  
        android:text="Start Second Activity"/>  

然后添加以下功能以显示弹出窗口并移至第二个 Activity

    public void newWindowPopup(View view){
        DisplayMetrics dm = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(dm);

        int wide = dm.widthPixels;
        int height = dm.heightPixels;

        layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
        ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.popwindow, null);
        popUpWindow = new PopupWindow(container, (int)(wide*.7), (int)(height*.25), true);
        popUpWindow.setAnimationStyle(R.style.PopupAnimation);
        popUpWindow.showAtLocation(relativeLayout, Gravity.CENTER, 0, 0);

        container.setOnTouchListener(new View.OnTouchListener(){
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                popUpWindow.dismiss();
                return true;
            }
        });

        Button p_button=container.findViewById(R.id.button);  

        p_button.setOnClickListener(new View.OnClickListener() {  
            @Override  
            public void onClick(View view) { 

            Intent showActivity = new Intent(getApplicationContext(), Main2Activity.class);
            startActivity(showActivity); 
            }  
        });  
    }

关于java - 通过单击 PopUpWindow 上的按钮打开 Activity (Android Studio),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61525748/

相关文章:

java - Selenium ImeActivationFailedException 和 ImeNotAvailableException

搜索AD的Java程序

android - ListView 中每个项目的可扩展 ListView

java - 如何在键盘出现时隐藏或保持底部导航向下,同时让文本编辑器工具栏上下移动?

java - 在 Spring 集成测试期间刷新/重建特定 beans

java - 需要解释 OpenCV Android 行为

java - 广播接收器或接口(interface)在两个 Activity 之间进行通信?

java - 如何在写入文件的同时将文件分成 block ?

android - Android API 20 是否允许支持蓝牙 LE 的设备充当外围设备?

Java Matcher 慢速正则表达式