ImageLoader NullPointerException用于屏幕外的SlidingMenu项目?

时间:2012-12-21 07:17:53

标签: java android asynchronous actionbarsherlock image-loading

我正在从天气预报中解析JSON响应,我收到的数据包含一个与条件对应的图标的网址,然后我在ImageView中显示该图标。在将代码包移动到我的项目中之前,我能够在沙盒应用程序中完成这项工作。

在主项目中,我使用最新版本的ActionBarSherlockSlidingMenu进行导航,两个应用都使用Android-Async-Http-Client,但现在尝试显示时出现致命的NullPointerException ImageView中来自该URL的图标。我不认为这是由任何库引起的。

我已经看到滑动菜单主要包含使用适配器处理的某种类型的ListView,我正在滑动菜单的一部分中进行。但我唯一的线索是它崩溃了,因为现在它是我正在尝试使用的屏幕外UI元素。我的滑动菜单中有什么东西不是由适配器处理的吗?我无法想象其他许多不同,但也许你们有一些我不知道的线索。希望它不是基本的,我只是忘记......或者它可能是:)

好的,谢谢你的任何想法。

wireframe

public class MainActivity extends SlidingActivity implements
        ActionBar.OnNavigationListener {
    final String TAG = "MainActivity";
    private TextView mSelected;
    private String[] mLocations;
    ImageLoader imageLoader;
    ImageView weatherIcon;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        imageLoader = new ImageLoader(this);

        wireViews();

        getWeatherFeed();

        Context context = getSupportActionBar().getThemedContext();
        ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
                context, R.array.activities, R.layout.sherlock_spinner_item);
        list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);

        getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
        getSupportActionBar().setListNavigationCallbacks(list, this);
        getSupportActionBar().setDisplayShowTitleEnabled(false);

        // set the Behind View
        setBehindContentView(R.layout.menu_frame);

        // SlidingMenu
        SlidingMenu sm = getSlidingMenu();
        sm.setShadowWidthRes(R.dimen.shadow_width);
        sm.setShadowDrawable(R.drawable.shadow);
        sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
        sm.setFadeDegree(0.35f);
        sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);

        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        setSlidingActionBarEnabled(false);

        ListView menuList = (ListView) findViewById(R.id.menuList);
        SlidingMenuItem[] data = new SlidingMenuItem[] {
                new SlidingMenuItem("Item1 Label", "Item1 Sub-label",getResources().getDrawable(R.drawable.item1)),
                new SlidingMenuItem("Item2 Label", "Item2 Sub-Label",getResources().getDrawable(R.drawable.item2)) };

        MenuAdapter adapter = new MenuAdapter(this, R.layout.sliding_menu_item,
                data);
        menuList.setAdapter(adapter);
    }

    private void updateWeatherUi(String degreeText, String descriptionText,
            String iconUrl) {
        Log.i(TAG, "Updating UI elements...");

        if (imageLoader != null) {
            Log.i(TAG, "IMAGE_LOADER NOT NULL!!!");
        }
        imageLoader.DisplayImage(iconUrl, weatherIcon);   // FIXME: FATAL CRASH HERE !!!
    }

    /** Build a Weather object from the JSON response.
     * @throws JSONException */
    private void buildWeatherObject(String rawResponse) throws JSONException {
        Log.i(TAG, "Building Weather ojbect...");

        JSONObject baseObject = new JSONObject(rawResponse);
        JSONObject data = new JSONObject(baseObject.getString(Key.DATA));

        JSONArray conditionArray = new JSONArray(
                data.getString(Key.CURRENT_CONDITION));

        for (int i = 0; i < conditionArray.length(); i++) {
            JSONObject conditionElement = new JSONObject(
                    conditionArray.getString(i));
            weather = new Weather();
            weather.tempMaxF = conditionElement.getString(Key.TEMP_F);

            JSONArray weatherDescriptionArray = new JSONArray(
                    conditionElement.getString(Key.W_WEATHER_DESC));
            for (int j = 0; j < weatherDescriptionArray.length(); j++) {
                JSONObject weatherDescriptionElement = new JSONObject(
                        weatherDescriptionArray.getString(j));
                weather.weatherDesc = weatherDescriptionElement
                        .getString(Key.VALUE);
            }

            JSONArray weatherIconArray = new JSONArray(
                    conditionElement.getString(Key.WEATHER_ICON_URL));
            for (int j = 0; j < weatherIconArray.length(); j++) {
                JSONObject weatherIconElement = new JSONObject(
                        weatherIconArray.getString(j));
                weather.weatherIconUrl = weatherIconElement
                        .getString(Key.VALUE);
                Log.i(TAG, weather.weatherIconUrl);
            }
            conditionElement = null;

            updateWeatherUi(weather.tempMaxF, weather.weatherDesc,
                    weather.weatherIconUrl);
        }
    }

    /** Asynchronously request and receive weather feed data. */
    private void getWeatherFeed() {
        Log.i(TAG, "Getting asynchronous JSON feed...");

        AsyncHttpClient client = new AsyncHttpClient();
        client.get(WEATHER_FEED_URL, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(String response) {
                try {
                    buildWeatherObject(response);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFailure(Throwable arg0, String arg1) {
                super.onFailure(arg0, arg1);
                showToast("Sorry, the weather forecast wasn't available just then.");
            }
        });
    }

    private void wireViews() {
        Log.i(TAG, "Wiring UI elements...");
        mSelected = (TextView) findViewById(R.id.text);
        mLocations = getResources().getStringArray(R.array.activities);
        weatherIcon = (ImageView) findViewById(R.id.weatherIconView);
    }

12-21 01:01:11.130: I/MainActivity(28502): Building Weather ojbect...
12-21 01:01:11.170: I/MainActivity(28502): http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png
12-21 01:01:11.170: I/MainActivity(28502): Updating UI elements...
12-21 01:01:11.170: I/MainActivity(28502): IMAGE_LOADER NOT NULL!!!
12-21 01:01:11.170: D/AndroidRuntime(28502): Shutting down VM
12-21 01:01:11.170: W/dalvikvm(28502): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
12-21 01:01:11.180: E/AndroidRuntime(28502): FATAL EXCEPTION: main
12-21 01:01:11.180: E/AndroidRuntime(28502): java.lang.NullPointerException
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.networkxml.ImageLoader.DisplayImage(ImageLoader.java:42)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.updateWeatherUi(MainActivity.java:129)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.buildWeatherObject(MainActivity.java:172)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity.access$0(MainActivity.java:137)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.eric.app.MainActivity$1.onSuccess(MainActivity.java:195)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler.handleSuccessMessage(AsyncHttpResponseHandler.java:160)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:173)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.loopj.android.http.AsyncHttpResponseHandler$1.handleMessage(AsyncHttpResponseHandler.java:85)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.os.Looper.loop(Looper.java:150)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at android.app.ActivityThread.main(ActivityThread.java:4263)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at java.lang.reflect.Method.invokeNative(Native Method)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at java.lang.reflect.Method.invoke(Method.java:507)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 01:01:11.180: E/AndroidRuntime(28502):    at dalvik.system.NativeStart.main(Native Method)

糟糕!我试图发布一个“片段”,但也许我应该发布完整的文件......

1 个答案:

答案 0 :(得分:0)

updateWeatherUi()逻辑放在buildWeatherObject()的末尾,为我修复此问题。

问题是我仍然不确定此前发生了什么。日志在原始代码中显示了imageLoader.DisplayImage(String, ImageView)之前的正确数据,但我一直在NullPointerException崩溃。