-3

私のアプリケーションは、エミュレーターとデバイスで完全に動作します。どちらもアンドロイド 2.3.X を実行しています。Android 4.0+ で実行しようとするとNullPointerException、結果として 2 番目のアクティビティの読み込みでクラッシュします。

これが私の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 に答える