致命异常:java.lang.RuntimeException:无法实例化服务

时间:2017-06-05 21:21:54

标签: android android-service android-studio-2.1

我是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>

2 个答案:

答案 0 :(得分:0)

您正在创建和解决服务,因此您需要在Manisfest文件中为此服务创建一个intent-filter。在清单文件编辑中:

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

答案 1 :(得分:0)

删除此行:

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

首先,您没有在任何show()个对象上调用Toast,因此不显示任何对象。使用Log.d()将消息记录到LogCat。

其次,不要在构造函数中使用this或在Service上调用继承的方法。

如果这没有帮助,请编辑问题并发布整个Java堆栈跟踪,而不仅仅是错误消息。