java - Intent 服务使我的应用程序崩溃?

标签 java android onclick crash intentservice

像这个问题一样,在堆栈溢出上有不同的问题得到解答。但这些问题中使用的代码与我正在使用的代码不同。当按下主要 Activity 上的按钮时,我只是调用一个intent_service。下面的链接引用了一个图像和代码,显示了按下按钮时如何调用intent_service;

:The "Send Intent Service" Button on MainActivity

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 时,会出现一条日志,即“Intent Service Started”。我还创建了一个标签并编辑了一个过滤器,以便只显示一条日志消息,如下图所示;

Editing Filter 1

Editing Filter 2

出现错误,即 TAG 中的字符最多为 23 个。但禁用检查后,该错误结束,如下图所示;

Disabling Inspection

但是当我运行我的程序并按启动意向服务时,它崩溃了,如下图所示;

app crashes 1 app crashes 2

如果有人知道如何解决此错误,请帮助我,因为如果不解决此错误,我将无法继续。提前致谢!

最佳答案

使用

startService(intent_service); 

而不是

startActivity(intent_service);

对于标签

LOG 语句中使用的 TAG 长度不得超过 23 个字符。

关于java - Intent 服务使我的应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51186805/

相关文章:

java - 方法参数的验证

java 数据输出流 getOutputStream() getInputStream()

java - 在 CHM 或任何其他电子书格式中嵌入小程序

java - 如何在 Spring JPA 中注入(inject) CrudRepository?

java - 将应用程序范围的 boolean 值保存在 SharedPreference 中

javascript - onClick 激活所有其他 onClick 事件

javascript - Onclick 事件弹跳

android - 在运行 Google Play Services < 7.5.0 的设备上使用 GcmNetworkManager

java - 单击时旋转 ImageView 属于 RecyclerView

javascript - 带有弹出窗口的 Chrome 扩展 Onclick()