应用程序崩溃只在android 4. *

时间:2013-03-18 17:33:39

标签: android

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

这是我的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;

    }

}

1 个答案:

答案 0 :(得分:2)

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

您应该在getImage()中创建另一个帖子以开始下载。