在android中的oncreate方法中插入while循环

时间:2016-08-23 13:40:53

标签: android multithreading loops while-loop

我正在尝试在我的onCreate方法中创建一个while循环,但它一直崩溃(没有循环很好)。有人可以帮帮我吗?

这是代码:

            while(runnersNearby.size()<3)
{
            GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(lat, lang), radius);
            geoQuery.addGeoQueryEventListener(new GeoQueryEventListener() {
                @Override
                public void onKeyEntered(String key, GeoLocation location) {
                    Toast.makeText(UserProfile.this, "Successfully Found"+key, Toast.LENGTH_SHORT).show();
                    if (key != user.getUid())
                        runnersNearby.add(key);
                    Log.d("Number of users", String.valueOf(runnersNearby.size()));
                    Log.d("KEY", String.valueOf(key));
                }

                @Override
                public void onKeyExited(String key) {
                    Toast.makeText(UserProfile.this, "left the place", Toast.LENGTH_SHORT).show();
                    runnersNearby.remove(key);
                }

                @Override
                public void onKeyMoved(String key, GeoLocation location) {
                    Toast.makeText(UserProfile.this, "key moved but here", Toast.LENGTH_SHORT).show();
                }

                @Override
                public void onGeoQueryReady() {
                    System.out.println("All initial data has been loaded and events have been fired!");
                }

                @Override
                public void onGeoQueryError(DatabaseError error) {
                    Toast.makeText(UserProfile.this, "Error Occured", Toast.LENGTH_SHORT).show();
                }
            });
radius=radius*1.5;
        }

修复之后的logcat:

08-23 16:53:37.979 10116-10116/com.firebase.shahaf.datasignin E/AndroidRuntime: FATAL EXCEPTION: main
                                                                                Process: com.firebase.shahaf.datasignin, PID: 10116
                                                                                java.lang.RuntimeException: Unable to start activity ComponentInfo{com.firebase.shahaf.datasignin/com.firebase.shahaf.datasignin.UserProfile}: java.lang.IllegalArgumentException: Precision of GeoHash must be larger than zero!
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365)
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:148)
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283)
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                    at android.os.Looper.loop(Looper.java:135)
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5272)
                                                                                    at java.lang.reflect.Method.invoke(Native Method)
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372)
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
                                                                                 Caused by: java.lang.IllegalArgumentException: Precision of GeoHash must be larger than zero!
                                                                                    at com.firebase.geofire.core.GeoHash.<init>(GeoHash.java:29)
                                                                                    at com.firebase.geofire.core.GeoHashQuery.queriesAtLocation(GeoHashQuery.java:85)
                                                                                    at com.firebase.geofire.GeoQuery.setupQueries(GeoQuery.java:224)
                                                                                    at com.firebase.geofire.GeoQuery.addGeoQueryEventListener(GeoQuery.java:325)
                                                                                    at com.firebase.shahaf.datasignin.UserProfile.onCreate(UserProfile.java:68)
                                                                                    at android.app.Activity.performCreate(Activity.java:5977)
                                                                                    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                                                                                    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2258)
                                                                                    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2365) 
                                                                                    at android.app.ActivityThread.access$800(ActivityThread.java:148) 
                                                                                    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1283) 
                                                                                    at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                    at android.os.Looper.loop(Looper.java:135) 
                                                                                    at android.app.ActivityThread.main(ActivityThread.java:5272) 
                                                                                    at java.lang.reflect.Method.invoke(Native Method) 
                                                                                    at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909) 
                                                                                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704) 

2 个答案:

答案 0 :(得分:4)

无限循环。你在循环时(runnersNearby.size()&lt; 3)。但是阵列的大小永远不会改变。所以你永远循环。因为你的onCreate永远不会返回,系统最终会让你花费太长时间来创建你的活动,因为它假设出现了问题。

我假设您想要做的是遍历runnersNearby数组?

代码中有一个地方runnersNearby可以更改,但它在异步回调中。所以它永远不会被调用。直到很久以后。

答案 1 :(得分:0)

将所有Internet /网络相关的内容放在AsyncTasks doInBackground中,并在OnPostExecute(...)中添加EventListener。不只是在onCreate中启动Asynctask,让你的应用程序等待EventListeners启动。

编辑:快速而又脏的示例代码:

public class yourClass probablyExtends Activity{
    onCreate(...){
        new LoadRunners().execute();
        NothingToSeeHereMoveAlongAndFollowYourUsualBusiness();
    }
}

public class LoadRunners extends AsyncTask {

    doInBackground(..){
        double radius = 1.0;
        while(runnersNearby.size()<3){
            loadRunners(radius);
            if(runnersNearby.size()<3)
                radius*=1.5;
        }
   }
   onPostExecute(...){
       addEventListeners();
   }
}