Reputation: 1
I keep getting an exception when I want to display an alertdialog when edit button is clicked. When i comment out alertDialog.show(), the app works, otherwise, it crashes. Please let me know where the error is. I really would appreciate an explanation.
Exception is:
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)
My xml code is:
<?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>
My Activity code where edtEdit onClick not working is:
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();
}
}
Upvotes: 0
Views: 1403
Reputation: 87
I see that on click "editIncomeValues()" is called where you are showing of dialog.
I guess you are facing issue because you have set actions for Alert Dialog - alertDialogBuilder and performed show on a new dialog - alertDialog .
Your code is perfect. Just change dont create another alertDialog(alertDialog) and use the same one created(alertDialogBuilder) for showing as well...
Goodluck..! Hope it helps :)
Upvotes: 0
Reputation: 424
You don't need to create AlertDialog
just delete this line:
AlertDialog alertDialog = alertDialogBuilder.create();
and change the last line to :
alertDialogBuilder.show();
Upvotes: 1