簡體   English   中英

致命異常:java.lang.RuntimeException:無法實例化服務

[英]FATAL EXCEPTION: java.lang.RuntimeException: Unable to instantiate service

我是android的初學者。 我創建了一個新服務,並在android mobile API Level-23中運行它。 當我按下按鈕啟動服務時,顯示錯誤。 現在我創建一個新項目,但問題在那里。

**FATAL EXCEPTION: java.lang.RuntimeException: Unable to instantiate service com.example.uzair.servicesapp.myIntentService: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources android.content.Context.getResources()' on a null object reference** 

MainActivity.java

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private static int i =0;
    private Intent  ii;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ii = new Intent(this,myIntentService.class);
       Toast.makeText(this, "this is custom toast", Toast.LENGTH_SHORT).show();
 }

    public void clickme2(View view)
    {
        startService(ii);
        Toast.makeText(this, " Service start toast", Toast.LENGTH_SHORT).show();
    }

    public void clickme(View view) {

 stopService(ii);
Toast.makeText(this, "Service stop toast", Toast.LENGTH_SHORT).show(); 
        i++;
    }
}

MyIntentService.java

import android.app.IntentService;
import android.content.Intent;
import android.widget.Toast;
public class myIntentService extends IntentService {

    public myIntentService()
    {
        super("myIntentService");
        Toast.makeText(this, "IntentService Constuctor toast", Toast.LENGTH_SHORT);
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        Toast.makeText(this, "OnhandleIntent toast", Toast.LENGTH_SHORT);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        Toast.makeText(this, "Destroy toast", Toast.LENGTH_SHORT);
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
}

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.uzair.servicesapp">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service android:name=".myIntentService"/>


    </application>

</manifest>

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.uzair.servicesapp.MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:id="@+id/textView" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="clickme"
        android:text="@string/click_me"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="118dp" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="clickme2" />
</RelativeLayout>

您正在創建和聲明服務,因此您需要在Manisfest文件中為此服務創建一個意圖過濾器。 在清單文件中編輯:

    <service android:name=".myIntentService">
       <intent-filter>
            <action android:name="com.example.uzair.servicesapp.myIntentService" />
        </intent-filter>
    </service>

刪除此行:

Toast.makeText(this, "IntentService Constuctor toast", Toast.LENGTH_SHORT);

首先,您沒有在任何Toast對象上調用show() ,因此不會顯示任何內容。 使用Log.d()將消息記錄到LogCat。

其次,不要使用this或從構造函數調用Service上的繼承方法。

如果那沒有幫助,請編輯問題並發布整個Java堆棧跟蹤,而不僅僅是錯誤消息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM