简体   繁体   English

Android Studio 问题; java.lang.IllegalStateException: 无法执行 android:onClick 的方法

[英]Android Studio problem ; java.lang.IllegalStateException: Could not execute method for android:onClick

I am new in Android studio, I am creating an app and I can't get it to run properly.我是 Android Studio 的新手,我正在创建一个应用程序,但无法正常运行。

The purpose is When I press the button order, one of the textView 's shows in text "5", by default is 0.目的是当我按下按钮顺序时, textView之一显示在文本“5”中,默认情况下为 0。

This is the activitymain.xml这是activitymain.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.amc.alvaro.justjava.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Quantity"
        android:textAllCaps="true"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:textSize="16sp"
        android:id="@+id/quantity_text_view"
        android:textColor="@android:color/black"
        android:layout_marginBottom="16dp"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Price"
        android:textAllCaps="true"
        android:layout_marginBottom="16dp"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        android:layout_marginBottom="16dp"
        android:textColor="@android:color/black"
        android:id="@+id/price_text_view"/>

    <Button
        android:text="ORDER"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button"
        android:layout_marginTop="16dp"
        android:onClick="submitOrder"/>
</LinearLayout>

This is the MainActivity.java这是MainActivity.java

package com.amc.alvaro.justjava;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;

/**
 * This app displays an order form to order coffee.
 */
public class MainActivity extends AppCompatActivity {

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

    /**
     * This method is called when the order button is clicked.
     */
    public void submitOrder(View view) {
        int quantity = 5;
        display(quantity);
    }

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

}

This is the logcat这是logcat

02/04 13:29:36: Launching app
$ adb shell am start -n "com.amc.alvaro.justjava/com.amc.alvaro.justjava.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER
Client not ready yet..Connected to process 4458 on device Nexus_5_API_24 [emulator-5554]
I/InstantRun: Instant Run Runtime started. Android package is com.amc.alvaro.justjava, real application class is null.
W/System: ClassLoader referenced unknown path: /data/app/com.amc.alvaro.justjava-1/lib/x86
W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Swap behavior 1
E/EGL_emulation: tid 4510: eglSurfaceAttrib(1146): error 0x3009 (EGL_BAD_MATCH)
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xab3b93a0, error=EGL_BAD_MATCH
W/ResourceType: No package identifier when getting value for resource number 0x00000005
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.amc.alvaro.justjava, PID: 4458
                  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:5610)
                      at android.view.View$PerformClick.run(View.java:22265)
                      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:6077)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                   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:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      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:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
                   Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5
                      at android.content.res.Resources.getText(Resources.java:331)
                      at android.widget.TextView.setText(TextView.java:4554)
                      at com.amc.alvaro.justjava.MainActivity.display(MainActivity.java:33)
                      at com.amc.alvaro.justjava.MainActivity.submitOrder(MainActivity.java:23)
                      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:5610) 
                      at android.view.View$PerformClick.run(View.java:22265) 
                      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:6077) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 
Application terminated.

You need to call textView.setText(Integer.toString(number)).您需要调用 textView.setText(Integer.toString(number))。 Passing in an integer makes it look for a string resource with that id, which is unlikely to exist (and if it does is some random string).传入一个整数使其查找具有该 id 的字符串资源,该资源不太可能存在(如果存在,则是一些随机字符串)。

Also: its always the bottom exception that's the real problem "Caused by: android.content.res.Resources$NotFoundException: String resource ID #0x5"另外:它始终是真正的问题的底部异常“由:android.content.res.Resources$NotFoundException:String resource ID #0x5”

Its crashing because you are passing int to .setText() instead of String and android then behaves like you are passing it id of string resource which it cant find.它崩溃是因为您将int传递给.setText()而不是String ,然后 android 的行为就像您将其无法找到的字符串资源的 id 传递给它一样。

Change it to quantityTextView.setText(String.format(Locale.getDefault(), "%d", number));将其更改为quantityTextView.setText(String.format(Locale.getDefault(), "%d", number));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.IllegalStateException:无法为 android 执行方法:onClick / ZE84E30B9390CDB67ZDDB6 - java.lang.IllegalStateException: Could not execute method for android:onClick / Android java.lang.IllegalStateException:无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick in android 错误:java.lang.IllegalStateException:无法执行android:onClick的方法 - Error: java.lang.IllegalStateException: Could not execute method for android:onClick Kotlin-java.lang.IllegalStateException:无法执行android:onClick的方法 - Kotlin - java.lang.IllegalStateException: Could not execute method for android:onClick Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception - Android Studio Application - java.lang.IllegalStateException: Could not execute method for android:onClick - Not a Null Pointer Exception Android Studio 3.1.3 中的错误。 java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - Error in Android Studio 3.1.3. java.lang.IllegalStateException: Could not execute method for android:onClick Android-Java:java.lang.IllegalStateException:无法执行 android 的方法:onClick - Android-Java: java.lang.IllegalStateException: Could not execute method for android:onClick ANDROID意图致命错误!!! java.lang.IllegalStateException: 无法执行 android:onClick 的方法 - ANDROID INTENTION FATAL ERROR!!! java.lang.IllegalStateException: Could not execute method for android:onClick java.lang.IllegalStateException:尝试单击按钮时无法执行android:onClick的方法 - java.lang.IllegalStateException: Could not execute method for android:onClick when trying to click a button Tictac toe 游戏不断停止并出现 logcat 错误 java.lang.IllegalStateException:Could not execute method for android:onClick - Tictac toe game keeps stopping with logcat error java.lang.IllegalStateException:Could not execute method for android:onClick
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM