当我想在单击编辑按钮时显示alertdialog时,我一直收到异常。当我注释掉alertDialog.show()时,该应用程序可以正常工作,否则会崩溃。请告诉我错误的位置。我真的很感激解释。
例外是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.kiran.expensecalculator, PID: 3325
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:293)
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)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
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)
Caused by: java.lang.NoSuchMethodError: No static method setOnApplyWindowInsetsListener(Landroid/view/View;Landroid/support/v4/view/OnApplyWindowInsetsListener;)V in class Landroid/support/v4/view/ViewCompatLollipop; or its super classes (declaration of 'android.support.v4.view.ViewCompatLollipop' appears in /data/data/com.example.kiran.expensecalculator/files/instant-run/dex/slice-internal_impl-24.2.1_1f99a106c741de69d4ed910ed0109f9cc344ee12-classes.dex)
at android.support.v4.view.ViewCompat$LollipopViewCompatImpl.setOnApplyWindowInsetsListener(ViewCompat.java:1558)
at android.support.v4.view.ViewCompat.setOnApplyWindowInsetsListener(ViewCompat.java:2829)
at android.support.v7.app.AppCompatDelegateImplV7.createSubDecor(AppCompatDelegateImplV7.java:425)
at android.support.v7.app.AppCompatDelegateImplV7.ensureSubDecor(AppCompatDelegateImplV7.java:312)
at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:277)
at android.support.v7.app.AppCompatDialog.setContentView(AppCompatDialog.java:80)
at android.support.v7.app.AlertController.installContent(AlertController.java:214)
at android.support.v7.app.AlertDialog.onCreate(AlertDialog.java:256)
at android.app.Dialog.dispatchOnCreate(Dialog.java:394)
at android.app.Dialog.show(Dialog.java:295)
at com.example.kiran.expensecalculator.Income.editIncomeValues(Income.java:256)
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:288)
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)
我的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:id="@+id/activity_income"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.kiran.expensecalculator.Income">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/save"
android:id="@+id/btnSave"
android:layout_weight="1"
android:onClick="insertIncomeValues"/>
<ImageButton
android:id="@+id/btnEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/edit"
android:onClick="editIncomeValues"></ImageButton>
<ImageButton
android:id="@+id/btnDel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="@drawable/del"></ImageButton>
</LinearLayout>
</LinearLayout>
我的活动代码,其中edtEdit onClick不起作用:
public class Income extends AppCompatActivity {
TextView edtTextMonth , edtTextTitle, edtTextDate, edtTextIncome;
TableLayout tableLayoutDisplay;
ImageButton btnNext, btnPrev, btnEdit;
DBHelper myDB;
int monthNumber = 12, year = 1964;
String monthName, prevNext;
String T1, T2, T3; // selected row values
int activity = 0, tr; // '0' as an IncomeActivity in DBHelper insertData method.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_income);
myDB = new DBHelper(this);
edtTextMonth = (TextView) findViewById(txtViewMonth);
edtTextDate = (EditText) findViewById(R.id.editTextDate);
edtTextTitle = (EditText) findViewById(R.id.editTextTitle);
edtTextIncome = (EditText) findViewById(R.id.editTextIncome);
btnNext = (ImageButton) findViewById(R.id.btnNext);
btnPrev = (ImageButton) findViewById(R.id.btnPrev);
btnEdit = (ImageButton) findViewById(R.id.btnEdit);
}
public void editIncomeValues(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(Income.this);
alertDialogBuilder.setTitle("Edit");
alertDialogBuilder.setMessage("Are you sure, You wanted to make decision");
alertDialogBuilder.setPositiveButton("yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
edtTextTitle.setText(T1);
edtTextDate.setText(T2);
edtTextDate.setText(T3);
edtTextTitle.requestFocus();
}
})
.setNegativeButton("No",new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
}
答案 0 :(得分:1)
您无需创建AlertDialog
只需删除此行:
AlertDialog alertDialog = alertDialogBuilder.create();
并将最后一行更改为:
alertDialogBuilder.show();
答案 1 :(得分:0)
我看到点击“editIncomeValues()”会在你显示对话框的地方调用。
我猜您面临的问题是因为您已为警报对话框设置了操作 - alertDialogBuilder 并在新对话框上执行了显示 - alertDialog 。
您的代码非常完美。只是改变不要创建另一个alertDialog(alertDialog)并使用相同的创建(alertDialogBuilder)来显示...
古德勒克..!希望它有所帮助:)