Google App Engine DataStore 查询空指针异常和找不到类

2014-10-14 13:25:28     人阅读    

我正在开发一个简单的应用程序将数据保存到谷歌App Engine的数据存储,然后查询数据库列出来所有的实体,

问题来了,虽然能够保存到数据存储中,但是无法进行有效的查询。
在查询的时候报错:

"java.lang.NullPointerException: No API environment is registered for this

问题代码如下:

 thread."Query query = new Query("myStoredBean");


注:在调用代码之前,我验证过数据存储是有效的
另外 logcat中列出了一些乱七八糟的错误


 

10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Unable to resolve superclass of Lcom/google/apphosting/api/DatastorePb$Transaction; (2168)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/apphosting/api/DatastorePb$Transaction;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule E/dalvikvm﹕ Could not find class 'com.google.apphosting.api.DatastorePb$Transaction', referenced from method com.google.appengine.api.datastore.BaseDatastoreServiceImpl.a
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ VFY: unable to resolve new-instance 2597 (Lcom/google/apphosting/api/DatastorePb$Transaction;) in Lcom/google/appengine/api/datastore/BaseDatastoreServiceImpl;
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Unable to resolve superclass of Lcom/google/apphosting/api/DatastorePb$Transaction; (2168)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/apphosting/api/DatastorePb$Transaction;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Unable to resolve superclass of Lcom/google/apphosting/api/DatastorePb$PutRequest; (2168)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/apphosting/api/DatastorePb$PutRequest;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule E/dalvikvm﹕ Could not find class 'com.google.apphosting.api.DatastorePb$PutRequest', referenced from method com.google.appengine.api.datastore.AsyncDatastoreServiceImpl.a
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ VFY: unable to resolve new-instance 2568 (Lcom/google/apphosting/api/DatastorePb$PutRequest;) in Lcom/google/appengine/api/datastore/AsyncDatastoreServiceImpl;
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Unable to resolve superclass of Lcom/google/apphosting/api/DatastorePb$PutRequest; (2168)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/apphosting/api/DatastorePb$PutRequest;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ VFY: unable to find class referenced in signature (Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/appengine/repackaged/com/google/io/protocol/ProtocolMessage;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Unable to resolve superclass of Lcom/google/apphosting/api/DatastorePb$DeleteRequest; (2168)
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule W/dalvikvm﹕ Link of class 'Lcom/google/apphosting/api/DatastorePb$DeleteRequest;' failed
1
10-01 18:11:38.884  32537-32537/com.myApp.MyModule E/dalvikvm﹕ Could not find class 'com.google.apphosting.api.DatastorePb$DeleteRequest', referenced from method com.google.appengine.api.datastore.AsyncDatastoreServiceImpl$1.a


有没有碰见过这些问题的?我用的 android studio是最新的api

还有一些其他错误异常如下:


 

10-03 10:25:17.844  16385-16385/com.myapp.myModule E/AndroidRuntime﹕ FATAL EXCEPTION: main
 
    Process: com.myapp.myModule, PID: 16385
 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myapp.myModule/com.myapp.myModule.MainActivityWithSwipeableTabs}: java.lang.NullPointerException
 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
 
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
 
            at android.os.Handler.dispatchMessage(Handler.java:102)
 
            at android.os.Looper.loop(Looper.java:157)
 
            at android.app.ActivityThread.main(ActivityThread.java:5867)
 
            at java.lang.reflect.Method.invokeNative(Native Method)
 
            at java.lang.reflect.Method.invoke(Method.java:515)
 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
 
            at dalvik.system.NativeStart.main(Native Method)
 
     Caused by: java.lang.NullPointerException
 
            at com.myapp.myModule.MainActivityWithSwipeableTabs.onCreate(SourceFile:66)
 
            at android.app.Activity.performCreate(Activity.java:5312)
 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2541)
�
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
�
            at android.app.ActivityThread.access$800(ActivityThread.java:156)
�
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
�
            at android.os.Handler.dispatchMessage(Handler.java:102)
�
            at android.os.Looper.loop(Looper.java:157)
�
            at android.app.ActivityThread.main(ActivityThread.java:5867)
�
            at java.lang.reflect.Method.invokeNative(Native Method)
�
            at java.lang.reflect.Method.invoke(Method.java:515)
�
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
�
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
�
            at dalvik.system.NativeStart.main(Native Method)

处理方法

最近做个类似的开发,下面是我的查询 datastore 的代码

@Api(name = "myendpoint", namespace = @ApiNamespace(ownerDomain = "domain.com", ownerName =                    "domain.com.com", packagePath = "myPackage"))
 
 public class MyEndpoint {



    /**
 
     * This method lists all the entities inserted in datastore.
 
      * It uses HTTP GET method and paging support.
 
 *
 
 * @return A CollectionResponse class containing the list of all entities
 
 * persisted and a cursor to the next page.
 
 */
@
@SuppressWarnings({ "unchecked", "unused" })
@
@ApiMethod(name = "listStuff")
p
public CollectionResponse listStuff(
 
        @Nullable @Named("cursor") String cursorString,
 
        @Nullable @Named("limit") Integer limit) {



    EntityManager mgr = null;
 
    Cursor cursor = null;
 
    List execute = null;



    try {
 
        mgr = getEntityManager();
 
        Query query = mgr
 
                .createQuery("select from Stuff as Stuff");
 
        if (cursorString != null && cursorString != "") {
 
            cursor = Cursor.fromWebSafeString(cursorString);
 
            query.setHint(JPACursorHelper.CURSOR_HINT, cursor);
 
        }



        if (limit != null) {
 
            query.setFirstResult(0);
 
            query.setMaxResults(limit);
 
        }



        execute = (List) query.getResultList();
 
        cursor = JPACursorHelper.getCursor(execute);
 
        if (cursor != null)
 
            cursorString = cursor.toWebSafeString();



        // Tight loop for fetching all entities from datastore and accomodate
 
        // for lazy fetch.
 
        for (Stuff obj : execute)
 
            ;
 
    } finally {
 
        mgr.close();
 
    }



    return CollectionResponse. builder().setItems(execute)
 
            .setNextPageToken(cursorString).build();
}
}

我是通过 AsyncTask 调用的上面代码,运行没有问题。

yendpoint.Builder endpointBuilder = new Myendpoint.Builder(
 
                AndroidHttp.newCompatibleTransport(), new JacksonFactory(),
 
                null);
 
        endpointBuilder =     CloudEndpointUtils.updateBuilder(endpointBuilder).setApplicationName(MainActivity.APPLICATION_NAME);



        endpoint = endpointBuilder.build();



        try {
 
             result = endpoint.listStuff().execute();
 
        } catch (IOException e) {
 
            // TODO Auto-generated catch block
 
            e.printStackTrace();
 
            // SynchKey = 0;
 
            // result = null;
 
        }



        return result;


原文地址:http://www.itmmd.com/201410/27.html
该文章由 萌萌的IT人 整理发布,转载须标明出处。

com.google.android.gms 报错 DeadObjectException   上一篇
下一篇  android 移动 app和pc 浏览器之间的对等通信
精彩回复
#1楼    ; [评论人]:啊   [发表时间]:2014-12-11 16:45:39

为啥评论不显示呢?

我要追加问题,请求站长解决!
姓名:       

《程序员app》专门为程序员量身定做!