0

私は 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

4 に答える 4

0

電話してはいけない

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

onCreateView


onViewCreated後に呼び出され、ドキュメントonCreateViewに従って

ただし、この時点では、フラグメントのビュー階層はその親に関連付けられていません。

于 2013-08-08T10:32:00.580 に答える