我创建了一个用于审核酒店的应用程序。 这是其中的一种方法。此方法应该保存审核员在使用应用程序时输入的答案。方法是:
private void loopQuestions(ViewGroup parent) {
JSONArray ansArry = new JSONArray();
JSONObject params;
String inspectedDate = inspctDate, InspectedBy = "" ;
try {
//loop1
Log.e("Total Child", parent.getChildCount() + "");
for (int i = 0; i < parent.getChildCount(); i++) {
View child = parent.getChildAt(i);
String header ="";
if (child instanceof LinearLayout) {
//Support for linearlayout
ViewGroup llchild = (LinearLayout) child;
//loop 2
for (int j = 0; j < llchild.getChildCount(); j++) {
View subChild = llchild.getChildAt(j);
params = new JSONObject();
if (subChild instanceof EditText) {
nosOfQustion++;
EditText edtText = (EditText) subChild;
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")) {
InspectedBy = getText(edtText.getText().toString().trim());
} else {
params.put("Question", getText(edtText.getTag().toString()));
params.put("Answer", getText(edtText.getText().toString()));
params.put("Category", header);
ansArry.put(params);
}
} else if (subChild instanceof Spinner) {
nosOfQustion++;
Spinner spr = (Spinner) subChild;
params.put("Question", getText(spr.getTag().toString()));
params.put("Answer", getText(spr.getSelectedItem().toString()));
params.put("Category", header);
ansArry.put(params);
} else if (subChild instanceof RadioButton) {
String answer="";
if (rg.getCheckedRadioButtonId()==edtId_1)
{answer= "Yes";}
else if (rg.getCheckedRadioButtonId()==edtId_2)
{answer="No";}
params.put("Question", getText(rg.getTag().toString()));
params.put("Answer", answer);
params.put("Category", header);
ansArry.put(params);
}
else if (subChild instanceof RatingBar) {
params.put("Question", getText(ratingBar.getTag().toString()));
params.put("Answer", ratingBar.getRating());
params.put("Category", header);
ansArry.put(params);
}
}//end inner for loop
}
}
if (isNewAudit) {
cal = Calendar.getInstance();
day = cal.get(Calendar.DAY_OF_MONTH);
month = cal.get(Calendar.MONTH);
year = cal.get(Calendar.YEAR);
Log.e("auditId", auditId);
if (!PropertySelectionPage.selectedCity.equalsIgnoreCase("------Other Cities------")) {
if (!InspectedBy.equalsIgnoreCase("")) {
System.out.println("JsonObj " + ansArry.toString());
datasource.insertAudit(auditId, InspectedBy, inspectedDate, PropertySelectionPage.selectedCity,PropertySelectionPage.selectedProperty, ansArry.toString(), 0, PropertySelectionPage.propAddress.contains("'") ? PropertySelectionPage.propAddress.replace("'", "") : PropertySelectionPage.propAddress, PropertySelectionPage.pID);
Log.e("NosOfQuestion", nosOfQustion + "");
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Please enter Inspected By", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Please select city", Toast.LENGTH_SHORT).show();
}
} else {
Log.e("else auditId", auditId);
datasource.deleteAudit(auditId);
System.out.println("JsonObj " + ansArry.toString());
if (!PropertySelectionPage.selectedCity.equalsIgnoreCase("------Other Cities------")) {
if (!InspectedBy.equalsIgnoreCase("")) {
datasource.insertAudit(auditId, InspectedBy, inspectedDate, PropertySelectionPage.selectedCity, PropertySelectionPage.selectedProperty, ansArry.toString(), 0, PropertySelectionPage.propAddress, PropertySelectionPage.pID);
Log.e(" else NosOfQuestion", nosOfQustion + "");
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
getActivity().finish();
} else {
Toast.makeText(getActivity(), "Please enter Inspected By", Toast.LENGTH_SHORT).show();
}
} else {
Toast.makeText(getActivity(), "Please select city", Toast.LENGTH_SHORT).show();
}
}
} catch(JSONException e){
e.printStackTrace();
}
}
它被称为:
public void saveAnswers() {
loopQuestions(layer1);
}
当我尝试调试时,调试器会将我带到Looper.java。它似乎有很多错误。
这是我的日志中的错误:
java.lang.NullPointerException: Try to invoke virtual method 'java.lang.String java.lang.Object.toString()' 在空对象上 参考 at com.csr.audit.Tab1.loopQuestions(Tab1.java:666) at com.csr.audit.Tab1.saveAnswers(Tab1.java:623) at com.csr.audit.Tab1.onOptionsItemSelected(Tab1.java:1018) 在 android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:2473) 在 android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:3350) 在 android.support.v4.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:347) 在 android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:413) 在 android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) 在 android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) 在 android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) 在 android.support.v7.app.ToolbarActionBar$2.onMenuItemClick(ToolbarActionBar.java:63) 在 android.support.v7.widget.Toolbar$1.onMenuItemClick(Toolbar.java:203) 在 android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780) 在 android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) 在 android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171) 在 android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973) 在 android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963) 在 android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) 在 android.support.v7.view.menu.ActionMenuItemView.onClick(ActionMenuItemView.java:150) at android.view.View.performClick(View.java:6294) at android.view.View$PerformClick.run(View.java:24770) at android.os.Handler.handleCallback(Handler.java:790) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:164) at android.app.ActivityThread.main(ActivityThread.java:6494) at java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
很明显,editText.getTag() 返回一个空值。因此,您应该检查您的数据,避免崩溃的快速解决方法是:
if(edtText.getTag() == null) return;
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")){