无法修复 Looper.java 中的异常

Unable to fix exception in Looper.java

我创建了一个用于审计酒店的应用程序。 这是里面的一个方法。此方法应该保存审核员在使用该应用程序时输入的答案。方法是:

  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。好像有很多错误。

这是我的错误 logcat:

java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference 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) at android.support.v4.app.Fragment.performOptionsItemSelected(Fragment.java:2473) at android.support.v4.app.FragmentManagerImpl.dispatchOptionsItemSelected(FragmentManager.java:3350) at android.support.v4.app.FragmentController.dispatchOptionsItemSelected(FragmentController.java:347) at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:413) at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:195) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:108) at android.support.v7.app.ToolbarActionBar.onMenuItemClick(ToolbarActionBar.java:63) at android.support.v7.widget.Toolbar.onMenuItemClick(Toolbar.java:203) at android.support.v7.widget.ActionMenuView$MenuBuilderCallback.onMenuItemSelected(ActionMenuView.java:780) at android.support.v7.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:822) at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:171) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:973) at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:963) at android.support.v7.widget.ActionMenuView.invokeItem(ActionMenuView.java:624) at 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) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

很明显 editText.getTag() return 是一个空值。因此,您应该检查您的数据,避免崩溃的快速修复是:

if(edtText.getTag() == null) return;
if (edtText.getTag().toString().equalsIgnoreCase("Inspected By")){