Android ListView.setAdapter(android.widget.ListAdapter)'在null对象引用上

时间:2017-07-29 16:02:38

标签: android android-arrayadapter

我创建了一个Android应用程序,我尝试使用自定义ListView。所有数据都来自我的休息服务,我已经检查过这些值是否在我的列表中。但是当我设置我的适配器时,我有这个错误:

07-29 15:56:00.357 6552-6552/ejplanningandroid.ejplanningandroid W/System.err: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
07-29 15:56:00.357 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at ejplanningandroid.ejplanningandroid.PlanningFragment.onCreateView(PlanningFragment.java:68)
07-29 15:56:00.357 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.Fragment.performCreateView(Fragment.java:1974)
07-29 15:56:00.357 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:517)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.os.Handler.handleCallback(Handler.java:739)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.os.Handler.dispatchMessage(Handler.java:95)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.os.Looper.loop(Looper.java:148)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at android.app.ActivityThread.main(ActivityThread.java:5417)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at java.lang.reflect.Method.invoke(Native Method)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
07-29 15:56:00.358 6552-6552/ejplanningandroid.ejplanningandroid W/System.err:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

但我初始化我的适配器看看我的片段:

PlanningFragment:

public class PlanningFragment extends Fragment {
    private ListView mListView;
    private View view;
    private List<Planning> listPlanning = new ArrayList<Planning>();
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.menu_fragment, container, false);
        mListView = (ListView) view.findViewById(R.id.listViewPlanning);

        String path = "http://192.168.128.13:8081/EJPlanningRest-Colart/planning/moniteur?login="+
                ((MainActivity) getActivity()).getUtilisateur().getLogin()
                +"&password="+((MainActivity) getActivity()).getUtilisateur().getMotDepasse();
        try {

        JsonArrayTask JsonArray = (JsonArrayTask) new JsonArrayTask().execute(path);
            if(JsonArray.get() != null) {
                for (int i = 0; i < JsonArray.get().length(); i++) {
                    Log.i("ICI",""+JsonArray.get().length());
                    Planning planning = new Planning();
                    planning.setDate(JsonArray.get().getJSONObject(i).getString("dateEvenement"));
                    planning.setType(JsonArray.get().getJSONObject(i).getString("Type"));
                    JSONObject json_data = JsonArray.get().getJSONObject(i);
                    JSONArray jsonArrayActivitePlanning = json_data.getJSONArray("ActivitePlannings");
                    JSONArray jsonArrayUtilisateurPlannings = json_data.getJSONArray("UtilisateurPlannings");
                    JSONObject Groupe =json_data.getJSONObject("Groupe");
                    planning.setNomGroupe(Groupe.getString("NomGroupe"));
                    if(jsonArrayActivitePlanning != null || jsonArrayUtilisateurPlannings != null ) {
                        List<String> NomActivite = new ArrayList<String>();
                        List<String> NomUtilisateur = new ArrayList<String>();
                        for (int j = 0; j < jsonArrayActivitePlanning.length(); j++) {
                            NomActivite.add(jsonArrayActivitePlanning.getJSONObject(j).getString("nomActivite"));
                        }
                        for (int j = 0; j < jsonArrayUtilisateurPlannings.length(); j++) {
                            NomUtilisateur.add(jsonArrayUtilisateurPlannings.getJSONObject(j).getString("nomUtilisateur"));
                        }
                        planning.setNomActivite(NomActivite);
                        planning.setUtilisateurs(NomUtilisateur);
                    }
                    listPlanning.add(planning);
                }
                Log.i("Count",""+listPlanning.size());
                PlanningAdapter adapter = new PlanningAdapter(view.getContext(),listPlanning);
                mListView.setAdapter(adapter);
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return view;
    }

PlanningViewHolder:

public class PlanningViewHolder {
    public TextView Date;
    public TextView NomGroupe;
    public TextView Type;
    public TextView NomActivite;
    public TextView Utilisateur;

}

我的班级是我创建适配器的地方。没有什么复杂但我不知道为什么我有一个nullException ......

public class PlanningAdapter extends ArrayAdapter<Planning> {

    public PlanningAdapter (Context context, List<Planning> plannings){
        super(context, 0, plannings);
    }
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if(view == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.rowplanning,parent, false);
        }
        PlanningViewHolder planningViewHolder = (PlanningViewHolder)view.getTag();
        if(planningViewHolder==null){
            planningViewHolder= new PlanningViewHolder();
            planningViewHolder.Date = (TextView)view.findViewById(R.id.Date);
            planningViewHolder.NomActivite =(TextView)view.findViewById(R.id.Activite);
            planningViewHolder.NomGroupe = (TextView)view.findViewById(R.id.NomGroupe);
            planningViewHolder.Type = (TextView)view.findViewById(R.id.Type);
            planningViewHolder.Utilisateur = (TextView)view.findViewById(R.id.Utilisateur);
            view.setTag(planningViewHolder);
        }
        Planning planning = getItem(position);
        planningViewHolder.Type.setText(planning.getType());
        planningViewHolder.NomGroupe.setText(planning.getNomGroupe());
        String nomActivite = setStringFromArray(planning.getNomActivite());
        planningViewHolder.NomActivite.setText(nomActivite);
        planningViewHolder.Date.setText(planning.getDate().toString());
        String nomUtilisateur =setStringFromArray(planning.getUtilisateurs());
        planningViewHolder.Utilisateur.setText(nomUtilisateur);
        return view;
    }

    public String setStringFromArray(List<String> Array ){
        String result="";
        for (String nomGroupePlanning : Array){
            if(nomGroupePlanning !="") {
                result += "-" + nomGroupePlanning;
            }else{
                result += nomGroupePlanning;
            }
        }
        return result;
    }
}

