0

以下是我的课程:

StatsObjectId.java

public class StatsObjectId  extends Activity {
    DBClass db;
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    db = new DBClass(this);
    }
    public void addObjId(String objid){
    Log.e("objectid","This is the object id going to store: "+objid);


            db.addObjectId(objid);  //This is the line# 105


            if(getObjId()){
        Log.e("objectid","Successfully stored!");
    }else{
        Log.e("objectid","Error in storing object id!");
        }
    }
    public boolean getObjId(){
    boolean result;
    try{
    c = db.getObjectId();
    c.moveToFirst();
    String str = c.getString(c.getColumnIndex("objectid"));
    Log.e("objectid","Object id returned form DB: "+str);
    result = true;
    }catch(CursorIndexOutOfBoundsException e){
        Log.e("objectid","Cursor index out of bound");
        result = false;
        e.printStackTrace();
    }catch(Exception e){
        Log.e("objectid","Some Another Exception");
        result = false;
        e.printStackTrace();
    }
    return result;
}

ParseComServerAccessor.java

public class ParseComServerAccessor {
//I am skipping some irrelevant code
     public void putStats(String authtoken, String userId, Tas statsToAdd) throws Exception {
     //Again skip some code
     //Here I got some HttpResponse and I need to extract an object id and save it to database
     HttpResponse response = httpClient.execute(httpPost);
     String responseString = EntityUtils.toString(response.getEntity());
     JSONObject json = new JSONObject(responseString);
     Log.e("objectid","Now Object Id is: "+json.getString("objectId") );
     StatsObjectId ob = new StatsObjectId();


     ob.addObjId(json.getString("objectId")); // This is the line#156
     //skip some code
     }
}

TasSyncAdapter.java

public class TasSyncAdapter extends AbstractThreadedSyncAdapter {
    //skipped Constructor code
    public void onPerformSync(Account account, Bundle extras, String authority,
    ContentProviderClient provider, SyncResult syncResult) {
    //skipped some code
    ParseComServerAccessor parseComService = new ParseComServerAccessor();
    //skipped some code again


    parseComService.putStats(authToken, userObjectId, remoteTas); //This is the line# 134
    //skip some code
    }
}

现在终于当我运行我的应用程序时......这是以下日志猫

Tag                    Text

objectid               This is the object id going to store: 9AFysqffz7
System.err             java.lang.NullPointerException
System.err             at com.myapp.ds_app.StatsObjectId.addObjId(StatsObjectId.java:105)
System.err             at com.myapp.ds_app.syncadapter.ParseComServerAccessor.putStats(ParseComServerAccessor.java:156)
System.err             at com.myapp.ds_app.syncadapter.TasSyncAdapter.onPerformSync(TasSyncAdapter.java:134)
System.err             at android.content.AbstractThreadedSyncAdapter$SyncThread.run(AbstractThreadedSyncAdapter.java:254)

DBClass.java

public class DBClass extends SQLiteOpenHelper { 
private static final String DATABASE_NAME="myapp.db";
public DBClass(Context cxt){
    super(cxt, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase mydatabase) {
    mydatabase.execSQL("CREATE TABLE IF NOT EXISTS temp (objectid STRING)");
}
public Cursor getObjectId(){
    Cursor cursor = getReadableDatabase().rawQuery("SELECT objectid FROM temp", null);
    return cursor;
}
public void addObjectId(String objid){
    try{
    ContentValues cv = new ContentValues(1);

    Log.e("objectid","In DBClass and object id: "+objid);
    cv.put("objectid", objid);
    Log.e("objectid","Content value contains: "+cv.toString());
    getWritableDatabase().insert("temp", "objectid", cv);

    }catch(NullPointerException e){
        e.printStackTrace();
    }
}

}

现在,我被困在这一点上!到目前为止,我只需要保存一个值。我试图创建一个文件而不是在数据库中保存一个值。但是 ContextWrapper 也有一些例外。

我目前有兴趣处理数据库。

如果你们需要任何其他信息,请告诉我。

如果有人请解释这件事,我将不胜感激。我是android新手,很想了解这个问题。提前致谢!

4

1 回答 1

2
StatsObjectId ob = new StatsObjectId();

您正在实例化一个 Activity 类。你不能这样做。(当你这样做时,Android 中应该有一些东西会告诉你)基本上,上下文没有被初始化,因为 android 需要这样做才能拥有一个功能性的 Activity。

另外,Android(当它创建 Activity 时)使用适当的上下文调用 onCreate 方法。你没有(你也不能),因此你的数据库是空的。

在 AbstractThreadedSyncAdapter 中,您有一个getContext方法来获取正确的上下文。使用它来初始化您的数据库并在其中插入数据,而不是将其传递给 Activity 对象。

于 2013-10-25T19:08:02.673 回答