Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/373.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java android.content.res.Resources$NotFoundException:字符串资源ID 0x4_Java_Android - Fatal编程技术网

Java android.content.res.Resources$NotFoundException:字符串资源ID 0x4

Java android.content.res.Resources$NotFoundException:字符串资源ID 0x4,java,android,Java,Android,我一直在尝试制作一个计时器应用程序。用户将输入秒数,搜索栏以递减方式移动。但只要我点击按钮,进度条就会移动到该值,但应用程序崩溃。我认为在进度条更新之前,代码运行良好,但在更新之后,它崩溃了。我已经为这个代码挣扎了几个小时。请帮忙,我是android应用开发的新手 以下是日志:- 2020-02-11 13:17:21.081 4121-4121/com.example.timer E/m.example.time: Invalid ID 0x00000004. 2020-02-11 13:17

我一直在尝试制作一个计时器应用程序。用户将输入秒数,搜索栏以递减方式移动。但只要我点击按钮,进度条就会移动到该值,但应用程序崩溃。我认为在进度条更新之前,代码运行良好,但在更新之后,它崩溃了。我已经为这个代码挣扎了几个小时。请帮忙,我是android应用开发的新手

以下是日志:-

2020-02-11 13:17:21.081 4121-4121/com.example.timer E/m.example.time: Invalid ID 0x00000004.
2020-02-11 13:17:21.081 4121-4121/com.example.timer D/AndroidRuntime: Shutting down VM
2020-02-11 13:17:21.083 4121-4121/com.example.timer E/AndroidRuntime: FATAL EXCEPTION: main

Process: com.example.timer, PID: 4121
android.content.res.Resources$NotFoundException: String resource ID #0x4
    at android.content.res.Resources.getText(Resources.java:348)
    at android.widget.TextView.setText(TextView.java:5846)
    at com.example.timer.MainActivity$1.onTick(MainActivity.java:29)
    at android.os.CountDownTimer$1.handleMessage(CountDownTimer.java:130)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:193)
    at android.app.ActivityThread.main(ActivityThread.java:6715)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:911)
XML


我认为问题在于:

textView.setText(secondsLeft);
尝试:


无法将整数值放入TextView。

单击“在后台线程上运行”,因此请通过runOnUIThread或使用视图处理程序运行
package com.example.timer;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.os.CountDownTimer;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    public void start(View view){
        EditText editText = findViewById(R.id.editText);
        final TextView textView = findViewById(R.id.textView2);
        final SeekBar seekBar = findViewById(R.id.seekBar);
        int timeGiven = Integer.parseInt(editText.getText().toString());
        seekBar.setMax(timeGiven);
        seekBar.setProgress(timeGiven);

        new CountDownTimer(timeGiven*1000, 1000){
            public void onTick(long millisecondsLeft){

                int secondsLeft = (int) millisecondsLeft/1000;
                seekBar.setProgress(secondsLeft);
                textView.setText(secondsLeft);
            }
            public void onFinish(){

                Toast.makeText(getApplicationContext(),"BOOM",Toast.LENGTH_SHORT).show();
            }
        }.start();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}
textView.setText(secondsLeft);
textView.setText(String.valueOf(secondsLeft));