1

不清楚为什么会发生错误,是由于 AsyncTask 的使用不正确?

我现在正在开发一个 android 日历应用程序。我想要同步功能,允许用户将他们的数据同步到服务器,反之亦然。
当用户打开应用程序时,它会从服务器获取数据并将新数据更新到 android 设备上的 sqlite 数据库。
并且每当用户创建新数据时,它都会将此数据发送到服务器以更新到服务器上的数据库。
我就这样实现了我的理想。
1. MainActivity 类做同步动作

public class MainActivity extends Activity {
    DataSyncTask syncTask;
    public void onCreate(Bundle savedInstanceState){
        Button insertBt = (Button) findViewById(R.id.bt_insert);

        // get data1
        Data1 data1;

        syncTask = new DataSyncTask(this);
        syncTask.syncOnStart(data1);

        insertBt.setOnClickListener(new OnClickListener(){
            public void onClick(View v){
                Data2 data2;
                // get data2

                syncTask.syncOnInsert(data2);
            }
        });
    }
}

2.DataSyncTask收集asyncTask

public class DataSyncTask {
    Context mContext;
    public DataSyncTask(Context context){
        mContext = context;
    }

    private class SyncWhenStartTask extends AsyncTask<Data1, Integer, String>{
        protected String doInBackground(Data1... data1s){
            Object obj1 = ServerUtilities.sendDataForStep1(data1s[0]);

            Data2 data2;
            // Process Object

            Object obj2 = ServerUtilities.sendDataToInsert(data2);
            return "Success";
        }
    }

    private class SyncWhenInsertTask  extends AsyncTask<Data2, Integer, String>{
        protected String doInBackground(Data2... datas){            
            Object obj2 = ServerUtilities.sendDataToInsert(datas[0]);

            return "Success";
        }
    }

    public void syncOnStart(Data1 data1){
        new SyncWhenStartTask().execute(data1);
    }

    public void syncOnInsert(Data2 data2){
        new SyncWhenInsertTask().execute(data2);
    }
}

3.ServerUtilities 类

public final class ServerUtilities {
    static final String SERVER_URL = "http://172.2.100.99:8080";

    public static String sendDataForStep1(Data1 data1) {
        String url = SERVER_URL + "/sendDataForStep1";
        ServerUtil<Data1, String> server = ServerUtil<Data1, String>();
        return server.post(url, data1, String.class);
    }

    public static String sendDataToInsert(Data2 data2) {
        String url = SERVER_URL + "/sendDataToInsert";
        ServerUtil<Data2, String> server = ServerUtil<Data2, String>();
        return server.post(url, data2, String.class);
    }

    private static class ServerUtil<T1, T2> {
        public T2 post(String url, T1 obj, Class<T2> returnType) {
            HttpHeaders requestHeaders = new HttpHeaders();

            // Sending a JSON or XML object i.e. "application/json" or
            // "application/xml"
            requestHeaders.setContentType(MediaType.APPLICATION_JSON);

            // Populate the Message object to serialize and headers in an
            // HttpEntity object to use for the request
            try {
                HttpEntity<T1> requestEntity = new HttpEntity<T1>(
                        object, requestHeaders);

                // Create a new RestTemplate instance
                RestTemplate restTemplate = new RestTemplate(true);

                // Make the network request, posting the message, expecting
                // a list of SyncData in response from the server
                ResponseEntity<T2> response = restTemplate
                        .exchange(url, HttpMethod.POST, requestEntity, returnType);

                if(response != null) {
                    return response.getBody();
                }
            } catch (Exception e) {
                Log.e(TAG + "-Exception", e.getMessage());  // Line 345 in logcat
            }
            return null;
        }
    }
}

当我启动 MainActivity 时,它连接到服务器,获取数据,更新到 sqlite 数据库并将新数据从设备发送到服务器以更新成功,一切正常。但是当我创建一个新数据时,当执行syncTask.syncOnInsert(),它得到异常:

11-30 14:38:25.849: W/dalvikvm(14140): threadid=12: thread exiting with uncaught exception (group=0x40a13300)
11-30 14:38:25.869: E/AndroidRuntime(14140): FATAL EXCEPTION: AsyncTask #2
11-30 14:38:25.869: E/AndroidRuntime(14140): java.lang.RuntimeException: An error occured while executing doInBackground()
11-30 14:38:25.869: E/AndroidRuntime(14140):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.lang.Thread.run(Thread.java:856)
11-30 14:38:25.869: E/AndroidRuntime(14140): Caused by: java.lang.NullPointerException: println needs a message
11-30 14:38:25.869: E/AndroidRuntime(14140):    at android.util.Log.println_native(Native Method)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at android.util.Log.e(Log.java:231)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at vn.com.bip.office.smart.form.ServerUtilities$ServerUtil.post(ServerUtilities.java:345)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at vn.com.bip.office.smart.form.ServerUtilities.sendSyncData(ServerUtilities.java:283)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at vn.com.bip.office.smart.util.DataSyncTask$SyncWhenChangeOnScheduleTask.doInBackground(DataSyncTask.java:158)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at vn.com.bip.office.smart.util.DataSyncTask$SyncWhenChangeOnScheduleTask.doInBackground(DataSyncTask.java:1)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
11-30 14:38:25.869: E/AndroidRuntime(14140):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
11-30 14:38:25.869: E/AndroidRuntime(14140):    ... 5 more

谁能解释我为什么会发生这种情况并教我如何解决这个错误。
提前致谢!

4

1 回答 1

4
Log.e(TAG + "-Exception", e.getMessage()); 

e.getMessage()并不总是返回有效的字符串。有时它会返回null。这就是你的应用崩溃的原因

于 2012-11-30T10:02:52.673 回答