gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 16:59:55 27 4
gpt4 key购买 nike

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

它显示的错误是:

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/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com