ListView不适用于片段

时间:2014-04-01 17:04:12

标签: android android-layout listview android-fragments android-listview

我的ListView有一个小问题,它在我使用Activity时有效,但当我切换到片段时它不起作用。

以下是代码:

package com.groupex.tp_mobile;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.res.Resources;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView.FindListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.zip.Inflater;


public class Fragment1 extends Fragment implements OnItemClickListener{

ListView listView;
communicator communic;
String[] nameMenu;
String[] menuDescription;
int[] images={R.drawable.pizza, R.drawable.frenchfries, R.drawable.fishsoup, R.drawable.tomatosoup};

public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view=inflater.inflate(R.layout.fragment1, container, false);
    listView=(ListView) view.findViewById(R.id.menu1);
    Resources resources=getResources();
    nameMenu=resources.getStringArray(R.array.nameMenu);
    menuDescription=resources.getStringArray(R.array.menuDescription);
    CustomAdapter adapter=new CustomAdapter(container.getContext(), nameMenu, images, menuDescription);
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(this);

    return view;
}

class CustomAdapter extends ArrayAdapter<String>{
    Context context;
    int[] imags;
    String[] nameMenuArray;
    String[] menuDescriptionArray;

    CustomAdapter(Context c, String[] titles, int imgs[], String[] descri){
        super(c, R.layout.singlerow, R.id.textViewNom, titles);
        this.context=c;
        this.imags=imgs;
        this.nameMenuArray=titles;
        this.menuDescriptionArray=descri;
    }


    class MyViewHolder{
            ImageView MyImage;
            TextView MyTitle;
            TextView MyDescription;
            public MyViewHolder(View view) {
                // TODO Auto-generated constructor stub
                MyImage=(ImageView)view.findViewById(R.id.images);
                MyTitle=(TextView)view.findViewById(R.id.textViewNom);
                MyDescription=(TextView)view.findViewById(R.id.textViewFonction);

            }
        }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub
        View row=convertView;//On recuere le relative layout
        MyViewHolder holder=null;
        if(row==null){
            LayoutInflater inflater=(LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            row=inflater.inflate(R.layout.singlerow, parent, false);
            holder=new MyViewHolder(row);
            //We store the holder in the row by using setTag(Object)
            row.setTag(holder);
            android.util.Log.d("Dehii", "New row has been created");
        }
        //Recyclage.
        else{
            holder=(MyViewHolder) row.getTag();
            android.util.Log.d("Dehii", "Recycling a row");
        }
        holder.MyImage.setImageResource(imags[position]);
        holder.MyTitle.setText(nameMenuArray[position]);
        holder.MyDescription.setText(menuDescription[position]);
        return row;
    }
}


@Override
public void onItemClick(AdapterView<?> arg0, View view, int i, long l) {
    // TODO Auto-generated method stub
}

public void setCommunicator(communicator commun){
    this.communic=commun;
}

public interface communicator{
    public void respond(int index);
}

} // End class definition

以下是错误消息:

FatalException: NullPointerException

Fragment.customAdapter.$CustomView(Fragment1.java:104)
.
.
.
.
.
android.widget.ListView.OnMeasure(ListView java:1175)

我还没有足够的声誉来添加图片。

感谢您的帮助。

1 个答案:

答案 0 :(得分:2)

为什么不使用ListFragment?

public class MyListFragment extends ListFragment {

  @Override
  public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
        "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
        "Linux", "OS/2" };
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
        android.R.layout.simple_list_item_1, values);
    setListAdapter(adapter);
  }

  @Override
  public void onListItemClick(ListView l, View v, int position, long id) {
    // do something with the data
  }
}