每次我尝试在片段中的OnActivityCreated方法中设置Adapter时,应用程序都会崩溃

时间:2014-07-17 13:03:32

标签: android android-activity android-fragments nullpointerexception adapter

我已经尝试了几个小时,寻找解决方案和替代方案......但是setAdapter方法似乎不起作用...... 我只想在片段活动中显示自定义视图列表...

我的主要活动存在于一个级别的包中...并且这两个类位于主包中的包中...请帮助

这是我的Adapter类...我用来填充片段中的列表

 public class GamesAdapter extends ArrayAdapter<String> {

    private final Activity context;
    private final String[] sport;
    private final Integer[] imageId;

    public GamesAdapter(Activity context,String[] sport, Integer[] imageId) {
            super(context, R.layout.fragment_list, sport);
            this.context = context;
            this.sport = sport;
            this.imageId = imageId;
            }

    @Override
    public View getView(int position, View view, ViewGroup parent) {

        if (view == null) {
            LayoutInflater inflater = LayoutInflater.from(parent.getContext());
            view = inflater.inflate(R.layout.activity_list2, parent, false);
        }

        TextView titleTextView = (TextView) view.findViewById(R.id.textViewTitle);
        titleTextView.setText(sport[position]);

        ImageView thumbImg=(ImageView)view.findViewById(R.id.imageViewThumb);
        thumbImg.setImageResource(imageId[position]);
        ImageView nextImg=(ImageView) view.findViewById(R.id.imageViewNext);
        nextImg.setImageResource(R.drawable.next);

    return view;

    }
    }

这是'我的片段类....

public class FragmentGames extends Fragment {

 String[] sport = {
            "GameA",
              "GameB",
              "GameC",
              "GameD",
              "GameE",
              "GameF",
              "GameG"
          } ;
          Integer[] imageId = {
              R.drawable.athletics,
              R.drawable.badminton,
              R.drawable.bowls,
              R.drawable.boxing,
              R.drawable.clyde1,
              R.drawable.clyde1_circle,
              R.drawable.clyde2
          };
          ListView list;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.listview, container,false);

    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

     //I guess the next three lines is where the problem exists.....
     GamesAdapter adapter = new GamesAdapter(getActivity(), sport, imageId);
     list=(ListView) getView().findViewById(R.id.listView);
   list.setAdapter(adapter);


}

}

以下是必需的xml文件....

fragmet_list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

<ImageView
    android:id="@+id/imageViewThumb"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
   />

<TextView
    android:id="@+id/textViewTitle"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:layout_weight="0.91"

    android:paddingLeft="15dp"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<ImageView
    android:id="@+id/imageViewNext"
    android:layout_width="28dp"
    android:layout_height="28dp"
     />

</LinearLayout>

listview.xml

 <?xml version="1.0" encoding="utf-8"?>
   <ListView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/listView" >


     </ListView>

的logcat ::

 07-17 18:10:47.994: W/dalvikvm(736): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
 07-17 18:10:48.074: E/AndroidRuntime(736): FATAL EXCEPTION: main
 07-17 18:10:48.074: E/AndroidRuntime(736): java.lang.NullPointerException
 07-17 18:10:48.074: E/AndroidRuntime(736):     at  com.prokode.cwg2014.fragments.GamesAdapter.getView(GamesAdapter.java:39)

2 个答案:

答案 0 :(得分:1)

片段类

public class FragmentGames extends Fragment {


              ListView list;

            private ArrayList<MyModel> arrayList;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        View view = inflater.inflate(R.layout.listview, container,false);

        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
        initArrayList();

        MyAdapter adapter = new MyAdapter(arrayList);
        list.setAdapter(adapter);


    }

    private void initArrayList() {
        // TODO Auto-generated method stub


         arrayList.add(new MyModel("GameA",R.drawable.athletics));
        .
        .
        .

    }

    }

适配器类。

public class MyAdapter extends BaseAdapter {


    private Context context;

ArrayList<MyModel> arrayList;   


    public MyAdapter(ArrayList<MyModel> arrayList) {
        // TODO Auto-generated constructor stub
    this.arrayList = arrayList;
    }



    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        // TODO Auto-generated method stub
        return arrayList.get(position);
    }

    @Override
    public long getItemId(int position) {
        // TODO Auto-generated method stub
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

    if(convertView == null){
        LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());
        convertView=layoutInflater.inflate(R.layout.activity_list2, null);
    }
    MyModel myModel = arrayList.get(position);


    TextView titleTextView = (TextView) converView.findViewById(R.id.textViewTitle);
    titleTextView.setText(myModel.getSportName());

    ImageView thumbImg=(ImageView)convertView.findViewById(R.id.imageViewThumb);
    thumbImg.setImageResource(myModel.getImageId());
    ImageView nextImg=(ImageView) view.findViewById(R.id.imageViewNext);
    nextImg.setImageResource(R.drawable.next);

    return convertView;
    }

}

模型类

public class MyModel {
    String sportName;
    Integer imageId;

    public MyModel(String sportName, Integer imageId) {
        this.sportName = sportName;
        this.imageId = imageId;
    }

    public Integer getImageId() {
        return imageId;
    }

    public String getSportName() {
        return sportName;
    }

    public void setImageId(Integer imageId) {
        this.imageId = imageId;
    }

    public void setSportName(String sportName) {
        this.sportName = sportName;
    }

}

答案 1 :(得分:0)

好吧......修复了我的问题......只是在FragmentGames类中扩展了ListFragment ...而不是Fragment,然后删除了OnCreateView方法....瞧它像魅力一样...... 不知道为什么它有效但它起作用..... :)。

这是修改后的片段类....

import android.app.ListFragment;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;

public class FragmentGames extends ListFragment {

 String[] sport = {
              "GameA",
          "GameB",
          "GameC",
          "GameD",
          "GameE",
          "GameF",
          "GameG"
          } ;
          Integer[] imageId = {
              R.drawable.athletics,
              R.drawable.badminton,
              R.drawable.bowls,
              R.drawable.boxing,
              R.drawable.clyde1,
              R.drawable.clyde1_circle,
              R.drawable.clyde2
          };
          ListView list;





@Override
public void onActivityCreated(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onActivityCreated(savedInstanceState);

     GamesAdapter adapter = new GamesAdapter(getActivity(), sport, imageId);
     list=(ListView) getActivity().findViewById(R.id.listView);
     setListAdapter(adapter);


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



 }

我很感激对此的解释......