有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

由于java,安卓应用程序不断关闭。Lang.RuntimeException

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

不幸的是,应用程序一直在用java关闭。Lang.RuntimeException带有消息无法启动服务。谁能告诉我为什么会这样

我的服务。班级

import 安卓.app.Service;
import 安卓.content.Intent;
import 安卓.os.IBinder;
import 安卓.widget.Toast;

import 安卓x.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();
    }
}

主要活动。班级

import 安卓x.appcompat.app.AppCompatActivity;

import 安卓.content.Intent;
import 安卓.os.Bundle;
import 安卓.view.View;
import 安卓.widget.Button;
import 安卓.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));
    }
}

主要活动。xml

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

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

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

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

</安卓x.constraintlayout.widget.ConstraintLayout>

堆栈跟踪

2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err: 安卓.content.res.Resources$NotFoundException: String resource ID #0x2d2
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at 安卓.content.res.Resources.getText(Resources.java:360)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at 安卓.content.res.MiuiResources.getText(MiuiResources.java:97)
2020-04-19 20:20:48.147 8493-8493/com.example.servicespractice W/System.err:     at 安卓.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 安卓.app.ActivityThread.handleServiceArgs(ActivityThread.java:3700)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at 安卓.app.ActivityThread.access$1700(ActivityThread.java:200)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:106)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at 安卓.os.Looper.loop(Looper.java:201)
2020-04-19 20:20:48.148 8493-8493/com.example.servicespractice W/System.err:     at 安卓.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.安卓.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.安卓.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: 安卓.content.res.Resources$NotFoundException: String resource ID #0x200
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at 安卓.content.res.Resources.getText(Resources.java:360)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at 安卓.content.res.MiuiResources.getText(MiuiResources.java:97)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at 安卓.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 安卓.app.ActivityThread.handleServiceArgs(ActivityThread.java:3700)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at 安卓.app.ActivityThread.access$1700(ActivityThread.java:200)
2020-04-19 20:20:53.380 8493-8493/com.example.servicespractice W/System.err:     at 安卓.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at 安卓.os.Handler.dispatchMessage(Handler.java:106)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at 安卓.os.Looper.loop(Looper.java:201)
2020-04-19 20:20:53.381 8493-8493/com.example.servicespractice W/System.err:     at 安卓.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.安卓.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.安卓.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

共 (1) 个答案

  1. # 1 楼答案

    替换

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

    与:

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