我在智能手机上运行应用程序时出错,但正确如果我在Android Studio上运行该应用程序

时间:2015-11-30 14:43:21

标签: java android json drawer rushorm

当我在Android Studio中运行我的应用时,一切都正常运行。通过运行RushSearch()查询正确填充抽屉中的ListView。 但是当我创建了apk并且我在我的智能手机上安装以尝试运行应用程序时,第一次运行时Drawer中的ListView没有填充。如果我关闭应用程序并再次运行它,一切都是正确的。

为什么会出现这种异常?

MainActivity.java类上的onCreate方法的代码是这个

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

    callWebservice();

    // Layout principale col drawer
    setContentView(R.layout.activity_main_drawer);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    leftArrow = (ImageView) findViewById(R.id.left_arrow);
    titolo = (TextView) findViewById(R.id.titolo_id);

    ActionBarDrawerToggle actionBarDrawerToggle =
            new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                    R.string.navigation_drawer_open, R.string.navigation_drawer_close);

    drawerLayout.setDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();

    drawerListView = (ListView) findViewById(R.id.drawer_listView);

    // SELECT * FROM Sections where Fk == 0;
    sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
    Log.d("ELEMENTI",""+sectionsList.size());
    drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, getApplicationContext());

    // Adapter che si occupa di popolare il drawer
    drawerListView.setAdapter(drawerListViewAdapter);


    // Listner per il click su un elemento del drawer
    drawerListView.setOnItemClickListener(click);
}

方法callWebservice()负责从url获取JSON文件,其代码是这个

private void callWebservice() {

    String tag_json_obj = "GetItem";
    String url = String.format("http://digitalbox.getsandbox.com/item");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                saveSections(response.getJSONArray("Sections"));

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

        }
    }, new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Settings", "Error: " + error.getMessage());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Accept", "application/json");
            return headers;
        }
    };
    NetworkController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);
}

