我正在使用Android中的警报对话框。警报对话显示正常, 但取消按钮无法正常工作:单击取消按钮时,OnClick-Method"取消对话框"找不到,虽然它在主活动中,我将Dialog实例传递给方法。我想不通,为什么;因此,非常感谢任何帮助。
Logcat显示以下错误:
11-01 21:10:30.111 11217-11217/de.die_web_agenten.www.batprice E/AndroidRuntime: FATAL EXCEPTION: main
Process: de.die_web_agenten.www.batprice, PID: 11217
java.lang.IllegalStateException: Could not find method cancelDialog(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.support.v7.widget.AppCompatButton with id 'dialog_cancel'
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:327)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:284)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
11-01 21:10:34.599 11217-11217/de.die_web_agenten.www.batprice I/Process: Sending signal. PID: 11217 SIG: 9
这是主要活动:
import android.app.Dialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.facebook.FacebookSdk;
import com.facebook.appevents.AppEventsLogger;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
openDialog();
FacebookSdk.sdkInitialize(getApplicationContext());
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Contact us", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
Uri uri = Uri.parse("http://www.die-web-agenten.de/en/kontaktaufnahme/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
@Override
protected void onResume() {
super.onResume();
// Logs 'install' and 'app activate' App Events.
AppEventsLogger.activateApp(this);
}
@Override
protected void onPause() {
super.onPause();
// Logs 'app deactivate' App Event.
AppEventsLogger.deactivateApp(this);
}
@Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_results) {
// Handle the camera action
Intent SecondListActivityIntent = new Intent(MainActivity.this, de.die_web_agenten.www.batprice.SecondListActivity.class);
MainActivity.this.startActivity(SecondListActivityIntent);
} else if (id == R.id.nav_map) {
// Handle the camera action
Intent MapsActivityIntent = new Intent(MainActivity.this, MapsActivity.class);
//ListActivity.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(MapsActivityIntent);
//Intent ListActivityIntent = new Intent(MainActivity.this, ListActivity.class);
//ListActivity.putExtra("key", value); //Optional parameters
//MainActivity.this.startActivity(ListActivityIntent);
} else if (id == R.id.nav_results) {
// Handle the camera action
Intent ResultsActivityIntent = new Intent(MainActivity.this, de.die_web_agenten.www.batprice.ResultsActivity.class);
//ListActivity.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(ResultsActivityIntent);
} /*else if (id == R.id.nav_training) {
// Handle the camera action
Intent TrainingActivityIntent = new Intent(MainActivity.this, de.die_web_agenten.www.batprice.TrainingActivity.class);
//ListActivity.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(TrainingActivityIntent);
} */
else if (id == R.id.nav_share) {
// Handle the camera action
Intent AndroidBarcodeQRExampleIntent = new Intent(MainActivity.this, de.die_web_agenten.www.batprice.AndroidBarcodeQrExample.class);
//ListActivity.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(AndroidBarcodeQRExampleIntent);
} else if (id == R.id.nav_send) {
// Handle the camera action
Uri uri = Uri.parse("http://www.die-web-agenten.de/en/kontaktaufnahme/");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
public void OpenListActivity() {
Intent SecondListActivityIntent = new Intent(MainActivity.this, de.die_web_agenten.www.batprice.SecondListActivity.class);
//ListActivity.putExtra("key", value); //Optional parameters
MainActivity.this.startActivity(SecondListActivityIntent);
}
public void openDialog() {
final Dialog dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
public void cancelDialog(Dialog dialog) {
dialog.dismiss();
}
}
这是相应的对话框演示布局文件:
<?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="wrap_content">
<TextView
android:id="@+id/dialog_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="@string/dialog_text"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="@id/dialog_info">
<Button
android:id="@+id/dialog_cancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:text="Cancel"
android:onClick="cancelDialog"/>
<Button
android:id="@+id/dialog_ok"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.50"
android:text="Check voucher"/>
<Button
android:id="@+id/buttonAlert"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Alert Box" />
</LinearLayout>
</RelativeLayout>
答案 0 :(得分:3)
你是以错误的方式实施它。你可以这样做
private Dialog dialog;
public void openDialog() {
dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
public void cancelDialog(View view) {
dialog.dismiss();
}
cancelDialog 方法需要查看类型的参数,并且您尝试将其作为对话框传递。这就是你的应用程序崩溃的原因。希望它会帮助你。
答案 1 :(得分:1)
这可能是这样的
Dialog dialog;
public void openDialog() {
dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
public void cancelDialog() {
dialog.dismiss();
}
答案 2 :(得分:0)
您的cancelDialog方法需要类型为Dialog
的参数,并且您的错误消息告诉您无法找到参数类型为View
的方法。
我的安卓时代已经结束了一段时间,但我认为你需要接受View作为参数(这是按钮所属的视图)并关闭它,而不仅仅是解除对话框。
答案 3 :(得分:0)
这很明显,因为如果你在XML上用View
定义onClick来运行方法,那么方法本身将需要View
,这是你在XML上定义的View
,当你将Dialog
传递给该方法时。在您的情况下,您传递Button
而不是Dialog
。
只需删除
public void cancelDialog(Dialog dialog) {
dialog.dismiss();
}
和
android:onClick="cancelDialog"
你的dialog_cancel按钮上的。 然后编辑openDialog方法如下:
public void openDialog() {
final Dialog dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
View vCancel = dialog.findViewById(R.id.dialog_cancel);
vCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
答案 4 :(得分:0)
试试此代码
Dialog dialog;
public void openDialog() {
dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
public void cancelDialog() {
dialog.dismiss();
}
您正在将按钮视图传递给cancelDialog(View)
而不是cancelDialog(Dialog v)
答案 5 :(得分:0)
你应该扩展对话框类并将你的&#34; onclick&#34;那里的方法。
这可以帮助您:Using onClick attribute in layout xml causes a NoSuchMethodException in Android dialogs
答案 6 :(得分:0)
This should be able to help:
private void openDialog() {
LayoutInflater layoutInflaterAndroid = LayoutInflater.from(BaseActivity.this);
View view = layoutInflaterAndroid.inflate(R.layout.edit_remark_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(BaseActivity.this);
alertDialogBuilder.setView(view);
final AlertDialog alertDialog = alertDialogBuilder.create();
Button btnEdit = (Button) view.findViewById(R.id.btn_edit);
Button btnDelete = (Button) view.findViewById(R.id.btn_delete);
final EditText etRemark = (EditText) view.findViewById(R.id.et_remark);
TextView tvTitle = (TextView) view.findViewById(R.id.tv_title);
TextView tvMessage = (TextView) view.findViewById(R.id.tv_msg);
tvTitle.setText(getResources().getString(R.string.exit_app));
tvMessage.setText(getResources().getString(R.string.are_you_sure_want_exit));
btnEdit.setText(getResources().getString(R.string.no));
btnDelete.setText(getResources().getString(R.string.yes));
btnEdit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
alertDialog.dismiss();
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
alertDialog.dismiss();
}
});
alertDialog.show();
}
答案 7 :(得分:-1)
只需在类声明中声明对话框,无需在cancleDialog方法中传递dialog参数。当你显示对话框时,只需调用方法打开对话框,当你关闭对话框时调用cancleDialog方法
Dialog dialog;
public void openDialog() {
dialog = new Dialog(this); // Context, this, etc.
dialog.setContentView(R.layout.dialog_demo);
dialog.setTitle(R.string.dialog_title);
dialog.show();
}
public void cancelDialog() {
dialog.dismiss();
}