-1

我正在解析来自天气提要的 JSON 响应,并且我收到的数据包括与条件相对应的图标的 URL,然后我在 ImageView 中显示该图标。在将代码包移动到我的项目中之前,我能够在沙盒应用程序中完成这项工作。

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

我已经看到滑动菜单主要由某种类型的 ListView 组成,它使用适配器处理,我在滑动菜单的一部分中正在这样做。但我唯一的线索是它崩溃了,因为现在它是我正在尝试使用的屏幕外 UI 元素。我的滑动菜单中有一些不是由适配器处理的东西有关系吗?我无法想象还有什么不同,但也许你们对我缺少什么有所了解。希望这不是我刚刚忘记的基本内容……或者也许是:)

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

线框

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)

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

4

1 回答 1

0

updateWeatherUi()逻辑放在末尾buildWeatherObject()为我解决了这个问题。

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

于 2012-12-21T21:32:12.940 回答