简体   繁体   中英

FATAL EXCEPTION: main - app crashes after a while

For a long time am I facing a problem with xml parsing into Fragment NavigationTabs. When i run my project it appears for 2-4secs, doesn't view my listview (blank fragment) and than crashes. I think, that there is something wrong with onPostExecute method. Can I beg you for help?

LogCat:

E/AndroidRuntime(473): FATAL EXCEPTION: main
E/AndroidRuntime(473): java.lang.NullPointerException
E/AndroidRuntime(473):  at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
12-12 15:53:59.730: E/AndroidRuntime(473):  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:150)
E/AndroidRuntime(473):  at com.example.premierleague.clubsAdapter.<init>(clubsAdapter.java:37)
E/AndroidRuntime(473):  at com.example.premierleague.Fragment1$ClubsDownloadTask.onPostExecute(Fragment1.java:113)
E/AndroidRuntime(473):  at com.example.premierleague.Fragment1$ClubsDownloadTask.onPostExecute(Fragment1.java:1)
E/AndroidRuntime(473):  at android.os.AsyncTask.finish(AsyncTask.java:417)
E/AndroidRuntime(473):  at android.os.AsyncTask.access$300(AsyncTask.java:127)
12-12 15:53:59.730: E/AndroidRuntime(473):  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
E/AndroidRuntime(473):  at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(473):  at android.os.Looper.loop(Looper.java:123)
E/AndroidRuntime(473):  at android.app.ActivityThread.main(ActivityThread.java:4627)
E/AndroidRuntime(473):  at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(473):  at java.lang.reflect.Method.invoke(Method.java:521)
E/AndroidRuntime(473):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
E/AndroidRuntime(473):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
E/AndroidRuntime(473):  at dalvik.system.NativeStart.main(Native Method)

Fragment1.java

public class Fragment1 extends Fragment {
MainActivity activity = new MainActivity();
private clubsAdapter mAdapter;
private ListView clubs;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_1, container, false);
    Log.i("PremierLeague", "OnCreateView()");




    clubs = (ListView)rootView.findViewById(R.id.clubsList);


    clubs.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }

    });

    if(isNetworkAvailable()){
        Log.i("PremierLeague", "starting download Task");
        ClubsDownloadTask download = new ClubsDownloadTask();
        download.execute();
    }else{
        mAdapter = new clubsAdapter(getActivity().getApplicationContext(), -1, 
                ClubsXmlPullParser.getItemsFromFile(getActivity()));
        clubs.setAdapter(mAdapter);
    }   


    return super.onCreateView(inflater, container, savedInstanceState);

}





//Helper method to determine if Internet connection is available.
private boolean isNetworkAvailable() {
    ConnectivityManager connectivityManager
          = (ConnectivityManager) getActivity().getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo activeNetworkInfo = connectivityManager.getActiveNetworkInfo();
    return activeNetworkInfo != null;





}

private class ClubsDownloadTask extends AsyncTask<Void, Void, Void>{
    private clubsAdapter mAdapter;
    private List<LeagueClub> clubs;
    Fragment1 ctx;
    Context context = getActivity();
    private Context mContext;


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



        try {
            Downloader.DownloadFromUrl("http://dl.dropboxusercontent.com/s/h2qc41k2yy3c1ir/clubs.xml", getActivity().openFileOutput("clubs.xml", Context.MODE_PRIVATE));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;

    }

    protected void onPostExecute(Void result){
        mAdapter = new clubsAdapter(mContext, -1, ClubsXmlPullParser.getItemsFromFile(getActivity()));
        ((ListView) clubs).setAdapter(mAdapter);
        Log.i("PremierLeague", "adapter size = "+ mAdapter.getCount());


    }

}


}

custom adapter

public class clubsAdapter extends ArrayAdapter<LeagueClub> {

ImageLoader imageLoader;
DisplayImageOptions options;



@SuppressWarnings("deprecation")
public clubsAdapter(Context class1, int textViewResourceId, List<LeagueClub> clubs) {
    super(class1, textViewResourceId, clubs);

    //Setup the ImageLoader, we'll use this to display our images
    ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(class1).build();
    imageLoader = ImageLoader.getInstance();
    imageLoader.init(config);

    //Setup options for ImageLoader so it will handle caching for us.
    options = new DisplayImageOptions.Builder()
    .cacheInMemory()
    .cacheOnDisc()
    .build();


}



