无法识别按钮上的MainActivity中的方法单击

时间:2018-01-19 16:26:28

标签: android listview button onclick

我对 listview 中的每个项目都有自定义布局。但布局中的按钮无法识别 MainActivity 中的活动。

ListView显示在主视图中。 每个项目的布局文件如下:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">


    <TextView
        android:id="@+id/txt_bookname_listview"
        android:layout_width="234dp"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_alignBottom="@+id/btn_renewed_listview"
        android:layout_alignParentTop="true"
        android:textAlignment="center"
        android:gravity="center"
        android:textStyle="bold"
        android:textSize="18dp"
        android:layout_toStartOf="@+id/btn_delete_book" />

    <Button
        android:id="@+id/btn_renewed_listview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Renewed"
        android:layout_alignParentTop="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"
        android:textAllCaps="false"
        android:background="@android:color/holo_green_light"
        android:onClick="clickedRenew"
        />

    <Button
        android:id="@+id/btn_delete_book"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_toStartOf="@+id/btn_renewed_listview"
        android:background="@android:color/holo_red_dark"
        android:onClick="clickedDelete"
        android:text="Delete"
        android:textAllCaps="false" />
</RelativeLayout>

这些函数位于MainActivity.java文件中。 与按钮相关的功能如下:

public void clickedRenew(View view)
{
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    TextView textView = new TextView(getApplicationContext());
    textView.setText("Have You renewed the Book?");
    textView.setPadding(10,10,10,10);
    textView.setGravity(Gravity.CENTER);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

    builder.setCustomTitle(textView)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).create().show();

}


public void clickedDelete(View view)
{
    Log.d("LetMeDelete", "here");
    final View view1 = view;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    TextView textView = new TextView(getApplicationContext());
    textView.setText("Have You retured the book?");
    textView.setPadding(10,10,10,10);
    textView.setGravity(Gravity.CENTER);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    textView.setTextColor(Color.RED);

    builder.setCustomTitle(textView)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    View parent = (View) view1.getParent();
                    TextView bookname = (TextView) parent.findViewById(R.id.txt_bookname_listview);
                    String bookName = bookname.getText().toString();
                    database.deleteData(bookName);
                    updateListView(database.getAllData());
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).create().show();
}

public void updateListView(Cursor data)
    {
        ArrayList<String> arrayList = new ArrayList<>();

        while (data.moveToNext())
        {
            arrayList.add(data.getString(1));
        }
        if(arrayAdapter == null)
        {
            arrayAdapter = new ArrayAdapter<String>(getApplicationContext(),R.layout.item_book_name,R.id.txt_bookname_listview, arrayList);
            listView.setAdapter(arrayAdapter);
        }
        else
        {
            arrayAdapter.clear();
            arrayAdapter.addAll();
            arrayAdapter.notifyDataSetChanged();
            arrayAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.item_book_name, R.id.txt_bookname_listview, arrayList);
            listView.setAdapter(arrayAdapter);
        }

        data.close();
        database.close();
    }

android studio中的 logcat 如下:

01-20 04:08:06.081 32344-32344/com.example.imad.bookrenewalert E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                 Process: com.example.imad.bookrenewalert, PID: 32344
                                                                                 java.lang.IllegalStateException: Could not find method clickedDelete(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.Button with id 'btn_delete_book'
                                                                                     at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:5368)
                                                                                     at android.view.View$DeclaredOnClickListener.onClick(View.java:5327)
                                                                                     at android.view.View.performClick(View.java:6256)
                                                                                     at android.view.View$PerformClick.run(View.java:24697)
                                                                                     at android.os.Handler.handleCallback(Handler.java:789)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                     at android.os.Looper.loop(Looper.java:164)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)

我现在已经多次检查拼写,但我找不到任何错误。 clickedDelete 方法已完全实施,但 clickedRenew 则未完全实施。 logcat中显示的错误是点击 listview 项目中的删除按钮后。

1 个答案:

答案 0 :(得分:0)

您可以通过另一种方式解决此问题,删除android:onClick,然后在初始化按钮后在onCreate方法中使用此代码:

firstButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View view) {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);

TextView textView = new TextView(getApplicationContext());
textView.setText("Have You renewed the Book?");
textView.setPadding(10,10,10,10);
textView.setGravity(Gravity.CENTER);
textView.setTypeface(null, Typeface.BOLD);
textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);

builder.setCustomTitle(textView)
        .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        })
        .setNegativeButton("No", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {

            }
        }).create().show();
    });

并使用第二个按钮:

secondButton.setOnClickListener(new View.OnClickListener() {
    @Override public void onClick(View view) {
 Log.d("LetMeDelete", "here");
    final View view1 = view;

    AlertDialog.Builder builder = new AlertDialog.Builder(this);

    TextView textView = new TextView(getApplicationContext());
    textView.setText("Have You retured the book?");
    textView.setPadding(10,10,10,10);
    textView.setGravity(Gravity.CENTER);
    textView.setTypeface(null, Typeface.BOLD);
    textView.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
    textView.setTextColor(Color.RED);

    builder.setCustomTitle(textView)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    View parent = (View) view1.getParent();
                    TextView bookname = (TextView) parent.findViewById(R.id.txt_bookname_listview);
                    String bookName = bookname.getText().toString();
                    database.deleteData(bookName);
                    updateListView(database.getAllData());
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {

                }
            }).create().show();
    });