微调器点击时出现空指针异常

时间:2015-09-18 11:28:02

标签: android parse-platform spinner adapter android-spinner

我正在尝试将数据从Parse.com绑定到微调器。我已经设置了ArrayList并验证了应用程序以适当的格式成功检索到的数据。但是当我点击微调器时,应用程序崩溃并出现以下错误。

        java.lang.NullPointerException
        at android.view.ViewGroup.canViewReceivePointerEvents(ViewGroup.java:2140)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1492)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at android.view.ViewGroup.dispatchTransformedGenericPointerEvent(ViewGroup.java:1824)
        at android.view.ViewGroup.dispatchHoverEvent(ViewGroup.java:1535)
        at android.view.View.dispatchGenericMotionEvent(View.java:7772)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchGenericMotionEvent(PhoneWindow.java:2076)
        at com.android.internal.policy.impl.PhoneWindow.superDispatchGenericMotionEvent(PhoneWindow.java:1525)
        at android.app.Activity.dispatchGenericMotionEvent(Activity.java:2494)
        at android.support.v7.internal.view.WindowCallbackWrapper.dispatchGenericMotionEvent(WindowCallbackWrapper.java:70)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchGenericMotionEvent(PhoneWindow.java:2030)
        at android.view.View.dispatchPointerEvent(View.java:7888)
        at android.view.ViewRootImpl$ViewPostImeInputStage.processPointerEvent(ViewRootImpl.java:3947)
        at android.view.ViewRootImpl$ViewPostImeInputStage.onProcess(ViewRootImpl.java:3826)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
        at android.view.ViewRootImpl$AsyncInputStage.forward(ViewRootImpl.java:3518)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
        at android.view.ViewRootImpl$AsyncInputStage.apply(ViewRootImpl.java:3575)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
        at android.view.ViewRootImpl$InputStage.onDeliverToNext(ViewRootImpl.java:3442)
        at android.view.ViewRootImpl$InputStage.forward(ViewRootImpl.java:3411)
        at android.view.ViewRootImpl$InputStage.apply(ViewRootImpl.java:3419)
        at android.view.ViewRootImpl$InputStage.deliver(ViewRootImpl.java:3392)
        at android.view.ViewRootImpl.deliverInputEvent(ViewRootImpl.java:5532)
        at android.view.ViewRootImpl.doProcessInputEvents(ViewRootImpl.java:5512)
        at android.view.ViewRootImpl.enqueueInputEvent(ViewRootImpl.java:5483)
        at android.view.ViewRootImpl$WindowInputEventReceiver.onInputEvent(ViewRootImpl.java:5612)
        at android.view.InputEventReceiver.dispatchInputEvent(InputEventReceiver.java:185)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:138)
        at android.os.Looper.loop(Looper.java:123)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invoke

我没有得到任何引用空指针异常的代码行。

这是设置微调器的片段。

  ArrayList<String> spinnerArray =  new ArrayList<String>();
    for (int i = 0 ; i < photoPrintsList.size(); i++ ) {
        spinnerArray.add(photoPrintsList.get(i).getSizes());
    }



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);

 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner sItems = (Spinner) findViewById(R.id.select_size_spinner);
    sItems.setAdapter(adapter);

和setOnSelectedListener

 sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }


    });

我做错了什么?我调试了20次,但无法弄清楚出了什么问题。请帮忙。

扩展代码: -

List<PhotoPrintsConfig> photoPrintsList;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_size);
    sharedPref=  getSharedPreferences("Images",MODE_PRIVATE);
    /*Set the action bar with the Spannable String class and add custom typeface to it.*/
    setActionBar();
    /* Get count of the images in the shared preferences. Create an arraylist to hold the images. */
    imageSelectedCount = getImageSelectedCount();
    /* Set the adapter*/
    setAdapter();



    photoPrintsList = new ArrayList<PhotoPrintsConfig>();
    ParseQuery<ParseObject> parseQuery = new ParseQuery<ParseObject>("PhotoPrints");
    parseQuery.orderByAscending("Sizes");
    parseQuery.findInBackground(new FindCallback<ParseObject>() {
        @Override
        public void done(List<ParseObject> list, ParseException e) {
            if (e == null) {
                int count = 0;
                for (ParseObject p : list) {
                    PhotoPrintsConfig prints = new PhotoPrintsConfig();
                    prints.setSizes(p.getString("Sizes"));
                      Log.d(TAG + " Data from parse " + count, prints.getSizes());
                    prints.setQuality((ArrayList) p.get("Quality"));
                    for (int i = 0; i < prints.getQuality().size(); i++) {
                          Log.d(TAG + " Data from Parse" + count, i + " " + prints.getQuality().get(i).toString());
                    }
                    prints.setPaperType((ArrayList) p.get("PaperType"));
                    for (int i = 0; i < prints.getPaperType().size(); i++) {
                         Log.d(TAG + " Data from parse" + count, i + " " + prints.getPaperType().get(i).toString());
                    }
                    prints.setPrices((ArrayList) p.get("Prices"));
                    for (int i = 0; i < prints.getPrices().size(); i++) {
                         Log.d(TAG + " data from parse " + count , i + " " + prints.getPrices().get(i).toString());
                    }
                    count++;
                    photoPrintsList.add(prints);
                    Log.d(TAG, String.valueOf(photoPrintsList.size()));
                }
                setUpSpinners();
            } else {

            }
        }

    });



}

@Override
protected void onDestroy() {
    super.onDestroy();

    if(settingsDialog!= null){
        settingsDialog.dismiss();
    }

}

private void setUpSpinners() {


   // PhotoPrintsConfig photoPrintsConfig = new PhotoPrintsConfig();

    ArrayList<String> spinnerArray =  new ArrayList<String>();
    for (int i = 0 ; i < photoPrintsList.size(); i++ ) {
        spinnerArray.add(photoPrintsList.get(i).getSizes());
    }



    ArrayAdapter<String> adapter = new ArrayAdapter<String>(
            this, android.R.layout.simple_spinner_item, spinnerArray);

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    Spinner sItems = (Spinner) findViewById(R.id.select_size_spinner);
    sItems.setAdapter(adapter);


   /* sItems.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            Toast.makeText(getApplicationContext(),"",Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }


    });*/
    //selectedListener(sItems1,spinnerArray1);
    //selectedListener(sItems2,spinnerArray2);


}

1 个答案:

答案 0 :(得分:0)

我认为这是因为订购

Spinner sItems = (Spinner) findViewById(R.id.select_size_spinner);
ArrayList<String> spinnerArray =  new ArrayList<String>();
for (int i = 0 ; i < photoPrintsList.size(); i++ ) {
    spinnerArray.add(photoPrintsList.get(i).getSizes());
}

ArrayAdapter< String > spinnerAdapter = new ArrayAdapter<>( getActivity( ), android.R.layout.simple_spinner_item, android.R.id.text1 );
spinnerAdapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item );
sItems.setAdapter( spinnerAdapter );
for ( String value : spinnerArray )
{
    spinnerAdapter.add( value );
}
sItems.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener( )
{
    @Override
    public void onItemSelected( AdapterView< ? > parent, View view, int pos, long id )
    {
        String selected = parent.getItemAtPosition( pos ).toString( );
        Toast.makeText(getApplicationContext(), selected, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onNothingSelected(AdapterView<?> adapterView) 
    {

    }
});