无法弄清楚Null指针异常错误

时间:2014-08-12 08:38:33

标签: android json android-fragments nullpointerexception

我现在正在开发一款Android应用程序,现在情况一直很顺利。但突然间我得到一个空指针错误,无法找出问题所在。事情是应用程序之前工作正常。我突然得到了这个错误。我可能会意外地做一些事情,现在我无法弄清楚我做错了什么

根据log cat,错误在这些类中

TheaterFragment类

package com.fortuna.cinemalk;

import java.util.ArrayList;

import android.app.Activity;
import android.app.FragmentManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.content.Intent;
import android.widget.AdapterView;



import com.fortuna.cinemalk.adapter.LazyAdapter;
import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.service.CommonVariable;
import com.fortuna.cinemalk.service.JSONServices;
import com.fortuna.cinemalk.util.Element;

public class TheaterFragment extends Fragment {

    private GridView gridView;

    private ArrayList<BaseElement> filmTheater;
    private LazyAdapter adapter;
    private Activity activity;
    private CommonVariable commonVariable;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.theater_fragment, container,
                false);

        activity = this.getActivity();

        commonVariable = (CommonVariable) activity.getApplication();

        gridView = (GridView) view.findViewById(R.id.gridView1);


        gridView.setOnItemClickListener(new OnItemClickListener() {
               public void onItemClick(AdapterView<?> parent, View v,
                 int position, long id) {


              android.support.v4.app.Fragment detail = new TheaterDetailFragment();
              android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
              fragmentManager.beginTransaction().add(R.id.content_frame, detail).addToBackStack("back").commit();
               }
              });



            new BackGround().execute();

        return view;
    }


public class BackGround extends AsyncTask<Void, Void, Void> {

        @Override
        protected Void doInBackground(Void... params) {

            filmTheater = JSONServices.getTheater();
            return null;
        }

        @Override
        /* check again */
        protected void onPostExecute(Void result) {

            commonVariable.setTheater(filmTheater);

            adapter = new LazyAdapter(filmTheater, activity,Element.THEATER_LIST.getType());

            gridView.setAdapter(adapter);

            super.onPostExecute(result);
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
        }

    }

} 

JSON服务类

package com.fortuna.cinemalk.service;

import java.util.ArrayList;
import java.util.LinkedHashMap;

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

import android.graphics.Paint.Join;
import android.widget.TextView;

import com.fortuna.cinemalk.model.BaseElement;
import com.fortuna.cinemalk.model.Film;
import com.fortuna.cinemalk.model.FilmBanner;
import com.fortuna.cinemalk.model.FilmCategory;
import com.fortuna.cinemalk.model.Home;
import com.fortuna.cinemalk.model.Theater;
import com.fortuna.cinemalk.util.JSONTag;
import com.fortuna.cinemalk.util.ServiceURL;

public class JSONServices {

    public static ArrayList<BaseElement> getCategory() {



        ArrayList<BaseElement> catogory = new ArrayList<BaseElement>();

        try {
            JSONObject jsonObject = new JSONObject(
                    CommonService.readJsonUrl(ServiceURL.CATEGORY_TOPCAT
                            .getUrl()));

            JSONArray array = jsonObject.getJSONArray(JSONTag.CATEGORY.getTag());
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObj = array.getJSONObject(i);

                FilmCategory filmCategory = new FilmCategory();

                filmCategory.setCatId(jsonObj.getString(JSONTag.ID.getTag()));
                filmCategory.setCategory(jsonObj.getString(JSONTag.NAME
                        .getTag()));

                catogory.add(filmCategory);

                // news.put(i, newsCategory);

            }

        } catch (JSONException e) {

            e.printStackTrace();
            return null;
        }

