Android IntentService 无法实例化类;没有空的构造函数

Posted

技术标签:

【中文标题】Android IntentService 无法实例化类;没有空的构造函数【英文标题】:Android IntentService can't instantiate class; no empty constructor 【发布时间】:2013-09-27 04:02:58 【问题描述】:

我有一个需要访问在线 API 的 MainActivity 类(因此使用网络资源)。这需要我在单独的文件 HttpRequestService.java 中创建的后台线程。

MainActivity.java:

public class MainActivity extends Activity 
    public static final String API_KEY = "KEYKEYKEYKEYKEY";
    public static final String CLIENT_ID = "IDIDIDIDIDIDID";
    private final String BROADCAST_ACTION = "com.example.BROADCAST";
    private final String EXTENDED_DATA_STATUS = "com.example.STATUS";
    static final String LOGCAT_TAG = "TAGTAGTAGTAGTAG";

    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    

    @Override
    protected void onResume() 
        super.onResume();

        String token = "TOKENTOKENTOKENTOKEN";
        String urlString = "https://www.example.com/api?key=" + API_KEY;

        // Create and start intent for HttpRequestService background thread.
        Intent httpRequestServiceIntent = new Intent(this, HttpRequestService.class);
        httpRequestServiceIntent.putExtra("token", token);
        httpRequestServiceIntent.putExtra("urlString", urlString);
        httpRequestServiceIntent.putExtra("client_id", CLIENT_ID);
        DownloadStateReceiver downloadStateReceiver = new DownloadStateReceiver();
        LocalBroadcastManager.getInstance(this).registerReceiver(downloadStateReceiver, new IntentFilter(BROADCAST_ACTION));
        this.startService(httpRequestServiceIntent);
    

    private class DownloadStateReceiver extends BroadcastReceiver 
        // Broadcast receiver for receiving status updates from IntentService
        private DownloadStateReceiver() 
            // prevents instantiation
        
        @Override
        public void onReceive(Context context, Intent intent) 
            String message = intent.getStringExtra(EXTENDED_DATA_STATUS);
            Log.d(LOGCAT_TAG, "onReceive received message: " + message);
            Toast.makeText(context, "Success!: " + message, Toast.LENGTH_LONG).show();
        
    

HttpRequestService.java:

public final class HttpRequestService extends IntentService 

    public HttpRequestService(String name) 
        super(name);
        Log.e(LOGCAT_TAG, "You called the HttpRequestService directly. Don't do that. Call onHandleIntent.");
    

    private final String LOGCAT_TAG = "HttpRequestService";
    private final String BROADCAST_ACTION = "com.genda.dayplanner.BROADCAST";
    private final String EXTENDED_DATA_STATUS = "com.genda.dayplanner.STATUS";

    @Override
    protected void onHandleIntent(Intent intent) 
        // Gets data from the incoming Intent
        String token = intent.getStringExtra("token");
        String urlString = intent.getStringExtra("urlString");
        String client_id = intent.getStringExtra("client_id");
        Boolean extraError = false;
        if (token == null) 
            Log.e(LOGCAT_TAG, "Intent didn't contain required token.");
            extraError = true;
        
        if (urlString == null) 
            Log.e(LOGCAT_TAG, "Intent didn't contain required urlBundle.");
            extraError = true;
        
        if (client_id == null) 
            Log.e(LOGCAT_TAG, "Intent didn't contain required client_id.");
            extraError = true;
        
        if (extraError == true) 
            // error response
        

        // Now do request

        URL url = null;
        try 
            url = new URL(urlString);
         catch (MalformedURLException e) 
            Log.e(LOGCAT_TAG, "The URL for requesting the tasks API was malformed.");
        
        HttpURLConnection conn = null;
        try 
            conn = (HttpURLConnection) url.openConnection();
         catch (IOException e) 
            Log.e(LOGCAT_TAG, "Bad connection to the API URL");
        
        conn.addRequestProperty("client_id", client_id);
        conn.setRequestProperty("Authorization", "OAuth " + token);
        String result = null;
        try 
            InputStream in = new BufferedInputStream(conn.getInputStream());
            result = convertStreamToString(in);
         catch (IOException e) 
            e.printStackTrace();
         finally 
            conn.disconnect();
        

        // Create new Intent with URI object
        Intent localIntent = new Intent(BROADCAST_ACTION);
        localIntent.putExtra(EXTENDED_DATA_STATUS, result);
        // Broadcasts intent to receivers in this app.
        LocalBroadcastManager.getInstance(this).sendBroadcast(localIntent);
    

    public static String convertStreamToString(InputStream in) 
        // Use BufferedReader.readLine() method. Iterate until BufferedReader returns null (no more data to read). Each line appended to StringBuilder.
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder sb = new StringBuilder();
        String line = null;
        try 
            while ((line = reader.readLine()) != null) 
                sb.append(line + "\n");
            
         catch (IOException e) 
            e.printStackTrace();
         finally 
            try 
                in.close();
             catch (IOException e) 
                e.printStackTrace();
            
        
        return sb.toString();
    

