android.content.res.$NotFoundException: String资源I



我正在编写一个应用程序,它启动时没有错误,但是当我试图在行中输入一些东西时,它崩溃并移动到主屏幕(总共3个屏幕)

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活动:

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);
            }
            });
        }

和类calate:

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];
        }
    }
}

*****数组的钱会不同

我试着重写代码,但它没有导致任何成功。如果可以,请告诉我出了什么问题以及如何解决。

resultmoney.setText((int) fotvet);

setText(int)期望int是字符串资源的标识符,例如R.string.app_name。如果您想要显示一个数字,请自己将其转换为String(使用您想要的任何格式),然后将String传递给setText(String)。一种可能的实现是这样的:

resultmoney.setText(NumberFormat.getInstance().format(fotvet));