1

您好,我在使用 asyncTask 填充惰性 ListView 时遇到问题。所以我在寻求你的帮助。

这是我的活动课程:

import java.util.ArrayList;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;

import com.outsoucefarm.musicagratis.functions.AlbumLazyAdapter;
import com.outsoucefarm.musicagratis.functions.JSONParser;
import com.outsoucefarm.musicagratis.objects.Albums;
import com.outsoucefarm.musicagratis.objects.Songs;
import com.outsourcefarm.musicagratis.R;

public class LatestAlbums extends Activity {

    // widgets
    private ListView list;
    private ImageView back;
    private EditText find;

    // URL to make request
    private static String url = "http://someurl.com/data.json";

    // JSON Node names
    private static final String TAG_ALBUMS = "albums";
    private static final String TAG_ID = "id";
    private static final String TAG_NAME = "name";
    private static final String TAG_SINGER = "singer";
    private static final String TAG_GENRE = "genre";
    private static final String TAG_MIX = "mix";
    private static final String TAG_THUMB = "thumb";
    private static final String TAG_SONGS = "songs";
    private static final String TAG_SONG_TITLE = "song";
    private static final String TAG_SONG_ARTIST = "artist";
    private static final String TAG_SONG_MP3 = "mp3";
    private static final String TAG_SONG_MP4 = "mp4";
    private static final String TAG_SONG_THUMB = "thumb";

    // albums JSONArray
    private JSONArray albums = null;
    private JSONArray sngs = null;

    private ArrayList<Albums> albumsArray;
    private ArrayList<Songs> songsArray;
    private AlbumLazyAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.latest_albums);
        try {
            loadJSON();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // initWidgets();
    }

    private void initWidgets() {
        list = (ListView) findViewById(R.id.listView1);
        back = (ImageView) findViewById(R.id.latest_back_img);
        find = (EditText) findViewById(R.id.latest_search_et);
        back.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new AlbumLazyAdapter(this, albumsArray);
        list.setAdapter(adapter);
    }

    public void loadJSON() throws JSONException {
        AsyncTask<Void, Void, Void> task = new AsyncTask<Void, Void, Void>() {

            @Override
            protected Void doInBackground(Void... params) {
                // Creating JSON Parser instance
                JSONParser jParser = new JSONParser();

                // getting JSON string from URL
                JSONObject json = jParser.getJSONFromUrl(url);

                JSONObject theJSON;

                try {
                    // Getting Array of albums
                    theJSON = json.getJSONObject("json");
                    albums = theJSON.getJSONArray(TAG_ALBUMS);
                    // sngs=theJSON.getJSONArray(TAG_SONGS);
                    // looping through All albums
                    for (int i = 0; i < albums.length(); i++) {
                        JSONObject c = albums.getJSONObject(i);

                        // Storing each json item in variable
                        String album_id = c.getString(TAG_ID);
                        String album_name = c.getString(TAG_NAME);
                        String album_singer = c.getString(TAG_SINGER);
                        String album_mix = c.getString(TAG_MIX);
                        String album_genre = c.getString(TAG_GENRE);
                        String album_thumb = c.getString(TAG_THUMB);
                        albumsArray.add(new Albums(album_id, album_name,
                                album_singer, album_genre, album_mix,
                                album_thumb));
                        Log.v("--", "Albums" + " " + album_id + " "
                                + album_name + " " + album_genre + " "
                                + album_singer + " " + album_thumb + " "
                                + album_mix);
                        sngs = c.getJSONArray(TAG_SONGS);

                        // getting songs for the album
                        for (int j = 0; j < sngs.length(); j++) {
                            JSONObject songs = sngs.getJSONObject(j);
                            String song_artist = songs
                                    .getString(TAG_SONG_ARTIST);
                            String song_mp3 = songs.getString(TAG_SONG_MP3);
                            String song_mp4 = songs.getString(TAG_SONG_MP4);
                            String song_thumb = songs.getString(TAG_SONG_THUMB);
                            String song_title = songs.getString(TAG_SONG_TITLE);
                            Log.v("--", "Songs: " + j + " " + song_artist + " "
                                    + song_mp3 + " " + song_mp4 + " "
                                    + song_thumb + " " + song_title);
                            songsArray.add(new Songs(song_title, song_artist,
                                    song_mp3, song_mp4, song_thumb));
                        }

                    }

                } catch (JSONException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            protected void onPostExecute(Void result) {
                super.onPostExecute(result);
                initWidgets();
                adapter.notifyDataSetChanged();
            }

        }; task.execute();

    }

}

这是我从 logCat 中得到的:

03-30 19:44:48.352: E/AndroidRuntime(2474): FATAL EXCEPTION: AsyncTask #1
03-30 19:44:48.352: E/AndroidRuntime(2474): java.lang.RuntimeException: An error occured while executing doInBackground()
03-30 19:44:48.352: E/AndroidRuntime(2474):     at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.lang.Thread.run(Thread.java:856)
03-30 19:44:48.352: E/AndroidRuntime(2474): Caused by: java.lang.NullPointerException
03-30 19:44:48.352: E/AndroidRuntime(2474):     at com.outsourcefarm.musicagratis.activities.LatestAlbums$2.doInBackground(LatestAlbums.java:116)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at com.outsourcefarm.musicagratis.activities.LatestAlbums$2.doInBackground(LatestAlbums.java:1)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-30 19:44:48.352: E/AndroidRuntime(2474):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-30 19:44:48.352: E/AndroidRuntime(2474):     ... 5 more

这是在第 116 行:

albumsArray.add(new Albums(album_id, album_name,
                                album_singer, album_genre, album_mix,
                                album_thumb));
4

1 回答 1

1

可能忘记调用启动 AsyncTaskAsyncTask.execute()loadJSON方法。在你的罐子里,你需要把task.execute();最后一条语句放在loadJSON()方法中

于 2013-03-30T18:41:24.213 回答