我不知道为什么在这种情况下我会失效

时间:2015-07-16 02:11:53

标签: android database

我在头等舱请求来自Facebook的信息:

        new GraphRequest(AccessToken.getCurrentAccessToken(), "/me/posts", null, HttpMethod.GET,
                new GraphRequest.Callback() {
            public void onCompleted(GraphResponse response) {
                /* handle the result */
                JSONObject json = response.getJSONObject();
                JSONArray arr;
                try {
                    arr = json.getJSONArray("data");

                        for(int i =0;i<4;i++){
                        JSONObject json_obj = arr.getJSONObject(i);

                        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
                        Criteria criteria = new Criteria();
                        String provider = locationManager.getBestProvider(criteria, true);
                        Location location = locationManager.getLastKnownLocation(provider);
                        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, TimeDisplayTimerTask.this);




                        try {
                            if(json_obj!=null){
                                if(location!=null){
                            String postDate = DatabaseConvertMethodsHelper.convertFaceBookDate(
                                json_obj.getString("created_time"));
                            String statusText = json_obj.getString("message");
                            String address = "None";
                            String detailUrl = "http://jv-it.com";
                            float lat = (float)location.getLatitude();
                            float lng = (float)location.getLatitude();
                            String accountId = json_obj.getString("id");
                            SNSPostModel snsPostModel = new SNSPostModel(postDate, statusText, address, 
                                    detailUrl, lat, lng, accountId);
                            snsPostDatabaseHelper.insertSNSPostToDatabase(snsPostModel);
                            Log.e("TEST", accountId);
                            Log.e("TEST", postDate);
                            }
                                }
                        } catch (ParseException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }}




                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }).executeAsync();
        }

我将它添加到第二类中的数据库:

public boolean insertSNSPostToDatabase(SNSPostModel snsPostModel){
        ContentValues values = new ContentValues();
        Log.e("TEST", ""+snsPostModel.getPostDate());
        values.put("post_date", snsPostModel.getPostDate());
        values.put("status_text", snsPostModel.getStatusText());
        values.put("address", snsPostModel.getAddress());
        values.put("detail_url", snsPostModel.getDetailUrl());
        values.put("lat", snsPostModel.getLat());
        values.put("lng", snsPostModel.getLng());
        values.put("account_id", snsPostModel.getAccountId());
        myDataBase.insert("sns_posts", null, values);
        return true;        
    }

但我不知道为什么会出错:

07-16 02:01:06.127: E/SQLiteDatabase(12020): Error inserting status_text=null detail_url=null lng=0.0 account_id=null address=null lat=0.0 post_date=null
07-16 02:01:06.127: E/SQLiteDatabase(12020): android.database.sqlite.SQLiteConstraintException: sns_posts.account_id may not be NULL (code 19)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(Native Method)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:775)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1469)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1339)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at com.itmo.lifelog_2015.data.remote.SNSPostDatabaseHelper.insertSNSPostToDatabase(SNSPostDatabaseHelper.java:61)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at com.itmo.lifelog_2015.service.LoginFacebook$TimeDisplayTimerTask$1$1.onCompleted(LoginFacebook.java:135)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at com.facebook.GraphRequest$5.run(GraphRequest.java:1243)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.os.Handler.handleCallback(Handler.java:615)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.os.Looper.loop(Looper.java:137)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at android.app.ActivityThread.main(ActivityThread.java:4745)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at java.lang.reflect.Method.invokeNative(Native Method)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at java.lang.reflect.Method.invoke(Method.java:511)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-16 02:01:06.127: E/SQLiteDatabase(12020):    at dalvik.system.NativeStart.main(Native Method)
07-16 02:01:06.127: E/TEST(12020): 929043207145744_934981236551941
07-16 02:01:06.127: E/TEST(12020): 2015-07-16
07-16 02:01:06.131: E/TEST(12020): null

如您所见,我可以在第一堂课中获得ID和日期

07-16 02:01:06.127: E/TEST(12020): 929043207145744_934981236551941
07-16 02:01:06.127: E/TEST(12020): 2015-07-16

但是我在第二课中得到零,请告诉我出了什么问题?

修改

public class SNSPostModel {
    private String mPostDate;
    private String mStatusText;
    private String mAddress;
    private String mDetailUrl;
    private float mLat;
    private float mLng;
    private String mAccountId;

    public SNSPostModel(){

    }
    public SNSPostModel(String mPostDate,String mStatusText,String mAddress,String mDetailUrl,
            float mLat,float mLng,String mAccountId){

    }

1 个答案:

答案 0 :(得分:0)

您缺少构造函数中的初始化

public SNSPostModel(String mPostDate,String mStatusText,String mAddress,String mDetailUrl,
            float mLat,float mLng,String mAccountId){

    // initialize like this 
    this. mAccountId= mAccountId;
    .....
    }

请参阅此how to initialize variable in constructor

的链接