尝试使用适配器将自定义ListView添加到片段?

时间:2014-03-25 15:45:02

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

我尝试将自定义列表视图添加适配器添加到片段。 我不知道问题究竟在哪里,我想我已经完成了所有工作,但我无法摆脱不幸的停止消息。 我使用了调试并知道它崩溃的地方;

Fragment 类中的

for (int i = 0;i<4;i++){ . . . questions.add(question);

当调试到达此语句时,它结束。

片段

import java.util.List;

import code.me.R.id;
import android.R.layout;
import android.app.Application.ActivityLifecycleCallbacks;
import android.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

@SuppressLint("NewApi")
public class Fragment1 extends Fragment 
{
    ListView lv;
    List<Question> questions;
    Integer[] Q_Id ;
    String[] AskerUserName =   {"Jack"      ,"John"    ,"Lio"      ,"Sam"         ,"Mike"        };
    String[] AnswererUserName ={"Jacob"     ,"Mario"   ,"Tom"      ,"Shon"        ,"Jasmine"     };
    String[] Qusetion =        {"What?"     ,"Where?"  ,"When"     ,"How?"        ,"Who?"        };
    String[] Answer =          {"jjjjjjjjjj","llllllll","fffffffff","eeeeeeeeeeee","oooooooooooo"};

    @Override
    public View onCreateView( LayoutInflater inflater, 
        ViewGroup container,
        Bundle savedInstanceState )
    {
        for (int i = 0;i<4;i++){
            int qid =  i;
            String AUName = AskerUserName[i].toString();
            String AnUName = AnswererUserName[i].toString();
            String Ans = Answer[i].toString();
            String Ques = Qusetion[i].toString();
            Question question = new Question(qid, AUName, AnUName, Ans, Ques);
            questions.add(question);


        }
        View view = inflater.inflate(R.layout.frag1, container, false);
        QuestionsAdapter questad = new QuestionsAdapter(getActivity(),questions);
        lv = (ListView) view.findViewById(id.listView1);
        lv.setAdapter(questad);


        return inflater.inflate( R.layout.frag1, 
            container, false );
    }
}

适配器

    import java.util.List;



import code.me.R.id;
import android.R.layout;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class QuestionsAdapter extends BaseAdapter {

    private final Activity context;
    public List<Question> questions;
    LayoutInflater inflater;


    public QuestionsAdapter(Activity context,List<Question> qu) {


        this.context = context;
        this.questions = qu;



        // TODO Auto-generated constructor stub
    }


    @Override
    public View getView(int position, View view , ViewGroup parent){
        View rowView = view;
        if(view == null)
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        rowView = inflater.inflate(R.layout.question_answer_item, null,false);
        TextView askerUserName = (TextView) view.findViewById(id.askerUsername);
        askerUserName.setText(questions.get(position).getAskerUserName().toString());
        TextView question = (TextView) view.findViewById(id.Question);
        question.setText(questions.get(position).getQuestion().toString());
        TextView ratingView = (TextView) view.findViewById(id.RatingAsker);
        //ratingView.setText(questions.get(position)._RatingU);
        TextView ratingAnswerer = (TextView) view.findViewById(id.RatingAnswerer);
        //ratingAnswerer.setText(questions.get(position).getRatingView());

        return rowView;
    }


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


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


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



}

Class_Question

    public class Question {

    private

     String _Answer ;
     String _AnswererUserName;
     String _AskerUserName ;
     String _Question ;
     Integer _QId;
     Integer _RatingU;
     Integer _RatingQ;

    public Question (int Q_Id,String AUName,String AnUName,String  Ans,String Ques){
        _QId = Q_Id;
        _AnswererUserName = AnUName;
        _AskerUserName = AUName;
        _Answer = Ans;
        _Question = Ques;
    }
    public String getAnswer() {
        // TODO Auto-generated method stub
        return _Answer;
    }


    public String getAnswererUserName() {
        // TODO Auto-generated method stub
        return _AnswererUserName;
    }


    public String getAskerUserName() {
        // TODO Auto-generated method stub
        return _AskerUserName;
    }

    public String getQuestion() {
        // TODO Auto-generated method stub
        return _Question;
    }


    public int getRatingView() {
        // TODO Auto-generated method stub
        return _RatingU;
    }


    public void setRatingQ() {
        // TODO Auto-generated method stub

    }

    public int getRatingQ() {
        // TODO Auto-generated method stub
        return _RatingQ;
    }
}

2 个答案:

答案 0 :(得分:1)

您永远不会初始化片段中的List<Question> questions

答案 1 :(得分:0)

您永远不会初始化您的问题集。这就是为什么当你尝试在onCreateView中添加问题时它会停止。在您尝试向其添加内容之前,最初或初始化List问题。