通用图像加载器与JSON数据而不是常量数据

时间:2015-02-04 13:31:20

标签: android json universal-image-loader

我有一个gridview来显示图片网址中的图片。我使用通用图像加载器来显示和缓存图像。库Constant中有一个类,它有图像URL。我想用json获取url但我没有这样做并且获得空指针异常。请帮忙。非常感谢你。

这是Constant类,它有图片网址:

public final class Constants {

public static final String[] IMAGES = new String[] {
        // Heavy images
        "https://lh6.googleusercontent.com/-55osAWw3x0Q/URquUtcFr5I/AAAAAAAAAbs/rWlj1RUKrYI/s1024/A%252520Photographer.jpg",
        "https://lh4.googleusercontent.com/--dq8niRp7W4/URquVgmXvgI/AAAAAAAAAbs/-gnuLQfNnBA/s1024/A%252520Song%252520of%252520Ice%252520and%252520Fire.jpg",
        "https://lh5.googleusercontent.com/-7qZeDtRKFKc/URquWZT1gOI/AAAAAAAAAbs/hqWgteyNXsg/s1024/Another%252520Rockaway%252520Sunset.jpg",
        "https://lh3.googleusercontent.com/--L0Km39l5J8/URquXHGcdNI/AAAAAAAAAbs/3ZrSJNrSomQ/s1024/Antelope%252520Butte.jpg",
        "https://lh6.googleusercontent.com/-8HO-4vIFnlw/URquZnsFgtI/AAAAAAAAAbs/WT8jViTF7vw/s1024/Antelope%252520Hallway.jpg",
        "https://lh4.googleusercontent.com/-WIuWgVcU3Qw/URqubRVcj4I/AAAAAAAAAbs/YvbwgGjwdIQ/s1024/Antelope%252520Walls.jpg",
        "https://lh6.googleusercontent.com/-UBmLbPELvoQ/URqucCdv0kI/AAAAAAAAAbs/IdNhr2VQoQs/s1024/Apre%2525CC%252580s%252520la%252520Pluie.jpg",
        "https://lh3.googleusercontent.com/-s-AFpvgSeew/URquc6dF-JI/AAAAAAAAAbs/Mt3xNGRUd68/s1024/Backlit%252520Cloud.jpg",
        "https://lh5.googleusercontent.com/-bvmif9a9YOQ/URquea3heHI/AAAAAAAAAbs/rcr6wyeQtAo/s1024/Bee%252520and%252520Flower.jpg",
        "https://lh5.googleusercontent.com/-n7mdm7I7FGs/URqueT_BT-I/AAAAAAAAAbs/9MYmXlmpSAo/s1024/Bonzai%252520Rock%252520Sunset.jpg",
        "https://lh6.googleusercontent.com/-4CN4X4t0M1k/URqufPozWzI/AAAAAAAAAbs/8wK41lg1KPs/s1024/Caterpillar.jpg",
        "https://lh3.googleusercontent.com/-rrFnVC8xQEg/URqufdrLBaI/AAAAAAAAAbs/s69WYy_fl1E/s1024/Chess.jpg",
        "https://lh5.googleusercontent.com/-WVpRptWH8Yw/URqugh-QmDI/AAAAAAAAAbs/E-MgBgtlUWU/s1024/Chihuly.jpg",
        "https://lh5.googleusercontent.com/-0BDXkYmckbo/URquhKFW84I/AAAAAAAAAbs/ogQtHCTk2JQ/s1024/Closed%252520Door.jpg",
        "https://lh3.googleusercontent.com/-PyggXXZRykM/URquh-kVvoI/AAAAAAAAAbs/hFtDwhtrHHQ/s1024/Colorado%252520River%252520Sunset.jpg",
        "https://lh3.googleusercontent.com/-ZAs4dNZtALc/URquikvOCWI/AAAAAAAAAbs/DXz4h3dll1Y/s1024/Colors%252520of%252520Autumn.jpg",
        "https://lh4.googleusercontent.com/-GztnWEIiMz8/URqukVCU7bI/AAAAAAAAAbs/jo2Hjv6MZ6M/s1024/Countryside.jpg",
        "https://lh4.googleusercontent.com/-bEg9EZ9QoiM/URquklz3FGI/AAAAAAAAAbs/UUuv8Ac2BaE/s1024/Death%252520Valley%252520-%252520Dunes.jpg",
        "https://lh6.googleusercontent.com/-ijQJ8W68tEE/URqulGkvFEI/AAAAAAAAAbs/zPXvIwi_rFw/s1024/Delicate%252520Arch.jpg",
};

public static class Config {
    public static final boolean DEVELOPER_MODE = false;
}

public static class Extra {
    public static final String FRAGMENT_INDEX = "com.nostra13.example.universalimageloader.FRAGMENT_INDEX";
    public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION";
}

这是我的JsonParser,我试图设置imageUrls =mobile; // in json parser而不是String[]imageUrls = Constants.IMAGES;

@Override
    protected Void doInBackground(Void... arg0) {
        // Creating service handler class instance
        ServiceHandler_ sh = new ServiceHandler_();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler_.GET);

        Log.d("Response: ", "> " + jsonStr);

        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                contacts = jsonObj.getJSONArray(TAG_PRODUCTS);

                // looping through All Contacts
                for (int i = 0; i < contacts.length(); i++) {
                    JSONObject c = contacts.getJSONObject(i);
                    // Phone node is JSON Object
                    JSONObject phone = c.getJSONObject(TAG_IMAGES);
                    String mobile = phone.getString(TAG_X220);

***/* I want to set each url to the array here*/***
                    imageUrls[i]=mobile;

                    // tmp hashmap for single contact
                    HashMap<String, String> contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_X220, mobile);

                    // adding contact to contact list
                    contactList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }

        return null;
    }

这是log cat输出:

    02-04 13:13:47.806: E/AndroidRuntime(1957): FATAL EXCEPTION: main
02-04 13:13:47.806: E/AndroidRuntime(1957): java.lang.NullPointerException
02-04 13:13:47.806: E/AndroidRuntime(1957):     at com.example.imageLoaderLibrary.ImageGridFragment$ImageAdapter.getCount(ImageGridFragment.java:112)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.GridView.setAdapter(GridView.java:182)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at com.example.imageLoaderLibrary.ImageGridFragment.onCreateView(ImageGridFragment.java:83)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.View.measure(View.java:15172)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
02-04 13:13:47.806: E/AndroidRuntime(1957):     at android.view.Choreographer$CallbackRecord.run(Chor

2 个答案:

答案 0 :(得分:1)

String[]imageUrls具有固定大小,以后无法修复。在第一个参数String[]imageUrls = Constants.IMAGES;中,Constants.IMAGES具有固定大小,因此您的imageUrls也具有大小。可能你在它上面声明为String[] imageUrls= new String[];并且它没有大小。在您的JSON解析器中,您尝试修复其大小,但您不能。试试这个声明static List<String> imageUrls = new ArrayList<String>();这样你可以修改你的尺寸并在其中添加你的物品。

答案 1 :(得分:0)

我认为你应该先打电话

String[] imageUrls= new String[];