从db加载图像路径时,应用程序崩溃

时间:2014-11-29 23:18:31

标签: java android

我认为问题是加载路径然后缩放图像。该路径直接指向MediaStore,但在数据库中保存为String。现在我想显示一个列表,其中包含此pichtures的缩略图。

  

FATAL EXCEPTION:onLoadFinished行上的主java.lang.NullPointerException(图片为imChild.setImageBitmap(bmImg)的加载器;

以下是代码:

    setListAdapter(adapter = new SimpleCursorAdapter(ChildrenActivity.this,
            R.layout.layout_list, null, new String[] {
            Children.COL_CHILD_IMAGE, Children.COL_CHILD_FIRSTNAME,Children.COL_CHILD_NUMBER }, new int[] {R.id.imagechild, R.id.textlist1,
            R.id.textlist2 }, 0));


    final ListView listView = getListView();

    // Load the content
    getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
        @Override
        public Loader<Cursor> onCreateLoader(int id, Bundle args) {


            return new CursorLoader(ChildrenActivity.this,
                    YounikumProvider.URI_YOUNIKUM, Children.FIELDS, null, null,
                    null);
        }

        @Override
        public void onLoadFinished(Loader<Cursor> loader, Cursor c) {


            if(c.getCount() == 0)
                return;
            String path = "";

            if(c.moveToFirst()) {
                path = c.getString(c.getColumnIndex("childImage"));
                File imgFile = new File(path);

                if(imgFile.exists()) {
                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inSampleSize = 2;

                    ImageView imChild = (ImageView) listView.findViewById(R.id.imagechild);
                    Bitmap bmImg = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),options);


                  imChild.setImageBitmap(bmImg);
                }
            }


            ((SimpleCursorAdapter) getListAdapter()).swapCursor(c);
        }

        @Override
        public void onLoaderReset(Loader<Cursor> arg0) {
            ((SimpleCursorAdapter) getListAdapter()).swapCursor(null);
        }
    });


        listView.setDivider(new ColorDrawable(Color.GRAY));
       listView.setDividerHeight(3); // 3 pixels height


    listView.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    listView.setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {
        @Override
        public void onItemCheckedStateChanged(ActionMode mode, int position, long id, boolean checked) {

        }

        @Override
        public boolean onCreateActionMode(ActionMode mode, Menu menu) {
           getMenuInflater().inflate(R.menu.dailylog_context, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
            return false;
        }

        @Override
        public boolean onActionItemClicked(ActionMode mode, MenuItem item) {


            if(item.getItemId() == R.id.dailylog_cab_delete) {

                //    SparseBooleanArray checkedDailyLogs = listView.getCheckedItemPositions();
                SparseBooleanArray checkedItems = listView.getCheckedItemPositions();

                final int checkedItemsCount = checkedItems.size();
                for(int i = 0; i < checkedItemsCount; i++) {

                    final int position = checkedItems.keyAt(i);
                    final boolean isChecked = checkedItems.valueAt(i);

                    int adid = (int) adapter.getItemId(position);

                    String selection =  listView.getItemAtPosition(position).toString();



                   Children childNo = DatabaseHandler.getInstance(ChildrenActivity.this).getChildren(adid);


                    if(checkedItems.valueAt(i)) {

                        ConnectivityManager connMgr = (ConnectivityManager)
                                getSystemService(Context.CONNECTIVITY_SERVICE);
                        NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
                        if (networkInfo != null && networkInfo.isConnected()) {

                            //delete hier von Eintrag noch einfügen
                            DatabaseHandler.getInstance(ChildrenActivity.this).removeChildren(adid);
                           webdelete(Long.toString(childNo.child_number));


                            checkedItems = listView.getCheckedItemPositions();
                        }
                    }
                }
            }
            return false;
        }

        @Override
        public void onDestroyActionMode(ActionMode mode) {

        }
    });


    Button clickButton = (Button) findViewById(R.id.buttonAddChildren);
    clickButton.setOnClickListener( new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent childrenAddIntent = new Intent(ChildrenActivity.this, ChildrenAddActivity.class);
            startActivity(childrenAddIntent);
        }
    });
}

0 个答案:

没有答案