致命异常:主 - 应用程序崩溃一段时间后

时间:2013-12-12 16:07:46

标签: java android android-asynctask

很长一段时间我都面临着将xml解析为Fragment NavigationTabs的问题。当我运行我的项目时,它显示2-4secs,不查看我的列表视图(空白片段),而不是崩溃。我认为,onPostExecute方法有问题。我可以求你帮忙吗?

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());


    }

}


}

自定义适配器

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;


}

}

3 个答案:

答案 0 :(得分:0)

Context在传递到您的班级null时为clubsAdapter,在家长班ArrayAdaptor中使用NullPointerException并投出mContext。可能是ClubsDownloadTask的{​​{1}}成员。

<强>另外

课程应以国会大厦字母开头:ClubsAdapter而不是clubsAdapter

答案 1 :(得分:0)

将onCreateView更改为以下内容:

@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;

}

也会覆盖此功能:

@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);
      }   

}

问题在于onCreateView,getActivity返回null,因为此片段尚未附加到活动。

您需要覆盖onActivityCreated函数并将适配器设置为其中的列表。

答案 2 :(得分:0)

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

我修复了这个问题,只需将上面的代码添加到onPostExecute。