-3

我的应用程序可以在模拟器和设备上完美运行。两者都运行 android 2.3.X。当我尝试在 android 4.0+ 上运行它时,它会在加载第二个活动时崩溃,NullPointerException结果是 a。

这是我的 logcat 输出:

03-18 20:01:59.133: E/AndroidRuntime(611): FATAL EXCEPTION: main
03-18 20:01:59.133: E/AndroidRuntime(611): java.lang.NullPointerException
03-18 20:01:59.133: E/AndroidRuntime(611):  at                           com.example.forsitefoodapp.lazyAdapter.getimage(lazyAdapter.java:106)
03-18 20:01:59.133: E/AndroidRuntime(611):  at com.example.forsitefoodapp.lazyAdapter.getView(lazyAdapter.java:81)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.ListView.fillDown(ListView.java:672)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.ListView.fillFromTop(ListView.java:732)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.ListView.layoutChildren(ListView.java:1625)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.View.layout(View.java:11278)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.RelativeLayout.onLayout(RelativeLayout.java:925)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.View.layout(View.java:11278)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.View.layout(View.java:11278)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.View.layout(View.java:11278)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.View.layout(View.java:11278)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.os.Looper.loop(Looper.java:137)
03-18 20:01:59.133: E/AndroidRuntime(611):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-18 20:01:59.133: E/AndroidRuntime(611):  at java.lang.reflect.Method.invokeNative(Native Method)
03-18 20:01:59.133: E/AndroidRuntime(611):  at java.lang.reflect.Method.invoke(Method.java:511)
03-18 20:01:59.133: E/AndroidRuntime(611):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-18 20:01:59.133: E/AndroidRuntime(611):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-18 20:01:59.133: E/AndroidRuntime(611):  at dalvik.system.NativeStart.main(Native Method)

我正在使用 ActionBarSherlock。我不知道这是否与这个问题有关。

编辑:

lazyAdapter是一个扩展的类BaseAdapter。我用它将行添加到我的ListView.

这是代码:

public class lazyAdapter extends BaseAdapter {
    private Activity activity;

    List<HashMap<String,String>> data ;

    private static LayoutInflater inflater=null;


    public lazyAdapter(Activity a, List<HashMap<String,String>> d) {
        activity = a;
        data=d;
        inflater = (LayoutInflater)activity.getSystemService(activity.getApplicationContext().LAYOUT_INFLATER_SERVICE);
        //imageLoader=new ImageLoader(activity.getApplicationContext());
    }



    public int getCount() {
        return data.size();
    }

    public Object getItem(int position) {
        HashMap<String, String> vid = new HashMap<String,String>();
        vid = data.get(position);
        return vid;

    }

    public long getItemId(int position) {
        return position;
    }

    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        if(convertView==null)
            vi = inflater.inflate(R.layout.list_row, null);

        TextView title = (TextView)vi.findViewById(R.id.title); // title
       // TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
       // TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
        ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image


        HashMap<String, String> song = new HashMap<String,String>();
        song = data.get(position);

        // Setting all values in listview
        title.setText(song.get(MainActivity.KEY_tittle));
       // artist.setText(song.get(VideoPage.KEY_description));
      //  duration.setText(song.get(VideoPage.KEY_playTime));
      String imageUrl= song.get(MainActivity.KEY_image);
      if (imageUrl != null){
       URL imgURL;
       try {
            imgURL = new URL(imageUrl);
            thumb_image.setImageBitmap(getimage (imgURL));
        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
      }
        //imageLoader.DisplayImage(song.get(VideoPage.KEY_THUMB_URL), thumb_image);
        return vi;
    }
    public Bitmap getimage (URL ImgURL){
        InputStream is = null;
        BufferedInputStream bis = null;
        Bitmap bmp = null;
        try {
           URLConnection conn = ImgURL.openConnection();
           conn.connect();
           is = conn.getInputStream();
           bis = new BufferedInputStream( is );
           bmp = BitmapFactory.decodeStream( bis );
        } catch (MalformedURLException e) {

        } catch (IOException e) {
    Log.i("error","io err");
        } finally {
           try {
              is.close();
              bis.close();
           } catch (IOException e) {

           }
        }

        return bmp;

    }

}
4

1 回答 1

2

您必须在 Lazy 图像中创建另一个线程。下载在主线程上的 Android 3.0+ 中不起作用。

您应该在其中创建另一个线程getImage()以开始下载。

于 2013-03-18T17:45:01.390 回答