java - 由于 java.Lang.RuntimeException,应用程序不断关闭

标签 java android service

我在后台编写了一个简单的练习应用程序,以便在按下按钮时使用 Started Services 不断生成随机数。

不幸的是,应用程序不断关闭,并出现java.Lang.RuntimeException,并显示消息无法启动服务。谁能告诉我为什么会发生这种情况?

MyService.class

import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.widget.Toast;

import androidx.annotation.Nullable;

import java.util.Random;

public class MyService extends Service {

    Random randomGenerator;

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        randomGenerator = new Random();
        Toast.makeText(this, randomGenerator.nextInt(1000), Toast.LENGTH_SHORT).show();
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}

MainActivity.class

import androidx.appcompat.app.AppCompatActivity;

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

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    TextView timerTextView;
    private Button startButton;
    private Button stopButton;

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

        timerTextView = findViewById(R.id.timerTextView);
        startButton = findViewById(R.id.startButton);
        stopButton = findViewById(R.id.stopButton);

        startButton.setOnClickListener(this);
        stopButton.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v == startButton)
            startService(new Intent(this, MyService.class));

        if (v == stopButton)
            stopService(new Intent(this, MyService.class));
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/timerTextView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="225dp"
        android:text="@string/timerString"
        android:textStyle="bold"
        android:textSize="50sp"
        android:textAlignment="center"/>

    <Button
        android:id="@+id/startButton"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/timerTextView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:layout_marginTop="50dp"
        android:text="@string/startButton"
        android:textSize="17sp"
        android:textStyle="bold"/>

    <Button
        android:id="@+id/stopButton"
        android:layout_width="300dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toBottomOf="@id/startButton"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        android:text="@string/stopButton"
        android:textSize="17sp"
        android:textStyle="bold"/>

</androidx.constraintlayout.widget.ConstraintLayout>

堆栈跟踪

2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err: android.content.res.Resources$NotFoundException: String resource ID #0x2d2
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at android.content.res.Resources.getText(Resources.java:360)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at android.content.res.MiuiResources.getText(MiuiResources.java:97)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at android.widget.Toast.makeText(Toast.java:309)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at com.example.servicespractice.MyService.onStartCommand(MyService.java:26)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3700)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.access$1700(ActivityThread.java:200)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at android.os.Looper.loop(Looper.java:201)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6823)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2020-04-19 20:20:53.379 8493-8493/com.example.servicespractice E/ervicespractic: Invalid ID 0x00000200.
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err: android.content.res.Resources$NotFoundException: String resource ID #0x200
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.content.res.Resources.getText(Resources.java:360)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.content.res.MiuiResources.getText(MiuiResources.java:97)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.widget.Toast.makeText(Toast.java:309)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at com.example.servicespractice.MyService.onStartCommand(MyService.java:26)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3700)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.access$1700(ActivityThread.java:200)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:106)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at android.os.Looper.loop(Looper.java:201)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:6823)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:547)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:873)
2020-04-19 20:20:53.511 8493-8541/com.example.servicespractice I/ervicespractic: ProcessProfilingInfo new_methods=1031 is saved saved_to_disk=1 resolve_classes_delay=8000

最佳答案

替换

Toast.makeText(this, randomGenerator.nextInt(1000), Toast.LENGTH_SHORT).show()

与:

Toast.makeText(this,Integer.toString(randomGenerator.nextInt(1000)), Toast.LENGTH_SHORT).show();

关于java - 由于 java.Lang.RuntimeException,应用程序不断关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61306225/

相关文章:

java - 使用指向派生对象的基引用调用重写方法,在运行时将调用什么?

java - 无法调试 gwt 2.8 和 java 1.8 版本的 gwt 测试

java - 需要从相机和图库中选择图像的代码 - 适用于所有 Android 手机

java - 使用java将String转换为JSONArray

android - Android-顺序音频播放?

java - 为什么Room数据库在重启应用后会丢失数据?

android - 可调试的Android应用

ajax - symfony2,从 Ajax 客户端调用服务

vb.net - 使用 vb.net 查看底层 SOAP 消息

Angular 8 : Define a utils service as singleton and with static methods?