SwipeRefreshLayout和AsyncHttpClient不在第一个onCreate上工作

时间:2014-05-04 17:11:58

标签: android listview asynchttpclient swiperefreshlayout

我正在使用SwipeRefreshLayout来远程获取数据,我使用AsyncHttpClient来处理网络方面的事情。

除了一个场景之外,一切都按预期工作:第一次onCreate启动应用程序。

主要活动:

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();
    }

onRefresh:

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

进行网络提升的getGames:

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());
            }
        });
    }

因此,当我从我的IDE启动应用时,onCreate会调用getGames()函数,并会记录它进入,但网络MyRestClient.get("games", null, new JsonHttpResponseHandler() {不会被调用因为它没有获取数据,也从不登录成功或失败。

现在,如果我刷卡以刷新它的工作原理并成功填充我的列表视图,其中包含已获取的数据和日志,它位于onSuccess中。

如果我通过后退按钮关闭应用并重新打开它可以正常工作并刷新我的列表视图。

因此,当从我的IDE启动到模拟器时,它不会仅在第一次onCreate调用上工作 - 但是在第一次初始onCreate之后,所有功能都无法正常工作。

我不确定我在这里失踪了什么,感谢任何帮助,谢谢。

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

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

修改

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");
    }
}

编辑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

当应用程序启动时,这是我日志中唯一看起来不寻常的部分。

2 个答案:

答案 0 :(得分:0)

你的意思是,onCreate它没有调用getGames()? 您是否尝试将getGames()放入onResume()?

答案 1 :(得分:0)

经过进一步测试后,我注意到它只发生在模拟器上。

启动真实设备时,应用会按预期响应。在这一点上,我将其归结为与模拟器相关的问题,但如果出现任何具体问题,将保持此线程更新。