    // TODO Auto-generated constructor stu


/*
 * (non-Javadoc)
 * @see android.widget.ArrayAdapter#getView(int, android.view.View, android.view.ViewGroup)
 * 
 * This method is responsible for creating row views out of a StackSite object that can be put
 * into our ListView
 */
@Override
public View getView(int pos, View convertView, ViewGroup parent){
    RelativeLayout row = (RelativeLayout)convertView;
    Log.i("PremierLeague", "getView pos = " + pos);
    if(null == row){
        //No recycled View, we have to inflate one.
        LayoutInflater inflater = (LayoutInflater)parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        row = (RelativeLayout)inflater.inflate(R.layout.row_site, null);
    }

    //Get our View References
    final ImageView clubLogo = (ImageView)row.findViewById(R.id.clubLogo);
    TextView nameTxt = (TextView)row.findViewById(R.id.nameTxt);
    TextView aboutTxt = (TextView)row.findViewById(R.id.aboutTxt);
    TextView stadiumTxt = (TextView)row.findViewById(R.id.stadiumTxt);
    final ProgressBar indicator = (ProgressBar)row.findViewById(R.id.progress);

    //Initially we want the progress indicator visible, and the image invisible
    indicator.setVisibility(View.VISIBLE);
    clubLogo.setVisibility(View.INVISIBLE);

    //Setup a listener we can use to switch from the loading indicator to the Image once it's ready
    ImageLoadingListener listener = new ImageLoadingListener(){



        @Override
        public void onLoadingStarted(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingCancelled(String arg0, View arg1) {
            // TODO Auto-generated method stub

        }

        @Override
        public void onLoadingComplete(String arg0, View arg1, Bitmap arg2) {
            indicator.setVisibility(View.INVISIBLE);
            clubLogo.setVisibility(View.VISIBLE);
        }

        @Override
        public void onLoadingFailed(String arg0, View view, FailReason arg2) {
            indicator.setVisibility(View.INVISIBLE);
            ImageView imageView = (ImageView) view.findViewById(R.id.clubLogo);
            imageView.setVisibility(View.VISIBLE);

        }

    };

    //Load the image and use our options so caching is handled.
    imageLoader.displayImage(getItem(pos).getLogo(), clubLogo,options, listener);

    //Set the relevant text in our TextViews
    nameTxt.setText(getItem(pos).getName());
    aboutTxt.setText(getItem(pos).getAbout());
    stadiumTxt.setText(getItem(pos).getStadium());



    return row;


}

}

The Context is null when passed to your class clubsAdapter where it is used in the parent class ArrayAdaptor and throws a NullPointerException . Possibly the mContext member of ClubsDownloadTask .

Additionally

Classes should start with a capitol letter: ClubsAdapter not clubsAdapter

Change your onCreateView to following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_1, container, false);
    Log.i("PremierLeague", "OnCreateView()");




    clubs = (ListView)rootView.findViewById(R.id.clubsList);


    clubs.setOnItemClickListener(new OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
                long arg3) {
            // TODO Auto-generated method stub

        }

    });



    return rootView;

}

also override this function:

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
      if(isNetworkAvailable()){
          Log.i("PremierLeague", "starting download Task");
          ClubsDownloadTask download = new ClubsDownloadTask();
          download.execute();
      }else{
          mAdapter = new clubsAdapter(getActivity().getApplicationContext(), -1, 
                  ClubsXmlPullParser.getItemsFromFile(getActivity()));
          clubs.setAdapter(mAdapter);
      }   

}

The problem is in onCreateView getActivity returns null because this fragment is not yet attached to an activity.

You need to override onActivityCreated function and set adapter to your list in it.

ListView clubs = (ListView) getView().findViewById(R.id.clubsList);

我通过将上面的代码添加到onPostExecute来解决了该问题。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM