0

我正在尝试使用以下代码在图像视图中显示位图图像:

ImageView imageView = (ImageView) findViewById(R.id.item_picture);
byte[] decodedString = Base64.decode(temp, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
imageView.setImageBitmap(decodedByte);

但是它一直在最后一行崩溃,我不知道为什么。

继承人logcat:

09-08 09:29:01.626: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/KeypressStandard.ogg
09-08 09:29:01.636: E/SoundPool(275): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-08 09:29:01.646: E/SoundPool(275): error loading /system/media/audio/ui/KeypressDelete.ogg
09-08 09:29:01.646: E/SoundPool(275): error loading /system/media/audio/ui/KeypressReturn.ogg
09-08 09:29:03.549: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/KeypressStandard.ogg
09-08 09:29:03.556: E/SoundPool(275): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-08 09:29:03.566: E/SoundPool(275): error loading /system/media/audio/ui/KeypressDelete.ogg
09-08 09:29:03.566: E/SoundPool(275): error loading /system/media/audio/ui/KeypressReturn.ogg
09-08 09:29:08.727: E/AndroidRuntime(7288): FATAL EXCEPTION: main
09-08 09:29:08.727: E/AndroidRuntime(7288): java.lang.NullPointerException
09-08 09:29:08.727: E/AndroidRuntime(7288):     at com.myCompany.myApp.SearchActivity$MyListAdapter.getView(SearchActivity.java:139)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.AbsListView.obtainView(AbsListView.java:2177)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.ListView.measureHeightOfChildren(ListView.java:1247)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.ListView.onMeasure(ListView.java:1159)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.View.measure(View.java:15848)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:494)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.View.measure(View.java:15848)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.View.measure(View.java:15848)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.View.measure(View.java:15848)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5008)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.View.measure(View.java:15848)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.Choreographer.doCallbacks(Choreographer.java:562)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.Choreographer.doFrame(Choreographer.java:532)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.os.Handler.handleCallback(Handler.java:730)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.os.Looper.loop(Looper.java:137)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at android.app.ActivityThread.main(ActivityThread.java:5103)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at java.lang.reflect.Method.invokeNative(Native Method)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at java.lang.reflect.Method.invoke(Method.java:525)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-08 09:29:08.727: E/AndroidRuntime(7288):     at dalvik.system.NativeStart.main(Native Method)
09-08 09:29:11.216: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:11.226: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:11.226: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:11.226: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:11.236: E/SoundPool(275): error loading /system/media/audio/ui/Effect_Tick.ogg
09-08 09:29:11.236: E/SoundPool(275): error loading /system/media/audio/ui/KeypressStandard.ogg
09-08 09:29:11.236: E/SoundPool(275): error loading /system/media/audio/ui/KeypressSpacebar.ogg
09-08 09:29:11.236: E/SoundPool(275): error loading /system/media/audio/ui/KeypressDelete.ogg
09-08 09:29:11.246: E/SoundPool(275): error loading /system/media/audio/ui/KeypressReturn.ogg

这里的getView代码:

@Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        //make sure we have a view to work with (may have been given null)
        View itemView = convertView;

        if(itemView == null)
        {
                itemView = getLayoutInflater().inflate(R.layout.list_item, parent, false);
        }

        //Find the user to work with
        Users currentUser = myUsers.get(position);

        //Fill the view
        TextView text;
        text = (TextView) itemView.findViewById(R.id.item_name);
        text.setText(currentUser.getName());

        text = (TextView) itemView.findViewById(R.id.item_location);
        text.setText(currentUser.getLocation());

        String temp = currentUser.getPicture();

        if(!temp.equals("noPicture"))
            {
                ImageView imageView = (ImageView) findViewById(R.id.item_picture);
                byte[] decodedString = Base64.decode(temp, Base64.DEFAULT);
                Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length); 
                imageView.setImageBitmap(decodedByte);
        }

        return itemView;
    }

任何人都可以看到它崩溃的原因吗?

4

1 回答 1

0

代替:

ImageView imageView = (ImageView) findViewById(R.id.item_picture);

和:

ImageView imageView = (ImageView) itemView.findViewById(R.id.item_picture);

View您应该在您的 中搜索itemView,因为它View在 Main 布局中不存在,它仅在您的ListView项目中可见,因此findViewById将返回null

于 2013-09-08T13:47:12.753 回答