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

AsyncTask を開始するためのメソッドを呼び出すのを忘れているAsyncTask.execute()可能性があります。loadJSONあなたの缶では、メソッドtask.execute();の最後のステートメントとして配置する必要がありますloadJSON()

于 2013-03-30T18:41:24.213 に答える