Android Studio onClick的显示方法根本不起作用

问题描述 投票:1回答:1

我试图为数组列表中的按钮制作一个onClick方法,但是由于某种原因,它一直说不起作用,因为没有给出“在父上下文中找不到方法”,我尝试了无数次,基本上起来(所以让我们暂时保留它)。现在,我将代码复制到一种非常简单的方法来检查问题,然后该按钮仍然无法使用,更具体地说,当调用displayQuantity时(当我尝试调试器时)。任何帮助。 (也是我的初学者,所以请对我轻松一点)。

public class world_tour extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_world_tour);
    }

    int quantity = 1;

    /**
     * This method displays the given quantity value on the screen.
     */
    private void displayQuantity(int number) {
        TextView quantityTextView = findViewById(R.id.world_text_view);
        quantityTextView.setText(number);
    }


    /** this method is used to increase quantity*/
    public void add (View view) {
        quantity = quantity + 1;

        displayQuantity(quantity);
    }
}
XML
<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:layout_height="match_parent"
    tools:context=".world_tour">


    <TextView
        android:id="@+id/world_text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:paddingLeft="8dp"
        android:paddingRight="8dp"
        android:paddingBottom="16dp"
        android:text="1"
        android:textAlignment="center"
        android:textColor="#000000"
        android:textSize="16sp"/>

    <Button
        android:layout_width="48dp"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:text="+"
        android:onClick="add"/>

</LinearLayout>

the logcat

java.lang.IllegalStateException: Could not execute method for android:onClick
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
        at android.view.View.performClick(View.java:6205)
        at android.widget.TextView.performClick(TextView.java:11103)
        at android.view.View$PerformClick.run(View.java:23653)
        at android.os.Handler.handleCallback(Handler.java:751)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:154)
        at android.app.ActivityThread.main(ActivityThread.java:6682)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Method.invoke(Native Method)
        at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
        at android.view.View.performClick(View.java:6205) 
        at android.widget.TextView.performClick(TextView.java:11103) 
        at android.view.View$PerformClick.run(View.java:23653) 
        at android.os.Handler.handleCallback(Handler.java:751) 
        at android.os.Handler.dispatchMessage(Handler.java:95) 
        at android.os.Looper.loop(Looper.java:154) 
        at android.app.ActivityThread.main(ActivityThread.java:6682) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
     Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x2
        at android.content.res.Resources.getText(Resources.java:1178)
        at android.widget.TextView.setText(TextView.java:5164)
        at com.example.android.franchjapp.world_tour.displayQuantity(world_tour.java:25)
        at com.example.android.franchjapp.world_tour.add(world_tour.java:33)
java android view onclick
1个回答
0
投票

XML代码:

<Button
    android:layout_width="48dp"
    android:layout_height="wrap_content"
    android:id="@+id/button1"
    android:layout_marginRight="16dp"
    android:text="+"
    />

Java代码:

Button button = (Button)findViewById(R.id.button1);
            // Register the onClick listener with the implementation above
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // do something when the corky is clicked
                }
            });

我认为这可能会做好您的工作

© www.soinside.com 2019 - 2024. All rights reserved.