java - 由于 java.Lang.RuntimeException,应用程序不断关闭
问题描述
我在后台编写了一个简单的练习应用程序,以便在按下按钮时使用 Started Services 不断生成随机数。
不幸的是,该应用程序不断关闭并出现java.Lang.RuntimeException消息Unable to start service。谁能告诉我为什么会这样?
我的服务类
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();
推荐阅读
- javascript - JQuery 从列表中删除选定的文件
- postgresql - 现有表上的分区出现错误“在规则中检测到无限递归”
- javascript - jquery Counter 不是从 0 开始到目标数
- python - 分离复杂数组的实部和虚部时的 NumPy 精度问题
- python - 如何为自定义距离矩阵构建我的数据和计算
- python - Pandas:在特定文本之后查找模式并替换模式
- javascript - 如何在悬停时更改材质 ui 卡的背景颜色?
- latex - 使用 \bibitem 引用 mathoverflow 答案(不使用 bibtex)
- mysql - 这些删除有什么区别
- mongodb - Mongo db indext.wt 和 collection.wt 文件被创建为大尺寸