java - 意图服务使我的应用程序崩溃?
问题描述
像这个问题一样,堆栈溢出时会回答不同的问题。但是这些问题中使用的代码与我使用的代码不同。当按下主要活动上的按钮时,我只是在调用 intent_service。下面的链接引用了一个图像,代码显示了在按下按钮时如何调用 intent_service;
package com.example.mk141.intentservicenotworking;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void start_intent_service(View view)
{
Intent intent_service=new Intent(this,Intent_Service.class);
startActivity(intent_service);
}
}
Intent_Service 类中的代码如下:
package com.example.mk141.intentservicenotworking;
import android.content.Intent;
import android.app.IntentService;
import android.util.Log;
public class Intent_Service extends IntentService
{
private static final String
TAG="com.example.mk141.intentservicenotworking";
public Intent_Service(String name)
{
super(name);
}
@Override
protected void onHandleIntent(Intent intent)
{
Log.i(TAG,"Intent Service Started");//I disabled the Inspection but
// still crashing
// when Intent Service is called
}
}
在上面的类中,当调用意图服务时会出现一个日志,即“Intent Service Started”。我还创建了一个标签并编辑了一个过滤器,以便只显示一条日志消息,如下图所示;
出现错误,即 TAG 中的字符最多为 23 个。但禁用检查后,错误完成,如下图所示;
但是当我运行我的程序并按下 Start intent Service 时,它会崩溃,如下图所示;
如果他知道如何解决此错误,请帮助我,因为如果不解决此错误,我将无法继续。提前致谢!
解决方案
利用
startService(intent_service);
代替
startActivity(intent_service);
标签
不要在 LOG 语句中使用超过 23 个字符的 TAG。
推荐阅读
- javascript - 将 JavaScript 棋盘库添加到 Angular 项目
- java - Maven:自定义 .jar 在引用时无法解析正确的依赖项
- docker - 由于代理问题窗口,Docker Compose 无法正常工作
- java - 来自 MainActivity 的 BroadcastReceiver 没有收到额外的
- c++ - 如何修复我的代码中的无限循环?我认为它跳过了第二个 cin 所以它一直在循环
- r - 将 geom_line(stat='count') 与 geom_smooth() 与分组变量相结合
- apache-calcite - Calcite 将 sql 解析为多数据源的部分
- docker - 无法在 metasploit 框架 docker 映像中定义别名
- eclipse - 如何设置 SPARK_LOCAL_IP 地址
- excel - 如果函数基于另一个条件