引起:java.lang.InstantiationException:类com.bustracker.PostData没有零参数构造函数

时间:2015-04-23 11:00:37

标签: android

我想每隔60秒定期将带有HttpURLConnection api的JSON字符串发布到localhost服务器(WAMP),以便插入数据库。为了实现这一点,我实现了从onStartCommand方法调用的Asynctask,但是我收到了这个错误:Caused by: java.lang.NoSuchMethodException: <init> []Caused by: java.lang.InstantiationException: class com.bustracker.PostData has no zero argument constructor如何将数据“JSON string”传递给PostData的构造函数?

Logcat错误:

04-23 12:52:24.647: E/AndroidRuntime(19309): Process: com.bustracker, PID: 19309
04-23 12:52:24.647: E/AndroidRuntime(19309): java.lang.RuntimeException: Unable to instantiate service com.bustracker.PostData: java.lang.InstantiationException: class com.bustracker.PostData has no zero argument constructor
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3135)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.app.ActivityThread.access$1900(ActivityThread.java:177)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.os.Looper.loop(Looper.java:145)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.app.ActivityThread.main(ActivityThread.java:5944)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.reflect.Method.invoke(Native Method)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.reflect.Method.invoke(Method.java:372)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1389)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1184)
04-23 12:52:24.647: E/AndroidRuntime(19309): Caused by: java.lang.InstantiationException: class com.bustracker.PostData has no zero argument constructor
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.Class.newInstance(Class.java:1641)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at android.app.ActivityThread.handleCreateService(ActivityThread.java:3132)
04-23 12:52:24.647: E/AndroidRuntime(19309):    ... 9 more
04-23 12:52:24.647: E/AndroidRuntime(19309): Caused by: java.lang.NoSuchMethodException: <init> []
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.Class.getConstructor(Class.java:531)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.Class.getDeclaredConstructor(Class.java:510)
04-23 12:52:24.647: E/AndroidRuntime(19309):    at java.lang.Class.newInstance(Class.java:1639)
04-23 12:52:24.647: E/AndroidRuntime(19309):    ... 10 more

这部分代码是从MainActivity类的内部类“MyLocationListerner”中的onLocationChanged方法调用的:

String jSONString = convertToJSON(pLong, pLat, formatted);
PostData sender = new PostData(jSONString);             
Intent intent3 = new Intent(MainActivity.this, PostData.class);
PendingIntent pintent3 = PendingIntent.getService(getApplicationContext(), 0, intent3, 0);
AlarmManager alarm3 = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
Calendar cal = Calendar.getInstance();
// for 30 mint 60*60*1000
alarm3.setRepeating(AlarmManager.RTC_WAKEUP, cal.getTimeInMillis(),
    1000, pintent3);

startService(new Intent(getBaseContext(), PostData.class));

PostData类:

package com.bustracker;

import java.io.DataOutputStream;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

import android.app.IntentService;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Handler;

public class PostData extends IntentService {
    String jSONString;
    Handler handler = new Handler();

    public PostData(String jSONString) {
        super("some");
        this.jSONString = jSONString;
    }

    public String getjSONString() {
        return jSONString;
    }

    public void setjSONString(String jSONString) {
        this.jSONString = jSONString;
    }


    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        new MyAsyncTask().execute(jSONString);

        return super.onStartCommand(intent, flags, startId);
    }
    @Override
    protected void onHandleIntent(Intent intent) {
        // TODO Auto-generated method stub

    }

    class MyAsyncTask extends AsyncTask<String, Integer, Void> {

        @Override
        protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub

            try {
                //This is the ip address of my laptop wifi because I am running the app in my device and I want to send the data to the localhost server(WAMP). 
                URL myUrl = new URL("http://192.168.182.15/webservice"); 
                HttpURLConnection myConnection = (HttpURLConnection) myUrl
                        .openConnection();
                myConnection.setRequestMethod("POST");
                myConnection.setDoOutput(true);
                myConnection.setUseCaches(false);
                myConnection.setConnectTimeout(10000);
                myConnection.setReadTimeout(10000);
                myConnection.setRequestProperty("Content-Type",
                        "application/json");
                myConnection.connect();
                // create data output stream
                DataOutputStream wr = new DataOutputStream(
                        myConnection.getOutputStream());
                // write to the output stream from the string
                wr.writeBytes(jSONString);
                wr.close();
            } catch (IOException e) {

                e.printStackTrace();
            }
            return null;

        }

    }
}

清单部分:

<service android:name=".PostData" />

1 个答案:

答案 0 :(得分:2)

错误信息不言自明:

class com.bustracker.PostData has no zero argument constructor

意味着您需要添加这样的构造函数,甚至是虚拟构造函数:

public PostData {}