Java Code:-
package com.example.plannerv1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
import java.util.ArrayList;
public class DisplayNotes extends AppCompatActivity {
handledatabaseonhomescreen handle_db_notes = new handledatabaseonhomescreen();
private int key;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_notes);
Intent incomingintent = getIntent();
key = incomingintent.getIntExtra("key",-1);
displaydate(key);genanddisplay();
}
protected void addanote(View v)
{
Intent goback = new Intent();
goback.putExtra("returnkey",-100);
goback.putExtra("datewas",key);
setResult(RESULT_OK,goback);
finish();
}
protected void onActivityResult(int requestCode,int resultCode,Intent intent)
{
genanddisplay();
}
}
XML code:-
<?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:gravity="top|center"
android:layout_height="match_parent"
tools:context=".DisplayNotes"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20sp"
android:paddingBottom="20sp"
android:textStyle="bold"
android:gravity="center"
android:textSize="20dp"
android:id="@+id/dmy"/>
<ListView
android:layout_width="match_parent"
android:layout_height="0px"
android:paddingTop="20sp"
android:paddingBottom="20sp"
android:paddingLeft="5sp"
android:paddingRight="6sp"
android:layout_weight="10"
android:id="@+id/displaynote">
</ListView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20sp"
android:text="Add Note"
android:onClick="addanote"/>
</LinearLayout>
Error:-
java.lang.IllegalStateException: Could not find method addanote(View) in a parent or ancestor Context for android:onClick attribute defined on view class androidx.appcompat.widget.AppCompatButton
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.resolveMethod(AppCompatViewInflater.java:424)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:381)
at android.view.View.performClick(View.java:5619)
at android.view.View$PerformClick.run(View.java:22298)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:165)
at android.app.ActivityThread.main(ActivityThread.java:6375)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:802)
I can't figure out why the onClick tag doesn't attach the button to the specified function.
I tried to Google the problem but nothing could help me. I tried changing the name and checking if the files are linked but nothing helps.
And I'm sorry about the ugly formatting on the error but stackoverflow wouldn't let me post.
AndroidStudio gives this warning:-
Method 'addanote' in 'DisplayNotes' has incorrect signature less... (Ctrl+F1)
Inspection info: Checks if the method specified in onClick XML attribute is declared in related activity
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…