private void saveSections(final JSONArray section) {

    Thread t = new Thread(new Runnable() {
        @Override
        public void run() {


            int c = section.length();
            for (int i = 0; i < c; i++) {

                try {
                    JSONObject obj = section.getJSONObject(i);

                    Sections s = new RushSearch().whereEqual("ID", obj.getInt("ID")).findSingle(Sections.class);
                    if (s == null) {
                        s = new Sections();
                        s.setID(obj.getInt("ID"));
                    }

                    if (!obj.isNull("Fk"))
                        s.setFk(obj.getInt("Fk"));

                    s.setTitle(obj.getString("Title"));

                    if (!obj.isNull("CategoryColor"))
                        s.setCategoryColor(obj.getString("CategoryColor"));

                    s.setPos(obj.getInt("Pos"));
                    s.setLanguageId(obj.getInt("LanguageId"));
                    s.setUnavailableForSend(obj.getBoolean("UnavailableForSend"));

                    s.save();

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

            Log.d("sections", "DB updated");


        }
    });
    t.setPriority(Thread.MIN_PRIORITY);
    t.start();
}

如果我使用Android Studio运行应用程序,在AVD智能手机中,我有此日志

    11-30 15:21:53.520 2170-2170/? I/art: Not late-enabling -Xcheck:jni (already on)
11-30 15:21:53.520 2170-2170/? I/art: Late-enabling JIT
11-30 15:21:53.558 2170-2170/? I/art: JIT created with code_cache_capacity=2MB compile_threshold=1000
11-30 15:21:53.685 2170-2170/rawpepper.it.digitalbox W/System: ClassLoader referenced unknown path: /data/app/rawpepper.it.digitalbox-2/lib/x86
11-30 15:21:54.099 2170-2170/rawpepper.it.digitalbox W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
11-30 15:21:54.469 2170-2170/rawpepper.it.digitalbox D/ELEMENT: 13
11-30 15:21:54.486 2170-2210/rawpepper.it.digitalbox D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-30 15:21:54.592 2170-2210/rawpepper.it.digitalbox I/OpenGLRenderer: Initialized EGL, version 1.4
11-30 15:21:54.719 2170-2210/rawpepper.it.digitalbox W/EGL_emulation: eglSurfaceAttrib not implemented
11-30 15:21:54.719 2170-2210/rawpepper.it.digitalbox W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xad6fc2e0, error=EGL_SUCCESS
11-30 15:21:57.542 2170-2204/rawpepper.it.digitalbox D/Volley: [117] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://digitalbox.getsandbox.com/item 0x95b78f5f NORMAL 1> [lifetime=3034], [size=196862], [rc=200], [retryCount=0]
11-30 15:21:57.691 2170-2180/rawpepper.it.digitalbox W/art: Suspending all threads took: 25.964ms
11-30 15:21:57.909 2170-2405/rawpepper.it.digitalbox D/sections: DB updated

告诉我sectionList数组中的元素数是13(第六行),这是正确的。 app run with Android Studio 如果我创建apk并将其安装在我的智能手机上,我将其插入PC以检查日志,当我运行应用程序时,我有这个日志

    11-30 15:35:01.765 17183-17183/rawpepper.it.digitalbox I/art: Late-enabling -Xcheck:jni
11-30 15:35:01.883 17183-17183/rawpepper.it.digitalbox W/System: ClassLoader referenced unknown path: /data/app/rawpepper.it.digitalbox-2/lib/arm
11-30 15:35:02.357 17183-17183/rawpepper.it.digitalbox W/art: Before Android 4.1, method int android.support.v7.internal.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView
11-30 15:35:03.235 17183-17183/rawpepper.it.digitalbox D/ELEMENT: 0
11-30 15:35:03.249 17183-17260/rawpepper.it.digitalbox D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
11-30 15:35:03.304 17183-17260/rawpepper.it.digitalbox I/Adreno-EGL: <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 09/02/15, 76f806e, Ibddc658e36
11-30 15:35:03.306 17183-17260/rawpepper.it.digitalbox I/OpenGLRenderer: Initialized EGL, version 1.4
11-30 15:35:05.976 17183-17321/rawpepper.it.digitalbox D/sections: DB updated

告诉我sectionList数组中的元素是0,但如果我关闭并再次运行它,一切都是正确的。

4 个答案:

答案 0 :(得分:0)

您需要在下载数据后设置抽屉。例如,在onCreate()中下载新节和setUpDraw()时调用saveDate()。

private void saveData(Sections newSections) {
    newSections.save()
    setUpDraw()
}

private void setUpDraw() {

    drawerListView = (ListView) findViewById(R.id.drawer_listView);
    sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
    drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, context); // You don't want application context
    drawerListView.setAdapter(drawerListViewAdapter);
    drawerListView.setOnItemClickListener(click);
}

答案 1 :(得分:0)

我认为您应该将这部分内容称为代码:

Warning: React.createElement: type should not be null, undefined, boolean, or number. It should be a string (for DOM elements) or a ReactClass (for composite components). Check the render method of `exports`. react.js:20250
  warning @ react.js:20728
  ReactElementValidator.createElement @ react.js:9853
  t.exports.React.createClass.render @ bundle.js:1ReactCompositeComponentMixin._renderValidatedComponentWithoutOwnerOrContext @ react.js:6330
  ReactCompositeComponentMixin._renderValidatedComponent @ react.js:6350
  wrapper @ react.js:12868
  ReactCompositeComponentMixin._updateRenderedComponent @ react.js:6303
  ReactCompositeComponentMixin._performComponentUpdate @ react.js:6287
  ReactCompositeComponentMixin.updateComponent @ react.js:6216
  wrapper @ react.js:12868
  ReactCompositeComponentMixin.performUpdateIfNecessary @ react.js:6164
  ReactReconciler.performUpdateIfNecessary @ react.js:13667
  runBatchedUpdates @ react.js:15356
  Mixin.perform @ react.js:17245
  Mixin.perform @ react.js:17245
  assign.perform @ react.js:15313
  flushBatchedUpdates @ react.js:15374
  wrapper @ react.js:12868
  Mixin.closeAll @ react.js:17311
  Mixin.perform @ react.js:17258
  ReactDefaultBatchingStrategy.batchedUpdates @ react.js:8842
  batchedUpdates @ react.js:15321
  ReactEventListener.dispatchEvent @ react.js:10336

Uncaught Error: Invariant Violation: Element type is invalid: expected a string (for built-in components) or a class/function (for composite components) but got: object. Check the render method of `exports`.

saveSections()方法完成后。像这样:

var List = require("components/Forms/productList/view.jsx");

}

