Я работаю над диалогом оповещений в Android. Диалоговое окно оповещения отображается хорошо, , но кнопка отмены не работает должным образом: при нажатии кнопки «Отмена» метод «Отменить диалог» OnClick не найден, хотя он находится в главной операции, и я передаю диалог экземпляров метода. Я не могу понять, почему; поэтому любая помощь была бы очень оценена.Отмена кнопки в предупреждении Диалог не работает
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>
Спасибо! Это было очень полезно! –