getActivity()使上下文等于null

时间:2013-08-08 10:13:33

标签: android android-fragments constructor android-context

我开发并推出了Android应用程序。它在我的Android手机上工作正常,但它在几台设备上都崩溃了。

*BugSense gives me the following error:*
0   java.lang.NullPointerException

1   at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)

2   at android.widget.ArrayAdapter.(ArrayAdapter.java:128)

3   **at com.challenger.app.ChallengeAdapter.(ChallengeAdapter.java:27)**

4   at com.challenger.app.AllChallenges.fitChallenges(AllChallenges.java:142)

5   at com.challenger.app.AllChallenges$1.onSuccess(AllChallenges.java:108)



import java.util.ArrayList;
import java.util.List;

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

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.AdapterView.OnItemClickListener;
    import android.widget.Button;
    import android.widget.ListView;
    import android.widget.TextView;

    import com.loopj.android.http.JsonHttpResponseHandler;
    import com.loopj.android.http.RequestParams;

    public class AllChallenges extends Fragment {


    Button loginButton;
    ListView listView2, listView1;

    TextView textView;
    int category = 0;
    View view;
    List<Challenge> challenge_data;

    public AllChallenges(int cat) {
        category = cat;
    }

    public AllChallenges() {
        // TODO Auto-generated constructor stub
    }

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        if(getActivity()!=null){
            load();
        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub

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

        if(getActivity()!=null){
            load();
        }

        return view;
    }

    public void load() {
        loadOld();

        if (!AppSettings.refreshed[category + 1]) {
            update();
        }

    }

    public void loadOld() {

        Log.d("Challenge", "Updating " + category);
        JSONArray a = AppSettings.loadChallenges(category, getActivity());
        if (a != null) {

            fitChallenges(makeArray(a));
        }

    }

    public void update() {
        Log.d("Challenge", "getChallenges/" + category);
        RequestParams a = new RequestParams();
        if (AppSettings.logged) {
            a.put("fb_id", AppSettings.facebookId);
            a.put("fb_authkey", AppSettings.facebookAuthToken);
        }
        NetworkClient.receiveJSON("getChallenges/" + category, a,
                new JsonHttpResponseHandler() {

                    @Override
                    public void onSuccess(JSONArray chalList) {
                        Log.d("Challenge", "Downloaded " + category);
                        AppSettings.saveChallenges(category, chalList,
                                getActivity());
                        fitChallenges(makeArray(chalList));
                        AppSettings.refreshed[category + 1] = true;
                    }
                });

    }

    public Challenge[] makeArray(JSONArray list) {
        challenge_data = new ArrayList<Challenge>();
        try {
            for (int i = 0; i < list.length(); i++) {
                JSONObject chal = (JSONObject) list.get(i);
                String[] arr = {};
                if (chal.has("friends"))
                    arr = JSONArrToArr(chal.getJSONArray("friends"));

                Log.e("Boolean", chal.getString("active"));

                challenge_data.add(new Challenge(chal.getInt("id"), chal
                        .getString("title"), chal.getInt("category"), chal
                        .getInt("taken_by"), arr, chal.getBoolean("active"),
                        chal.getString("text"), chal.getInt("streak"), chal
                                .getInt("percentage")));

            }
            return challenge_data.toArray(new Challenge[challenge_data.size()]);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
    }

    public void fitChallenges(Challenge[] chalArray) {
        **ChallengeAdapter adapter = new ChallengeAdapter(getActivity(),
                R.layout.list_row, chalArray);** //
        listView1 = (ListView) view.findViewById(R.id.listView1);
        listView1.setAdapter(adapter);

        OnItemClickListener listener = new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {

                Log.i("listener", "am ID" + id + "position " + position);
                int n = (int) id;
                String title = challenge_data.get(n).toStringArray()[1];
                Log.i("listener", "am ID" + id + "position " + position
                        + "  title " + title);
                int people = challenge_data.get(n).peopleInt;
                String category = challenge_data.get(n).toStringArray()[0];
                String description = challenge_data.get(n).toStringArray()[5];
                int streak = challenge_data.get(n).streak;
                int percentage = challenge_data.get(n).percentage;
                int chal_id = challenge_data.get(n).id;
                boolean active = challenge_data.get(n).active;

                ((AllChallengesPager) getActivity()).showDetailed(chal_id,
                        title, "" + people, category, active, description, ""
                                + streak, "" + percentage);
            }
        };

        listView1.setOnItemClickListener(listener);

    }

    public String[] JSONArrToArr(JSONArray arr) {
        String[] newArr = {};
        try {
            List<String> list = new ArrayList<String>();
            for (int i = 0; i < arr.length(); i++) {

                list.add(arr.getString(i));

            }
            newArr = list.toArray(new String[list.size()]);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        return newArr;
    }}

public class ChallengeAdapter extends ArrayAdapter<Challenge> {

    Context context;
    int layoutResourceId;
    Challenge data[] = null;
    Typeface a;

    public ChallengeAdapter(Context context, int layoutResourceId,
            Challenge[] data) {

        **super(context, layoutResourceId, data);** //in this line code crashes

        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
        a = Typeface.createFromAsset(context.getAssets(),
                "fonts/roboto_condensed.ttf");

    }


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ChallengeHolder holder = null;

        Challenge challenge = data[position];

        if (row == null) {

            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ChallengeHolder();
            holder.imgIcon = (ImageView) row.findViewById(R.id.list_image);
            holder.txtTitle = (TextView) row.findViewById(R.id.challenge);
            holder.txtPeople = (TextView) row.findViewById(R.id.peopleNumber);
            holder.friendThumbs = (LinearLayout) row
                    .findViewById(R.id.friendThumbs);

            row.setTag(holder);
        } else {
            holder = (ChallengeHolder) row.getTag();
        }
        holder.friendThumbs.removeAllViews();
        for (int i = 0; i < challenge.friendIds.length; i++) {
            holder.friendThumbs.addView(createIcon(challenge.friendIds[i],
                    context));
        }

        holder.txtTitle.setText(challenge.title);
        holder.txtTitle.setTypeface(a);
        holder.imgIcon.setImageResource(challenge.icon);
        holder.txtPeople.setText(challenge.peopleString);

        return row;
    }

    static class ChallengeHolder {
        public LinearLayout friendThumbs;
        ImageView imgIcon;
        TextView txtTitle;
        TextView txtPeople;

    }

    public ImageView createIcon(String friendID, Context cont) {
        ImageView icon = new ImageView(context);
        LinearLayout.LayoutParams par = new LinearLayout.LayoutParams(
                (int) cont.getResources().getDimension(R.dimen.fbIconSize),
                (int) cont.getResources().getDimension(R.dimen.fbIconSize));
        par.setMargins(0, 0, 15, 0);
        icon.setLayoutParams(par);

        Log.e("ID " + friendID, "log");

        final String myurl = AppSettings.profilePictureUrl(friendID);
        icon.setImageResource(R.drawable.default_fb_icon_small);
        ((MyApplication) ((Activity) context).getApplication())
                .loadImageSimple(myurl, icon);

        return icon;

    }
}

我猜这个问题的原因是从getActivity方法得到的上下文等于null,但是不知道如何处理这个问题。任何人都可以解释如何解决问题吗?

4 个答案:

答案 0 :(得分:1)

我认为onActivityCreated方法会覆盖并实现此代码,其工作正常可能是......

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    if(getActivity()!=null){
        load();
    }
}

Fragment lifecyle必须阅读此文档Documentation

答案 1 :(得分:0)

你不应该打电话

if(getActivity()!=null){
     load();
}
onCreateView中的


onViewCreatedonCreateView之后和docs

之后调用
  

然而,片段的视图层次结构不会附加到其父级   在这一点上。

答案 2 :(得分:0)

在这个阶段,你的片段很可能与你的活动分离,因为这是在外部线程JsonHttpResponseHandler中调用的。

你在这里有一个很好的解释: https://stackoverflow.com/a/11536337/891479

答案 3 :(得分:-1)

我想如果我没记错的话,最好使用getActivity().getApplicationContext()

相关问题