我正在为一所大学的学术管理系统制作一个简单的应用程序 最初,我有一个MainActivity,其中包含从xml文件加载的选项菜单,以及一个被调用到API的请求,两者都是成功的。 然后,我向MainActivity添加了一个onCreateOptionsMenu()方法。这导致应用程序崩溃,并且调试消息表明对同一API的请求发出错误。
我的onCreateOptionsMenu()代码是
@Override
public boolean onCreateOptionsMenu(Menu menu) {
Log.d(TAG,"Entered onCreateOptionsMenu");
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.drawermenu, menu);
//super.onCreateOptionsMenu(menu);
MenuItem courseitem = menu.findItem(R.id.courses);
SubMenu coursemenu = courseitem.getSubMenu();
//The CourseObject contained Json of user and courses..So courses now has only course specific json..
JSONArray courses = null;
if( SessionManager.getCourseData()==null){
Log.d(TAG,"coursedata is empty");
callCourse();
}
try {
courses = (JSONArray)SessionManager.getCourseData().get("courses");
}
catch (JSONException e) {
e.printStackTrace();
}
String coursecode = "";
int courseId = 0;
//For Loop for adding courses to submenu
for (int i = 0; i < courses.length(); i++){
JSONObject course = null;
try
{
course = courses.getJSONObject(i);
//Log.d(TAG, course.toString());
coursecode = (String) course.get("code");
courseId = (int) course.get("id");
}
catch (JSONException e)
{
e.printStackTrace();
}
coursemenu.add(Menu.NONE,courseId,Menu.NONE,coursecode);
}
return true;
}
我的截击请求的代码是
//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();
}
});
//课程申请现已添加到请求队列中... requestQueue.add(courseRequest);
日志中的错误消息是(格式化后)
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)
我动态创建项目的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>
任何帮助将不胜感激。谢谢!