答案 2 :(得分:0)

我修改了我的代码,现在我有了onCreate()方法的代码

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

        callWebservice();

//        setContentView(R.layout.activity_main);

        // Layout principale col drawer
        setContentView(R.layout.activity_main_drawer);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        leftArrow = (ImageView) findViewById(R.id.left_arrow);
        titolo = (TextView) findViewById(R.id.titolo_id);

        ActionBarDrawerToggle actionBarDrawerToggle =
                new ActionBarDrawerToggle(this, drawerLayout, toolbar,
                        R.string.navigation_drawer_open, R.string.navigation_drawer_close);

        drawerLayout.setDrawerListener(actionBarDrawerToggle);
        actionBarDrawerToggle.syncState();

        drawerListView = (ListView) findViewById(R.id.drawer_listView);
        setupDrawer();

    }

setupDrawer()方法

private void setupDrawer() {
        // SELECT * FROM Sections where Fk == 0;
        sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
        Log.d("ELEMENT", "" + sectionsList.size());
        drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, getApplicationContext());

        // Adapter che si occupa di popolare il drawer
        drawerListView.setAdapter(drawerListViewAdapter);

        // Listner per il click su un elemento del drawer
        drawerListView.setOnItemClickListener(click);
    }

如果我在{-1}}或onCreate()之后在try-catch中调用此方法,我会遇到同样的问题。

答案 3 :(得分:0)

好的男人。试试这个:

private void callWebservice() {
    String tag_json_obj = "GetItem";
    String url = String.format("http://digitalbox.getsandbox.com/item");
    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {

            try {
                new SaveData().execute(response.getJSONArray("Sections"));
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }, new Response.ErrorListener() {
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("Settings", "Error: " + error.getMessage());
        }
    }) {
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("Accept", "application/json");
            return headers;
        }
    };
    NetworkController.getInstance().addToRequestQueue(jsonObjectRequest, tag_json_obj);
}

然后在您的活动中添加此内部类:

private final class SaveData extends AsyncTask<JSONArray, Void, Void> {
    protected Void doInBackground(JSONArray... params) {
        int c = section.length();
        for (int i = 0; i < c; i++) {

            try {
                JSONObject obj = section.getJSONObject(i);

                Sections s = new RushSearch().whereEqual("ID", obj.getInt("ID")).findSingle(Sections.class);
                if (s == null) {
                    s = new Sections();
                    s.setID(obj.getInt("ID"));
                }

                if (!obj.isNull("Fk"))
                    s.setFk(obj.getInt("Fk"));

                s.setTitle(obj.getString("Title"));

                if (!obj.isNull("CategoryColor"))
                    s.setCategoryColor(obj.getString("CategoryColor"));

                s.setPos(obj.getInt("Pos"));
                s.setLanguageId(obj.getInt("LanguageId"));
                s.setUnavailableForSend(obj.getBoolean("UnavailableForSend"));

                s.save();

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

        Log.d("sections", "DB updated");
        return null;
    }

    public void onPostExecute(Void result) {
        sectionsList = new RushSearch().whereEqual("Fk", 0).find(Sections.class);
        Log.d("ELEMENTI",""+sectionsList.size());
        drawerListViewAdapter = new DrawerListViewAdapter(sectionsList, getApplicationContext());
    }
}

我希望这有帮助!