سؤال

I am using SwipeRefreshLayout to fetch data remotely which I use AsyncHttpClient to take care of the network side of things.

Everything works as expected except for one scenario: the first onCreate launch of the app.

The main activity:

public class MainActivity extends Activity implements AdapterView.OnItemClickListener, SwipeRefreshLayout.OnRefreshListener{

    ListView mainListView;
    JSONMainAdapter mJSONAdapter;
    SwipeRefreshLayout swipeLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        swipeLayout = (SwipeRefreshLayout) findViewById(R.id.main_swipe_container);
        swipeLayout.setOnRefreshListener(this);
        swipeLayout.setColorScheme(android.R.color.holo_blue_bright,
                android.R.color.holo_green_light,
                android.R.color.holo_orange_light,
                android.R.color.holo_red_light);

        mainListView = (ListView) findViewById(R.id.main_listview);
        mainListView.setOnItemClickListener(this);

        mJSONAdapter = new JSONMainAdapter(this, getLayoutInflater());
        mainListView.setAdapter(mJSONAdapter);

        getGames();
    }

The onRefresh:

@Override 
public void onRefresh() {
    getGames();
}

The getGames which does the network lifting:

private void getGames() {

        swipeLayout.setRefreshing(true);
        //gets logged on first onCreate load
        Log.d("DEBUG", "IN GET GAMES");

        MyRestClient.get("games", null, new JsonHttpResponseHandler() {
            @Override
            public void onSuccess(JSONObject jsonObject) {
                //does NOT get logged on first onCreate
                //but does get logged on refresh and other onCreate calls 
                Log.d("DEBUG", "IN GET GAMES ON SUCCESS");
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Success!", Toast.LENGTH_LONG).show();
                mJSONAdapter.updateData(jsonObject.optJSONArray("games"));
            }

            @Override
            public void onFailure(int statusCode, Throwable throwable, JSONObject error) {
                Log.d("DEBUG", "IN GET GAMES ON FAIL");
                swipeLayout.setRefreshing(false);
                Toast.makeText(getApplicationContext(), "Error: " + statusCode + " " + throwable.getMessage(), Toast.LENGTH_LONG).show();
                Log.e("ERROR", statusCode + " " + throwable.getMessage());
            }
        });
    }

So when I launch the app from my IDE the onCreate calls the getGames() function and it logs that it gets in there, but the network MyRestClient.get("games", null, new JsonHttpResponseHandler() { doesn't get called as it doesn't fetch the data and never logs onSuccess or onFailure.

Right now if I swipe to refresh it works and succesfully populates my listview with fetched data and logs it was in onSuccess.

If I close the app via back button and re-open it works properly and refreshes my listview.

So it doesn't work only on the first onCreate call when launched from my IDE to the emulator -- but after that first initial onCreate doesn't work everything functions properly.

I am not sure what I'm missing here, any help is appreciated, thanks.

SwipeRefreshLayout: https://developer.android.com/reference/android/support/v4/widget/SwipeRefreshLayout.html

AsyncHttpClient: http://loopj.com/android-async-http/

EDIT:

public class MyRestClient {
    private static final String BASE_URL = "http://10.10.1.10:3000/api/";

    private static AsyncHttpClient client = new AsyncHttpClient();

    public static void get(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        addAuthHeaders();
        client.get(getAbsoluteUrl(url), params, responseHandler);
    }

    public static void post(String url, RequestParams params, AsyncHttpResponseHandler responseHandler) {
        addAuthHeaders();
        client.post(getAbsoluteUrl(url), params, responseHandler);
    }

    private static String getAbsoluteUrl(String relativeUrl) {
        return BASE_URL + relativeUrl;
    }

    public static void addAuthHeaders(){
        client.addHeader("blah", "xxx");
        client.addHeader("blah", "xxx");
    }
}

EDIT 2:

> 05-05 08:51:20.477: E/memtrack(1696): Couldn't load memtrack module
> (No such file or directory) 05-05 08:51:20.477:
> E/android.os.Debug(1696): failed to load memtrack module: -2 05-05
> 08:51:20.857: D/AndroidRuntime(1696): Calling main entry
> com.android.commands.am.Am 05-05 08:51:20.937: I/ActivityManager(380):
> START u0 {act=android.intent.action.MAIN
> cat=[android.intent.category.LAUNCHER] flg=0x10000000
> cmp=com.example.myapp.app/.MainActivity} from pid 1696 05-05
> 08:51:21.047: D/gralloc(49): Registering a buffer in the process that
> created it. This may cause memory ordering problems. 05-05
> 08:51:21.047: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/SurfaceFlinger(49): glCheckFramebufferStatusOES error
> 1620973071 05-05 08:51:21.057: E/SurfaceFlinger(49): got
> GL_FRAMEBUFFER_COMPLETE_OES error while taking screenshot 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.057: E/libEGL(49): called unimplemented OpenGL ES API 05-05
> 08:51:21.067: W/WindowManager(380): Screenshot failure taking
> screenshot for (328x546) to layer 21005

Thats the only part that looks out of the ordinary in my log when the app launches.

هل كانت مفيدة؟

المحلول 2

After further testing, I noticed it only happens on the emulator.

When launching to a real device the app responds as expected. At this point I'm chalking it up to an emulator related issue, but will keep this thread updated if anything concrete develops.

نصائح أخرى

Do you mean, onCreate it doesn’t call getGames() ? Did you try to put getGames() into onResume() ?

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top