gpt4 book ai didi

java - Android Studio问题; java.lang.IllegalStateException : Could not execute method for android:onClick

转载 作者:太空宇宙 更新时间:2023-11-04 11:48:58 24 4
gpt4 key购买 nike

我是 Android studio 的新手,我正在创建一个应用程序,但无法让它正常运行。

目的是当我按下按钮顺序时,textView之一显示在文本“5”中,默认情况下为0。

这是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>

这是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.
*/

}

这是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.

最佳答案

您需要调用textView.setText(Integer.toString(number))。传入一个整数会使其查找具有该 id 的字符串资源,该资源不太可能存在(如果存在,则是一些随机字符串)。

另外:它始终是真正的问题“由以下原因引起的底部异常:android.content.res.Resources$NotFoundException:字符串资源 ID #0x5”

关于java - Android Studio问题; java.lang.IllegalStateException : Could not execute method for android:onClick,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42044074/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com