        return catogory;

    }

    public static ArrayList<BaseElement> getCategoryById(String id) {

        ArrayList<BaseElement> films = new ArrayList<BaseElement>();

        try {
            JSONObject jsonObject = new JSONObject(CommonService.readJsonFromUrl(ServiceURL.MOVE_BY_CATEGORY
                            .getUrl() + id));

            JSONArray array = jsonObject.getJSONArray(JSONTag.MOVIE.getTag());
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObj = array.getJSONObject(i);

                Film film = new Film();

                film.setActorsActress(jsonObj.getString(JSONTag.ACTOER_ACTRESS
                        .getTag()));
                film.setComposer(jsonObj.getString(JSONTag.COMPOSER.getTag()));
                film.setDescrition(jsonObj.getString(JSONTag.DESCRIPTION
                        .getTag()));
                film.setDirecter(jsonObj.getString(JSONTag.DIRECTOR.getTag()));
                film.setGenre(jsonObj.getString(JSONTag.GENERE.getTag()));
                film.setImage(jsonObj.getString(JSONTag.IMAGE_2.getTag()));
                film.setIMDbRating(jsonObj.getString(JSONTag.IMDB_RATING
                        .getTag()));
                film.setLength(jsonObj.getString(JSONTag.LENGTH.getTag()));
                film.setNewsId(jsonObj.getString(JSONTag.ID.getTag()));
                film.setPlot(jsonObj.getString(JSONTag.PLOT.getTag()));
                film.setProducer(jsonObj.getString(JSONTag.PRODUCER.getTag()));

                films.add(film);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return films;

    }

    public static LinkedHashMap<Integer, BaseElement> getNewsDescription(
            String id) {

        LinkedHashMap<Integer, BaseElement> news = new LinkedHashMap<Integer, BaseElement>();

        try {
            JSONObject jsonObject = new JSONObject(
                    CommonService
                            .readJsonFromUrl(ServiceURL.CATEGOR_DESCRIPTION
                                    .getUrl() + id));

            JSONArray array = jsonObject.getJSONArray(JSONTag.TOPNEWSGIVENCAT.getTag());
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObj = array.getJSONObject(i);

                Film newsDescription = new Film();

                newsDescription.setNewsId(jsonObj.getString(JSONTag.ID.getTag()));

                newsDescription.setTitle(jsonObj.getString(JSONTag.TITLE.getTag()));

                newsDescription.setShortDescription(jsonObj.getString(JSONTag.SHORTDESCRIPTION.getTag()));

                newsDescription.setDescrition(jsonObj.getString(JSONTag.DESCRIPTION.getTag()));

                newsDescription.setThumImage(jsonObj.getString(JSONTag.THUMBNAIL.getTag()));

                newsDescription.setImage(jsonObj.getString(JSONTag.IMAGE.getTag()));

                newsDescription.setTrailerImage(jsonObj.getString(JSONTag.TRAILER_IMAGE.getTag()));

                newsDescription.setVideo(jsonObj.getString(JSONTag._VIDEO.getTag()));

                news.put(i, newsDescription);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return news;

    }

    public static LinkedHashMap<Integer, BaseElement> getHomeContent() {

        LinkedHashMap<Integer, BaseElement> news = new LinkedHashMap<Integer, BaseElement>();

        try {
            JSONObject jsonObject = new JSONObject(
                    CommonService.readJsonFromUrl(ServiceURL.HOME_CONTENT
                            .getUrl()));

            JSONArray array = jsonObject.getJSONArray("home");
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObj = array.getJSONObject(i);

                Home home = new Home();

                JSONArray homeArray = jsonObj.getJSONArray("photos");

                LinkedHashMap<Integer, BaseElement> banner = new LinkedHashMap<Integer, BaseElement>();

                for (int j = 0; j < homeArray.length(); j++) {

                    JSONObject jsonObject2 = homeArray.getJSONObject(j);

                    FilmBanner filmBanner = new FilmBanner();

                    filmBanner.setId(jsonObject2.getString(JSONTag.ID.getTag()));
                    filmBanner.setTitle(jsonObject2.getString(JSONTag.TITLE.getTag()));
                    filmBanner.setDescription(jsonObject2.getString(JSONTag.SHORTDESCRIPTION.getTag()));
                    filmBanner.setImage(jsonObject2.getString(JSONTag.IMAGE.getTag()));
                    filmBanner.setThumb(jsonObject2.getString(JSONTag.THUMBNAIL.getTag()));
                    filmBanner.setType(jsonObject2.getString(JSONTag.TYPE.getTag()));

                    banner.put(j, filmBanner);

                }

                home.setCategory(jsonObj.getString(JSONTag.CATEGORY.getTag()));
                home.setImages(banner);

                news.put(i, home);

            }

        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return news;

    }

    public static ArrayList<BaseElement> getTheater() {

        ArrayList<BaseElement> theaters = new ArrayList<BaseElement>();

        try {
            JSONObject jsonObject = new JSONObject(
                    CommonService.readJsonUrl(ServiceURL.THEATER_CONTENT
                            .getUrl()));

            JSONArray array = jsonObject.getJSONArray("theater");
            for (int i = 0; i < array.length(); i++) {

                JSONObject jsonObj = array.getJSONObject(i);

                Theater theater = new Theater();

                theater.setTheaterId(jsonObj.getString(JSONTag.ID.getTag()));
                theater.setTheaterImage(jsonObj.getString(JSONTag.IMAGE.getTag()));
                theater.setName(jsonObj.getString(JSONTag.NAME.getTag()));

                theaters.add(theater);


}

        } catch (JSONException e) {

            e.printStackTrace();
            return null;
        }

        return theaters;

    }

}

记录猫

08-12 13:58:55.241: E/AndroidRuntime(11709): FATAL EXCEPTION: AsyncTask #1
08-12 13:58:55.241: E/AndroidRuntime(11709): java.lang.RuntimeException: An error occured while executing doInBackground()
08-12 13:58:55.241: E/AndroidRuntime(11709):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.lang.Thread.run(Thread.java:1096)
08-12 13:58:55.241: E/AndroidRuntime(11709): Caused by: java.lang.NullPointerException
08-12 13:58:55.241: E/AndroidRuntime(11709):    at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:112)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at org.json.JSONTokener.nextValue(JSONTokener.java:90)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at org.json.JSONObject.<init>(JSONObject.java:154)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at org.json.JSONObject.<init>(JSONObject.java:171)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at com.fortuna.cinemalk.service.JSONServices.getTheater(JSONServices.java:210)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at com.fortuna.cinemalk.TheaterFragment$BackGround.doInBackground(TheaterFragment.java:75)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at com.fortuna.cinemalk.TheaterFragment$BackGround.doInBackground(TheaterFragment.java:1)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
08-12 13:58:55.241: E/AndroidRuntime(11709):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-12 13:58:55.241: E/AndroidRuntime(11709):    ... 4 more
08-12 13:59:24.294: E/ActivityThread(11807): Failed to find provider info for com.samsung.sec.android.clockpackage
08-12 13:59:25.563: E/WindowManager(2512): Overwriting rotation value from 0
08-12 13:59:25.622: E/Launcher(2609): setWindowOpaque()
08-12 13:59:25.735: E/Launcher(2609): setWindowOpaque()
08-12 14:03:57.817: E/Finsky(3371): [1] SelfUpdateScheduler.onError: Self-update failed because of HTTP error code: 495
08-12 14:04:08.298: E/Launcher(2609): setWindowOpaque()

1 个答案:

答案 0 :(得分:1)

异常是NullPointerException并指向此语句:

JSONObject jsonObject = new JSONObject(
                CommonService.readJsonUrl(ServiceURL.THEATER_CONTENT
                        .getUrl()));

所以我想可能是你将null参数传递给JSONObject类的构造函数。尝试打印参数以查看其格式是否正确。