错误: - 服务类没有零参数构造函数
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误: - 服务类没有零参数构造函数相关的知识,希望对你有一定的参考价值。
我是android的新手。我希望在我的活动中使用服务来在一段时间后获取JSON数据。我的服务类是我的记录类的内部类。我做了一个我的服务类的空构造函数,并尝试了不同的方法,但每次我遇到一个错误。有人可以指导我这个问题吗?
record.class
public class record extends Activity{
.....
private static String url ="https://podgier-woman.000webhostapp.com/table.php";
JSONParser jParser = new JSONParser();
......
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.record);
......
Intent intent = new Intent(record.this,
BackgroundService.class);
startService(intent);
}
class gro extends AsyncTask<String, String, String> {
protected void onPreExecute() {
super.onPreExecute();
............
}
}
protected String doInBackground(String... args) {
java.util.List<NameValuePair> pr = new ArrayList<NameValuePair>();
JSONObject json = jParser.makeHttpRequest(url, "GET", pr);
try {
JSONArray code = json.getJSONArray("code");
Log.d("list :",code.toString());
for (int i = 0; i < code.length(); i++) {
JSONObject c = code.getJSONObject(i);
asd.add(c.getString("name"));
// adding each child node to HashMap key => value
// adding contact to contact list
JSONArray p = null;
}
}catch(JSONException e){
}
return null;
}
protected void onPostExecute(String file_url) {
pDialog.dismiss();
..........
Collections.reverse(Arrays.asList(info));
customList = new CustomList(record.this,info);
listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(customList);
}
}
//-----------------------------------------------------------------------------------------------------------------------
//
//
//-----------------------------------------------------------------------------------------------------------------------
public class BackgroundService extends Service
{
public BackgroundService(){
super();
}
private static final String TAG = "BackgroundService";
private ThreadGroup myThreads = new ThreadGroup("ServiceWorker");
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "in onCreate()");
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
super.onStartCommand(intent, flags, startId);
int counter = intent.getExtras().getInt("counter");
Log.v(TAG, "in onStartCommand(), counter = " + counter +
", startId = " + startId);
new Thread(myThreads, new ServiceWorker(counter), "BackgroundService")
.start();
return START_NOT_STICKY;
}
class ServiceWorker implements Runnable
{
private int counter = -1;
public ServiceWorker(int counter) {
this.counter = counter;
}
public void run() {
final String TAG2 = "ServiceWorker:" + Thread.currentThread().getId();
// do background processing here...
try {
Log.e(TAG2, "sleeping for 5 seconds. counter = " + counter);
while(true)
{
Thread.sleep(5000);
Log.e("agdghdgdgdrgrdgrdg","dgdgd");
new gro().execute();
}
} catch (InterruptedException e) {
Log.e(TAG2, "... sleep interrupted");
}
}
}
@Override
public void onDestroy()
{
}
@Override
public IBinder onBind(Intent intent) {
Log.v(TAG, "in onBind()");
return null;
}
}
}
的manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.grover.jsonp">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".record"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name=".record$BackgroundService"/>
</application>
logcat的
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.grover.jsonp, PID: 13046
java.lang.RuntimeException: Unable to instantiate service com.grover.jsonp.record$BackgroundService: java.lang.InstantiationException: class com.grover.jsonp.record$BackgroundService has no zero argument constructor
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2721)
at android.app.ActivityThread.access$1800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.InstantiationException: class com.grover.jsonp.record$BackgroundService has no zero argument constructor
at java.lang.Class.newInstance(Class.java:1563)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2718)
at android.app.ActivityThread.access$1800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.NoSuchMethodException: <init> []
at java.lang.Class.getConstructor(Class.java:531)
at java.lang.Class.getDeclaredConstructor(Class.java:510)
at java.lang.Class.newInstance(Class.java:1561)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:2718)
at android.app.ActivityThread.access$1800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1368)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5237)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:912)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
首先,你试图使用Bounded Service
发射Intent
。哪个错了。为了做到这一点,你需要创建一个Intent Service
您正在获得零参数构造函数异常,因为您没有将服务绑定到MainActivity“是不正确的,因为构造函数的问题与使用startService()或bindService()无关。(Thanks)
要创建BoundService,您需要一个iBinder
对象来绑定您的服务。我知道这很奇怪,但Android就是这样知道的。正如您所读,有两种服务
1. Intent Service
2. Bound Service
Bound Service
需要像这样实例化。
用这个编辑你的代码,确保你用我的更新你的onBind()
。
public class BackgroundService extends Service
{
public BackgroundService(){
super();
}
private BackgroundService mBS;
// This is the object that receives interactions from clients. See
// RemoteService for a more complete example.
private final IBinder mBinder = new LocalBinder();
/**
* Class for clients to access. Because we know this service always
* runs in the same process as its clients, we don't need to deal with
* IPC.
*/
public class LocalBinder extends Binder {
BackgroundService getService() {
return BackgroundService.this;
}
}
@Override
public IBinder onBind(Intent intent) {
Log.v(TAG, "in onBind()");
return mBinder;
}
}
在您的MainActivity中,您需要一个界面 - > ServiceConnection
在MainActivity.java
中添加以下内容
BackgroundService mBackgroundService;
boolean mServiceBound = false;
private ServiceConnection mServiceConnection = new ServiceConnection() {
@Override
public void onServiceDisconnected(ComponentName name) {
mServiceBound = false;
}
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
LocalBinder myBinder = (LocalBinder ) service;
mBackgroundService = myBinder.getService();
mServiceBound = true;
}
};
//Start the service when the activity starts
@Override
protected void onStart() {
super.onStart();
Intent intent = new Intent(this, BackgroundService.class);
startService(intent);
bindService(intent, mServiceConnection, Context.BIND_AUTO_CREATE);
}
@Override
protected void onStop() {
super.onStop();
if (mServiceBound) {
unbindService(mServiceConnection);
mServiceBound = false;
}
}
以上是关于错误: - 服务类没有零参数构造函数的主要内容,如果未能解决你的问题,请参考以下文章
InstantiationException:没有零参数构造函数?
Scala 数据集映射失败并出现异常没有为零实际参数找到适用的构造函数/方法
java.lang.Exception:测试类应该只有一个公共零参数构造函数: