gpt4 book ai didi

android - java.lang.RuntimeException : Unable to instantiate service {. ...} 无法实例化类;没有空的构造函数

转载 作者:行者123 更新时间:2023-11-29 00:12:43 25 4
gpt4 key购买 nike

尝试在我的 Android APP 中启动服务时出现错误我在暂停 Activity 之前启动服务,因此我可以将一些信息上传到服务器。

    java.lang.RuntimeException: Unable to instantiate service  can't instantiate class ; no empty constructor

启动服务时出现问题:

 protected void onPause() {
super.onPause();

EditText nicknameText = (EditText) findViewById(R.id.EditText_Nickname);
EditText emailText = (EditText) findViewById(R.id.EditText_Email);

String strNickname = nicknameText.getText().toString();
String strEmail = emailText.getText().toString();

SharedPreferences.Editor editor = mGameSettings.edit();
editor.putString(GAME_PREFERENCES_NICKNAME, strNickname);
editor.putString(GAME_PREFERENCES_EMAIL, strEmail);

editor.commit();

//Antes de salir lanzamos una actividad nueva para hacer el upload de la info

Intent uploadService = new Intent(getApplicationContext(),UploaderService.class);
startService(uploadService);


}

然后 Service 会启动一个 AsyncTask

public  class UploaderService extends Service{

public UploaderService(){
super();
}

private UpLoadUserData upLoadUserData;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
upLoadUserData = new UpLoadUserData();
upLoadUserData.execute();
Log.d(DEBUG_TAG, "Settings and image upload requested");
return START_REDELIVER_INTENT;
}

@Override
public IBinder onBind(Intent intent) {
// no binding
return null;
}
}

list :

    <?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidbook.btdt.hour6"
android:versionCode="1"
android:versionName="1.0"
android:installLocation="auto">
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="10"></uses-sdk>

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:debuggable="true">
<activity
android:name=".QuizSplashActivity"
android:label="@string/app_name">
<intent-filter>
<action
android:name="android.intent.action.MAIN" />
<category
android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="QuizHelpActivity"></activity>
<activity
android:name="QuizMenuActivity"></activity>
<activity
android:name="QuizScoresActivity"></activity>
<activity
android:name="QuizSettingsActivity"></activity>
<activity
android:name="QuizGameActivity"></activity>
<service android:name="QuizSettingsActivity$UploaderService"></service>
</application>
</manifest>

最佳答案

首先,您不能拥有一个普通嵌套类的 Service。它必须是一个 static 嵌套类。

其次,ServiceActivitystatic 嵌套类,这很奇怪。

第三,让 Service 生成 AsyncTask,而不是仅仅使用 IntentService,这很奇怪。

第四,将getApplicationContext()替换为this。仅当您知道为什么要使用 getApplicationContext() 时才使用 getApplicationContext(),此处不需要它。

关于android - java.lang.RuntimeException : Unable to instantiate service {. ...} 无法实例化类;没有空的构造函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29066293/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com