ROWPLANNING XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="10dp" >
    <TextView
        android:id="@+id/Date"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_marginLeft="10dp"
        android:textStyle="bold"
        tools:text="Date" />
    <TextView
        android:id="@+id/NomGroupe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/Date"
        android:layout_marginLeft="10dp"
        android:textStyle="bold"
        tools:text="Groupe" />
    <TextView
        android:id="@+id/Type"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/NomGroupe"
        android:layout_marginLeft="10dp"
        android:textStyle="bold"
        tools:text="Type" />
    <TextView
        android:id="@+id/Activite"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/Type"
        android:layout_marginLeft="10dp"
        android:textStyle="bold"
        tools:text="Activite"
 />
    <TextView
        android:id="@+id/Utilisateur"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textColor="@android:color/black"
        android:layout_below="@+id/Activite"
        android:layout_marginLeft="10dp"
        android:textStyle="bold"
        tools:text="Utilisateur"
        />
</RelativeLayout>

FRAGMENTPLANNING XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ListView
        android:id="@+id/listViewPlanning"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:1)

你写过

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

但您的布局名称(menu_fragment)与您在问题中指定的名称不同,即FRAGMENTPLANNING,将其更改为正确的xml名称。

答案 1 :(得分:-1)

将此PlanningViewHolder planningViewHolder对象设为私有字段,并使用从活动传递到适配器的上下文。

public View getView(int position, View view, ViewGroup parent) {
        if(view == null){
    LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            view = mInflater.inflate(R.layout.rowplanning,parent, false);

            planningViewHolder= new PlanningViewHolder();
            planningViewHolder.Date = (TextView)view.findViewById(R.id.Date);
            planningViewHolder.NomActivite =(TextView)view.findViewById(R.id.Activite);
            planningViewHolder.NomGroupe = (TextView)view.findViewById(R.id.NomGroupe);
            planningViewHolder.Type = (TextView)view.findViewById(R.id.Type);
            planningViewHolder.Utilisateur = (TextView)view.findViewById(R.id.Utilisateur);

            view.setTag(planningViewHolder);

        } else {
         planningViewHolder = (PlanningViewHolder)view.getTag();
        }

        Planning planning = getItem(position);
        planningViewHolder.Type.setText(planning.getType());
        planningViewHolder.NomGroupe.setText(planning.getNomGroupe());
        String nomActivite = setStringFromArray(planning.getNomActivite());
        planningViewHolder.NomActivite.setText(nomActivite);
        planningViewHolder.Date.setText(planning.getDate().toString());
        String nomUtilisateur =setStringFromArray(planning.getUtilisateurs());
        planningViewHolder.Utilisateur.setText(nomUtilisateur);

        return view;
    }