Android应用程序崩溃

时间:2017-01-03 04:57:35

标签: android

以下是我的Spinner代码。当我点击Spinner应用程序时崩溃了。我想检查微调器是否为空,然后它应该提供崩溃警告。 '区域'是我声明的材质微调器。        xml代码

       <TextView
        android:text="Region"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/txtregion"
        android:layout_below="@+id/city"/>
    <com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner
        android:id="@+id/region"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Select Region"
        android:layout_below="@+id/txtregion"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"/>
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        pDialog.dismiss();
        Log.d("RESULT",""+s);
        if (s != null) {
            try {
                JSONObject jsonObject = new JSONObject(s);
                String error = jsonObject.get("error").toString();
                Log.d("RESULT", " " + error);
                if (error == "false")
                {
                  try
                {
                    Iterator keys = jsonObject.keys();
                    JSONObject currentDynamicValue;
                    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_item);
                    adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
                    int i=0;
                    while (keys.hasNext()) {
                        // loop to get the dynamic key
                        String currentDynamicKey = (String) keys.next();
                        // get the value of the dynamic key
                        currentDynamicValue = jsonObject.getJSONObject(currentDynamicKey);
                        list.put(i,currentDynamicValue.get("region_id").toString());

                        i++;
                        Log.d("name", "----------------------> " + currentDynamicValue);
                        final String name =  currentDynamicValue.get("region_name").toString();
                        pin =  currentDynamicValue.get("pin").toString();
                        Toast.makeText(getActivity().getApplicationContext(),"Region Pin"+pin,Toast.LENGTH_SHORT).show();
                        adapter.add(name);
                        region.setAdapter(adapter);
                        region.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                            @Override
                            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                                String region_name = list.get(i);
                                if(region != null && adapterView.getSelectedItem() !=null ) {
                                   Toast.makeText(getActivity().getApplicationContext(), "Region Pin" + region_name, Toast.LENGTH_SHORT).show();
                                    postcode.setText(pin);
                                }
                                else{
                                    Toast.makeText(getActivity().getApplicationContext(), "Please select Country", Toast.LENGTH_SHORT).show();
                                }
                            }

                        });

                    }
                }
                catch(Exception e)
                {

                }

这是Logcat:

           E/MessageQueue-JNI: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Filter.filter(java.lang.CharSequence, android.widget.Filter$FilterListener)' on a null object reference
                                                                       at android.widget.AutoCompleteTextView.performFiltering(AutoCompleteTextView.java:867)
                                                                       at com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner.onFocusChanged(MaterialBetterSpinner.java:49)
                                                                       at android.view.View.handleFocusGainInternal(View.java:5556)
                                                                       at android.view.View.requestFocusNoSearch(View.java:8565)
                                                                       at android.view.View.requestFocus(View.java:8544)
                                                                       at android.view.View.requestFocus(View.java:8511)
                                                                       at android.view.View.requestFocus(View.java:8490)
                                                                       at com.weiwangcn.betterspinner.library.material.MaterialBetterSpinner.onTouchEvent(MaterialBetterSpinner.java:74)
                                                                       at android.view.View.dispatchTouchEvent(View.java:9427)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2666)
                                                                       at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2318)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2451)
                                                                       at com.android.internal.policy.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1770)
                                                                       at android.app.Activity.dispatchTouchEvent(Activity.java:2817)
                                                                       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
                                                                       at android.support.v7.view.WindowCallbackWrapper.dispatchTouchEvent(WindowCallbackWrapper.java:67)
                                                                       at com.android.internal.policy.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:2412)
                                                                       at android.view.View.dispatchPointerEvent(View.java:9658)
                                                                       at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:4828)
                                                                       at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:4686)
                                                                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
                                                                       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4244)
                                                                       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4210)
                                                                       at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:4356)
                                                                       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4218)
                                                                       at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:4413)
                                                                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
                                                                       at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:4244)
                                                                       at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:4210)
                                                                       at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:4218)
                                                                       at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:4191)
                                                                       at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:6645)
                                                                       at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:6619)
                                                                       at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:6562)
                                                                    at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRoot

1 个答案:

答案 0 :(得分:1)

这是因为您在空对象引用上调用filter(java.lang.CharSequence, android.widget.Filter.FilterListener)方法。

引用为null,因此它不知道如何在运行时处理它。