[由于Java.Lang.RuntimeException,应用程序不断关闭

问题描述 投票:0回答:1

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

不幸的是,应用程序通过java.Lang.RuntimeException并显示消息Unable to start service一直关闭。谁能告诉我为什么会这样吗?

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>

The Stacktrace

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
java android service
1个回答
1
投票
替换
© www.soinside.com 2019 - 2024. All rights reserved.