这是我从互联网上得到的代码,我需要它用于我自己的应用程序。我试着用这个例子制作一个应用程序。不同的是,我为这个名为JSONTask的对象创建了一个单独的类。getApplicationContex()函数出错。请帮帮我。

import android.os.AsyncTask;
import android.widget.ListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import navdrawerexample1.models.MovieModel;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class JSONTask extends AsyncTask<String,String,List<MovieModel>>
{
private ListView lvMovies;
@Override
protected List<MovieModel> doInBackground(String... params) {
    HttpURLConnection connection = null;
    BufferedReader reader = null;
    try {
        URL url= new URL(params[0]);
        connection = (HttpURLConnection) url.openConnection();
        connection.connect();


        InputStream stream = connection.getInputStream();
        reader = new BufferedReader(new InputStreamReader(stream));
        StringBuffer buffer = new StringBuffer();
        String line ="";
        while ((line = reader.readLine())!=null){
            buffer.append(line);
        }
        String finalJason = buffer.toString();

        JSONObject parentObject = new JSONObject(finalJason);
        JSONArray parentArray = parentObject.getJSONArray("movies");

        List<MovieModel> movieModelList = new ArrayList<>();
        //StringBuffer finalBufferData = new StringBuffer();
        for (int i=0; i<parentArray.length();i++) {
            JSONObject finalObject = parentArray.getJSONObject(i);
            MovieModel movieModel = new MovieModel();
            movieModel.setMovie(finalObject.getString("movie"));
            movieModel.setYear(finalObject.getInt("year"));
            movieModel.setRating((float) finalObject.getDouble("rating"));
            movieModel.setDuration(finalObject.getString("duration"));
            movieModel.setDirector(finalObject.getString("director"));
            movieModel.setTagline(finalObject.getString("tagline"));
            movieModel.setImage(finalObject.getString("image"));
            movieModel.setStory(finalObject.getString("story"));

            List<MovieModel.cast> castList = new ArrayList<>();
            for (int j=0; j<finalObject.getJSONArray("cast").length();j++){
                MovieModel.cast cast = new MovieModel.cast();
                cast.setName(finalObject.getJSONArray("cast").getJSONObject(j).getString("name"));
                castList.add(cast);
            }
            movieModel.setCastList(castList);
            //adding the final object in the list
            movieModelList.add(movieModel);
        }
        return  movieModelList;

    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    } finally {
        if (connection != null) {
            connection.disconnect();
        }
        try {
            if (reader != null) {
                reader.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return null;
}

@Override
protected void onPostExecute(List<MovieModel> result) {
    super.onPostExecute(result);
    //This is where I get the error
    MovieAdapter adapter = new MovieAdapter(getApplicationContext(),R.layout.row,result);
    lvMovies.setAdapter(adapter);
    //TODO need to set data to the list
}

}

最佳答案

问题是,您在不扩展getApplicationContext()或其子类(Context等)的类中调用Activity。您应该将Context或其子类传递给JSONTask构造函数。此外,我看不到您在哪里初始化lvMovies-而且您很可能在NullPointerException-我的建议是,您应该将这个也传递给您的lvMovies.setAdapter(adapter);构造函数:

private Context mContext;
private ListView lvMovies;
public JSONTask (Context context, ListView  lstView){
   this.lvMovies = lstView;
   this.mContext = context;
}

因此在JSONTask中,您可以执行如下操作:
MovieAdapter adapter = new MovieAdapter(mContext,R.layout.row,result);
lvMovies.setAdapter(adapter);

我希望这能有帮助。

07-24 13:28