这是 LogCat:

09-21 17:15:28.157: D/dalvikvm(11055): newInstance failed: no <init>()
09-21 17:15:28.157: D/androidRuntime(11055): Shutting down VM
09-21 17:15:28.157: W/dalvikvm(11055): threadid=1: thread exiting with uncaught exception (group=0x41f3b700)
09-21 17:15:28.167: E/AndroidRuntime(11055): FATAL EXCEPTION: main
09-21 17:15:28.167: E/AndroidRuntime(11055): java.lang.RuntimeException: Unable to instantiate service com.example.HttpRequestService: java.lang.InstantiationException: can't instantiate class com.example.HttpRequestService; no empty constructor
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2561)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.app.ActivityThread.access$1600(ActivityThread.java:141)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1338)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.os.Looper.loop(Looper.java:137)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.app.ActivityThread.main(ActivityThread.java:5103)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at java.lang.reflect.Method.invokeNative(Native Method)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at java.lang.reflect.Method.invoke(Method.java:525)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at dalvik.system.NativeStart.main(Native Method)
09-21 17:15:28.167: E/AndroidRuntime(11055): Caused by: java.lang.InstantiationException: can't instantiate class com.example.HttpRequestService; no empty constructor
09-21 17:15:28.167: E/AndroidRuntime(11055):    at java.lang.Class.newInstanceImpl(Native Method)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at java.lang.Class.newInstance(Class.java:1130)
09-21 17:15:28.167: E/AndroidRuntime(11055):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:2558)
09-21 17:15:28.167: E/AndroidRuntime(11055):    ... 10 more

我认为重要的部分是can't instantiate class com.example.HttpRequestService; no empty constructor。我见过其他几个类似的讨论,但他们都能够解决问题,因为他们的类是他们的 MainActivity 的子类。所以他们只是使子类静态并解决了问题。不幸的是,作为一个单独的类HttpRequestService 不能是静态的。有什么建议吗?

【问题讨论】:

【参考方案1】:

Exception 告诉您需要实现默认的公共构造函数。

public HttpRequestService() 
    super("Name for Service");

默认的公共构造函数是没有参数的构造函数

在这里你调用super() 并传递一个String 用于命名IntentService

【讨论】:

我的代码类似,但声明默认构造函数对我不起作用。 创建一个问题,所以我们所有的 SO 都可以尝试提供帮助。 来看看:***.com/questions/21168579/… 因为IntentService 需要它。它有一个接受String的构造函数。【参考方案2】:

要记住的另一件事:如果您将服务作为嵌套类,它必须是静态的。

比较intentservice no empty constructor, but there is a constructor

【讨论】:

这不是他要求的,但这正是我所需要的!谢谢!虽然使嵌套类静态破坏了我的设计......

以上是关于Android IntentService 无法实例化类;没有空的构造函数的主要内容,如果未能解决你的问题,请参考以下文章

如何从 IntentService 收集信息并更新 Android UI

Android16.4 IntentService类

android 中IntentService的使用场景

Android 多线程之IntentService 完全详解

Android IntentService

Android Training - 使用IntentService运行任务(Lesson 2 - 发送任务给IntentService)