很遗憾,intentservice崩溃已经停止了错误

时间:2015-03-21 11:10:24

标签: android intentservice

我想测试一下intentservice。

当我在eclipse中运行app时,第一个日志显示我,然后退出并出现此错误:不幸的是,a已停止

现在我不知道如何解决这个问题:(

我有这个片段:

的AndroidManifest.xml

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

    <uses-sdk
        android:minSdkVersion="15"
        android:targetSdkVersion="21" />

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

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <service
            android:name=".se_service"
            android:exported="false" />
    </application>

</manifest>

MainActivity.java

package com.example.a;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class MainActivity extends Activity {
    private static final String TAG = "states";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Log.d(TAG, "onCreate");

        Intent i = new Intent(this, se_service.class);
        startService(i);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

se_service.java

package com.example.a;

import android.app.IntentService;
import android.content.Intent;
import android.util.Log;

public class se_service extends IntentService {
    private static final String TAG = "states";

    public se_service(String name) {
        super(se_service.class.getName());
        Log.d(TAG, "se_service");
    }

    @Override
    protected void onHandleIntent(Intent arg0) {
        Log.d(TAG, "onHandleIntent");       
    }

}

eclipse日志:

03-21 06:56:24.447: D/dalvikvm(2176): Not late-enabling CheckJNI (already on)
03-21 06:56:24.497: D/states(2176): onCreate
03-21 06:56:24.517: D/dalvikvm(2176): newInstance failed: no <init>()
03-21 06:56:24.517: D/AndroidRuntime(2176): Shutting down VM
03-21 06:56:24.517: W/dalvikvm(2176): threadid=1: thread exiting with uncaught exception (group=0xb1f15648)
03-21 06:56:24.517: E/AndroidRuntime(2176): FATAL EXCEPTION: main
03-21 06:56:24.517: E/AndroidRuntime(2176): java.lang.RuntimeException: Unable to instantiate service com.example.a.se_service: java.lang.InstantiationException: can't instantiate class com.example.a.se_service; no empty constructor
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.app.ActivityThread.access$1600(ActivityThread.java:141)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.os.Looper.loop(Looper.java:137)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at java.lang.reflect.Method.invoke(Method.java:525)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at dalvik.system.NativeStart.main(Native Method)
03-21 06:56:24.517: E/AndroidRuntime(2176): Caused by: java.lang.InstantiationException: can't instantiate class com.example.a.se_service; no empty constructor
03-21 06:56:24.517: E/AndroidRuntime(2176):     at java.lang.Class.newInstanceImpl(Native Method)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at java.lang.Class.newInstance(Class.java:1130)
03-21 06:56:24.517: E/AndroidRuntime(2176):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2558)
03-21 06:56:24.517: E/AndroidRuntime(2176):     ... 10 more
03-21 06:56:31.307: D/dalvikvm(2191): newInstance failed: no <init>()
03-21 06:56:31.307: D/AndroidRuntime(2191): Shutting down VM
03-21 06:56:31.307: W/dalvikvm(2191): threadid=1: thread exiting with uncaught exception (group=0xb1f15648)
03-21 06:56:31.307: E/AndroidRuntime(2191): FATAL EXCEPTION: main
03-21 06:56:31.307: E/AndroidRuntime(2191): java.lang.RuntimeException: Unable to instantiate service com.example.a.se_service: java.lang.InstantiationException: can't instantiate class com.example.a.se_service; no empty constructor
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.app.ActivityThread.access$1600(ActivityThread.java:141)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.os.Looper.loop(Looper.java:137)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.app.ActivityThread.main(ActivityThread.java:5103)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at java.lang.reflect.Method.invokeNative(Native Method)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at java.lang.reflect.Method.invoke(Method.java:525)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at dalvik.system.NativeStart.main(Native Method)
03-21 06:56:31.307: E/AndroidRuntime(2191): Caused by: java.lang.InstantiationException: can't instantiate class com.example.a.se_service; no empty constructor
03-21 06:56:31.307: E/AndroidRuntime(2191):     at java.lang.Class.newInstanceImpl(Native Method)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at java.lang.Class.newInstance(Class.java:1130)
03-21 06:56:31.307: E/AndroidRuntime(2191):     at android.app.ActivityThread.handleCreateService(ActivityThread.java:2558)
03-21 06:56:31.307: E/AndroidRuntime(2191):     ... 10 more

1 个答案:

答案 0 :(得分:0)

这是因为您没有为se_service类提供任何空(即没有参数)构造函数。你需要创建一个,例如:

    public se_service() {
        super("se_service");
    }