繁体   English   中英

意向服务使我的应用程序崩溃?

[英]Intent Service crashes my app?

像这样的问题,在堆栈溢出时会回答不同的问题。 但是那些问题中使用的代码与我使用的代码不同。 当按下主要活动上的按钮时,我只是在调用intent_service。 下面的链接是一张图片,代码显示了按下按钮时如何调用intent_service;

: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
    }
}

在上面的类中,当调用意图服务即“意图服务已启动”时,将显示一个日志。 我还创建了一个TAG并编辑了一个过滤器,以便只有一个日志消息出现,如下图所示;

编辑过滤器1

编辑过滤器2

出现错误,即TAG中的字符最多为23。但是禁用检查后,错误已完成,如下图所示;

禁用检查

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

应用程序崩溃1 应用程序崩溃2

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

采用

startService(intent_service); 

代替

startActivity(intent_service);

对于TAG

不要在LOG语句中使用超过23个字符的TAG。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM