PopUp window 显示 IllegalStateException
PopUp window shows IllegalStateException
我是 android 的新手。我想在弹出 window 中按下按钮时打印一条消息(使用 Log.i),但它退出并显示
java.lang.IllegalStateException: Could not find method deletemtd(View)
in a parent or ancestor Context for android:onClick attribute defined
on view class com.google.android.material.button.MaterialButton with
id 'button'
我的onclick方法编码
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_output_calculation, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
弹出布局的activity_output_calculation.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="@color/myRED"
android:orientation="vertical"
tools:context=".Output_calculation">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="deletemtd"
android:text="Button" />
</LinearLayout>
Java弹窗代码
public class Output_calculation extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_output_calculation);
}
public void deletemtd(View view) {
Log.i("tag nema", "deletemtd:");
}
}
我想在按下按钮时在 运行 控制台中打印此日志消息。
您用来设置视图 activity 和弹出窗口的布局相同,但是,它们是不同的视图实例,因为视图设置为 activity 你在 activity 代码中有 deletemtd
。 但是当你将 xml 膨胀到一个视图以在那个地方的弹出窗口中显示它时,没有 deletemtd
方法。,你必须做的是设置一个该视图的明确侦听器。
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_output_calculation, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupView.findViewById<Button>(R.id.button).setOnClickListener(null);
//or a listener
popupView.findViewById<Button>(R.id.button).setOnClickListener(v->{/*your task here*/});
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
我是 android 的新手。我想在弹出 window 中按下按钮时打印一条消息(使用 Log.i),但它退出并显示
java.lang.IllegalStateException: Could not find method deletemtd(View) in a parent or ancestor Context for android:onClick attribute defined on view class com.google.android.material.button.MaterialButton with id 'button'
我的onclick方法编码
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_output_calculation, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
弹出布局的activity_output_calculation.xml
代码
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:background="@color/myRED"
android:orientation="vertical"
tools:context=".Output_calculation">
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="deletemtd"
android:text="Button" />
</LinearLayout>
Java弹窗代码
public class Output_calculation extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_output_calculation);
}
public void deletemtd(View view) {
Log.i("tag nema", "deletemtd:");
}
}
我想在按下按钮时在 运行 控制台中打印此日志消息。
您用来设置视图 activity 和弹出窗口的布局相同,但是,它们是不同的视图实例,因为视图设置为 activity 你在 activity 代码中有 deletemtd
。 但是当你将 xml 膨胀到一个视图以在那个地方的弹出窗口中显示它时,没有 deletemtd
方法。,你必须做的是设置一个该视图的明确侦听器。
public void onButtonShowPopupWindowClick(View view) {
// inflate the layout of the popup window
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(R.layout.activity_output_calculation, null);
// create the popup window
int width = LinearLayout.LayoutParams.MATCH_PARENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
boolean focusable = true; // lets taps outside the popup also dismiss it
final PopupWindow popupWindow = new PopupWindow(popupView, width, height, focusable);
popupView.findViewById<Button>(R.id.button).setOnClickListener(null);
//or a listener
popupView.findViewById<Button>(R.id.button).setOnClickListener(v->{/*your task here*/});
// show the popup window
// which view you pass in doesn't matter, it is only used for the window tolken
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}