You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android应用输入崩溃:Resources$NotFoundException问题排查修复

Android应用输入内容崩溃问题排查与修复

问题现象

应用启动正常,但输入内容时崩溃退回主屏幕,崩溃日志提示android.content.res.Resources$NotFoundException: String resource ID #0x1。

崩溃日志

E/.example.carap: [qarth_debug:]  get PatchStore::createDisableExceptionQarthFile method fail.
E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.carapp, PID: 11557
    android.content.res.Resources$NotFoundException: String resource ID #0x1
        at android.content.res.Resources.getText(Resources.java:430)
        at android.content.res.HwResources.getText(HwResources.java:463)
        at android.widget.TextView.setText(TextView.java:6036)
        at com.example.carapp.carapp$1.recalc(carapp.java:51)
        at com.example.carapp.carapp$1.onTextChanged(carapp.java:34)
        at android.widget.TextView.sendOnTextChanged(TextView.java:10069)
        at android.widget.TextView.handleTextChanged(TextView.java:10166)
        at android.widget.TextView$ChangeWatcher.onTextChanged(TextView.java:12963)
        at android.text.SpannableStringBuilder.sendTextChanged(SpannableStringBuilder.java:1267)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:579)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:315)
        at android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:506)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:305)
        at androidx.emoji2.text.SpannableBuilder.replace(SpannableBuilder.java:49)
        at android.text.method.NumberKeyListener.onKeyDown(NumberKeyListener.java:131)
        at android.widget.TextView.doKeyDown(TextView.java:7914)
        at android.widget.TextView.onKeyDown(TextView.java:7691)
        at android.view.KeyEvent.dispatch(KeyEvent.java:2748)
        at android.view.View.dispatchKeyEvent(View.java:12559)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1905)
        at com.android.internal.policy.DecorView.superDispatchKeyEvent(DecorView.java:553)
        at com.android.internal.policy.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1919)
        at android.app.Activity.dispatchKeyEvent(Activity.java:3548)
        at androidx.core.app.ComponentActivity.superDispatchKeyEvent(ComponentActivity.java:124)
        at androidx.core.view.KeyEventDispatcher.dispatchKeyEvent(KeyEventDispatcher.java:86)
        at androidx.core.app.ComponentActivity.dispatchKeyEvent(ComponentActivity.java:142)
        at androidx.appcompat.app.AppCompatActivity.dispatchKeyEvent(AppCompatActivity.java:601)
        at androidx.appcompat.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:60)
        at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.dispatchKeyEvent(AppCompatDelegateImpl.java:3106)
        at com.android.internal.policy.DecorView.dispatchKeyEvent(DecorView.java:427)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processKeyEvent(ViewRootImpl.java:5663)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:5531)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4983)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5036)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5002)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:5157)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5010)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:5214)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4983)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:5036)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:5002)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:5010)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4983)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:7803)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:7768)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:7726)
        at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:4551)
E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:112)
        at android.os.Looper.loop(Looper.java:216)
        at android.app.ActivityThread.main(ActivityThread.java:7625)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:524)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:987)

相关代码

MainActivity

package com.example.carapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void startactivityone(View v) {
        Intent intent = new Intent(this, carapp.class);
        startActivity(intent);
    }
    public void startactivitytwo(View v) {
        Intent intentt = new Intent(this, car_insructional1.class);
        startActivity(intentt);
    }
}

carapp Activity

package com.example.carapp;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;

public class carapp extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.carapp);
        Spinner typeofroad = (Spinner) findViewById(R.id.typeofroad);
        ArrayAdapter<CharSequence> typeofroadAdapter = ArrayAdapter
                .createFromResource(this,R.array.typesofroad, android.R.layout.simple_spinner_item);
        typeofroadAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        typeofroad.setAdapter(typeofroadAdapter);
        //Привязка списка к списку(выбор дороги)

        final EditText probegavto = (EditText) findViewById(R.id.probeg);
        final EditText resultmoney = (EditText) findViewById(R.id.resultnumber);
        probegavto.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                recalc();
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }


            public void recalc(){
                float fresult;
                try {
                    fresult = Float.parseFloat(probegavto.getText().toString());
                } catch (NumberFormatException e) {
                    fresult=0;
                }
                int position = typeofroad.getSelectedItemPosition();
                float fotvet = Calculate.calculateresult(position,fresult);
                resultmoney.setText((int) fotvet);
            }
            });
        }

Calculate类

package com.example.carapp;

public class Calculate {
    static float[] factor = {1F, 0.5F,0.8F};
    static float[] money = {1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F,1F};
    public static float calculateresult(int position,float fresult) {
        if (fresult < 10000) {
            return (money[0]);
        } else if ((fresult * factor[position]) > 200000){
            return (money[19]);
        } else{
            float v = (fresult * factor[position]) / 10000;
            return money[(int) v];
        }
    }
}

注:money数组实际值会有所不同

问题原因

崩溃日志指向carapp.java:51的resultmoney.setText((int) fotvet);代码。TextView.setText()有多个重载方法:当传入int类型参数时,系统会将其视为字符串资源ID,去res/values/strings.xml中查找对应字符串,但你传入的是计算后的数值(比如1),而资源中不存在ID为0x1的字符串,因此抛出Resources$NotFoundException。

修复方法

核心修复:将数值转为字符串

需把计算结果转换成字符串类型再传入setText(),两种常用方式:

  1. 使用String.valueOf()转换:
resultmoney.setText(String.valueOf((int) fotvet));
  1. 拼接空字符串强制转换:
resultmoney.setText((int) fotvet + "");

附加优化:避免数组越界

在Calculate.calculateresult方法中,确保数组索引不会超出money数组的范围:

public static float calculateresult(int position,float fresult) {
    if (fresult < 10000) {
        return money[0];
    } 
    float calcValue = fresult * factor[position];
    if (calcValue > 200000){
        return money[19];
    } else{
        int index = (int) (calcValue / 10000);
        // 确保索引不超过数组最大下标
        index = Math.min(index, money.length - 1);
        return money[index];
    }
}

内容的提问来源于stack exchange,提问作者TuRb0Z

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.13 16:25:21