Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用OptionMenu android时出现的特殊截击错误_Android_Android Volley_Android Optionsmenu - Fatal编程技术网

使用OptionMenu android时出现的特殊截击错误

使用OptionMenu android时出现的特殊截击错误,android,android-volley,android-optionsmenu,Android,Android Volley,Android Optionsmenu,我正在为一所大学的学术管理系统制作一个简单的应用程序。 最初,我有一个MainActivity,其中有一个从xml文件加载的选项菜单,还有一个请求被调用到API,这两个都成功了。 然后,我将onCreateOptionsMenu()方法添加到MainActivity。这导致应用程序崩溃,调试消息表明对同一API的请求出现错误 我的onCreateOptions菜单()代码是 我动态创建项的drawermenu.xml的代码是 <item android:title=

我正在为一所大学的学术管理系统制作一个简单的应用程序。
最初,我有一个MainActivity,其中有一个从xml文件加载的选项菜单,还有一个请求被调用到API,这两个都成功了。 然后,我将onCreateOptionsMenu()方法添加到MainActivity。这导致应用程序崩溃,调试消息表明对同一API的请求出现错误

我的onCreateOptions菜单()代码是

我动态创建项的drawermenu.xml的代码是

    <item
        android:title="courses"
        android:id="@+id/courses">
        <menu >                              <!-- add courses here -->
            <item
                android:title="course1"
                android:icon="@drawable/courses"
                android:id="@+id/nav_item_courses"/>
            <item
                android:title="course2"
                android:icon="@drawable/bin"/>
        </menu>
    </item>


任何帮助都将不胜感激。谢谢

我认为问题就在这里——course=courses.getJSONObject(I);因为您的响应JsonObject返回null。但是在此之前调用的callCourse()方法应该获取该对象(作为一个截击请求的响应获得)。因此,问题在于抽射。调试日志表明,截取请求返回一个错误响应,而同一请求返回一个成功响应,但没有onCreateOptionView()方法。
//this is entered when the user has just logged in...
Log.d(TAG, "calling course api");
RequestQueue requestQueue = Volley.newRequestQueue(this,       SessionManager.httpStack);
//requestqueue is made using http-stack as we need to check the sessions of the logged in user
JsonObjectRequest courseRequest = new JsonObjectRequest
    (Request.Method.GET, course_url, null, new Response.Listener<JSONObject> () {

        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG, response.toString());
            //pDialog.setMessage("Response: "+ response.toString());
            //0th element of the course object now has the response
            courseobject[0] = response;
            Log.d(TAG,"success");
            //So the dialog box is hid now...
            //pDialog.hide();
            if (courseobject[0] != null)
            {
                //Also the Course data is put in a hash map of the Session Manager Preferences so that we dont need to call this API again...
                SessionManager.setCourseData(courseobject[0]);
                Log.d(TAG, "setting course data in cookie");

                // Setup UI elements
                setup();

                // Call home fragment - course buttons created there
                FragmentTransaction xfragmentTransaction = mFragmentManager.beginTransaction();
                xfragmentTransaction.replace(R.id.containerView,new FragmentHome()).commit();

                callGrades();
            }
            else
            {
                //The Response is null and the user is not registered in any course till now...
                Toast.makeText(getApplicationContext(), "Not registered for any course", LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error)
        {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            //pDialog.setMessage(error.getMessage());
            Toast.makeText(getApplicationContext(), "Failed to fetch course data", LENGTH_LONG).show();
            //pDialog.setMessage(error.getCause().toString());
            //pDialog.hide();
        }
    });
Entered onCreateOptionsMenu
coursedata is empty
http://10.192.44.89:8000/courses/list.json
calling course api
AndroidRuntime: Shutting down VM
I/System.out: Thread-31118(ApacheHTTPLog):isSBSettingEnabled false
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.quantumcoder.moodleplus, PID: 30013
java.lang.NullPointerException: Attempt to invoke virtual method java.lang.Object org.json.JSONObject.get(java.lang.String) on a null object reference
at com.example.quantumcoder.moodleplus.MainActivity.onCreateOptionsMenu(MainActivity.java:290)
at android.app.Activity.onCreatePanelMenu(Activity.java:2959)
at android.support.v4.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:298)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.app.AppCompatDelegateImplBase$AppCompatWindowCallbackBase.onCreatePanelMenu(AppCompatDelegateImplBase.java:243)
at android.support.v7.internal.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:85)
at android.support.v7.internal.app.ToolbarActionBar.populateOptionsMenu(ToolbarActionBar.java:448)
at android.support.v7.internal.app.ToolbarActionBar$1.run(ToolbarActionBar.java:65)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6145)
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:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
    <item
        android:title="courses"
        android:id="@+id/courses">
        <menu >                              <!-- add courses here -->
            <item
                android:title="course1"
                android:icon="@drawable/courses"
                android:id="@+id/nav_item_courses"/>
            <item
                android:title="course2"
                android:icon="@drawable/bin"/>
        </menu>
    </item>