簡體   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