2 sinat 35670989 sinat_35670989 于 2016.09.17 10:50 提问

关于fragment添加listView的问题,

我在Fragment中添加listView,listView需要从网络获取数据并解析,开启一个异步进行执行,然后要添加一个头布局,是viewpager,也是开启一个异步任务去执行,可不可以这样做呢,我把Fragment完全当做一个activity去用,出现闪退

//这是mainActivity
 public class Welcome extends AppCompatActivity {
    private ViewPager viewPager_vp;

    private ImageView image_iv;
    private RadioGroup group;
    private RadioButton[] buttons;
    private List<Fragment> list=new ArrayList<Fragment>();
    FragmentManager manager;
    MyAdapter my;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_welcome);

        initView();
        fragmentlog();
    }

    private void fragmentlog() {
        manager =getSupportFragmentManager();
        MyFragment1 my1=new MyFragment1();
        list.add(my1);
        my=new MyAdapter(manager,list);
        viewPager_vp.setAdapter(my);
        viewPager_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }
        private void initView() {
        viewPager_vp= (ViewPager) findViewById(R.id.viewPager_vp);

        image_iv= (ImageView) findViewById(R.id.image_iv);
        image_iv.setAlpha(100);


        group= (RadioGroup) findViewById(R.id.group);
        buttons=new RadioButton[group.getChildCount()];
        for (int i = 0; i < buttons.length; i++) {
            buttons[i]= (RadioButton) group.getChildAt(i);

        }
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {

            }
        });

    }


 public class MyFragment1 extends Fragment implements Interfaces.CallBackJson,CallBackJson1{

    private ViewPager inViewPager_vp;
    private ListView list_lv;
    private String pathTitle="http://m.mydrivers.com/app/newstoppic.aspx?id=1464423383694";
    private String pathNews="http://m.mydrivers.com/app/newslist.aspx?tid=0&minId=0&maxId=0&ver=2.2&temp=1464423764091";
    List<View> views=new ArrayList<View>();
    private TextView text_tv1;
    private ImageView image_top;
    private List<NewsBean> total=new ArrayList<NewsBean>();
    ListAdapter adapter;
    View head;



    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.fragment_layout,container,false);
        list_lv= (ListView) view.findViewById(R.id.list_lv);
        return view;
//


    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);


        head=LayoutInflater.from(getActivity()).inflate(R.layout.head_layout,null);
        inViewPager_vp= (ViewPager) head.findViewById(R.id.inviewPager_vp);
        text_tv1= (TextView) head.findViewById(R.id.text_tv1);
        image_top= (ImageView) head.findViewById(R.id.image_top);
        iniviewPager();


        adapter=new ListAdapter(getActivity(),total);
        list_lv.addHeaderView(head);
        list_lv.setAdapter(adapter);
        iniList();
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    private void iniList() {
        new DownJsonTask1(this).execute(pathNews);
    }


    private void iniviewPager() {
        new DownJsonTask(this).execute(pathTitle);

    }
    @Override
    public void callBack(String json) {
        final List<TitleBean> list = JsonParserInner.getJson(json);
        for (int i = 0; i < list.size(); i++) {
            ImageView view=new ImageView(getActivity());
            views.add(view);
            new DownBitmapTask(view).execute(list.get(i).getIcon());
        }
        inViewPager_vp.setAdapter(new InnerAdapter(views));
        inViewPager_vp.setCurrentItem(0);
        text_tv1.setText(list.get(0).getTitle());
        inViewPager_vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                text_tv1.setText(list.get(position%list.size()).getTitle());
                if(position%list.size()==0) {
                    image_top.setImageResource(R.mipmap.focus_point_1);
                }
                if(position%list.size()==1){
                    image_top.setImageResource(R.mipmap.focus_point_2);
                }
                if(position%list.size()==2){
                    image_top.setImageResource(R.mipmap.focus_point_3);
                }
            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });


        //回调listView

    }

    @Override
    public void callBack1(String json) {
        List<NewsBean> json2 = JsonParser2.getJson2(json);
        total.addAll(json2);
        adapter.notifyDataSetChanged();


    }

//这是报错的类
 public class ListAdapter extends BaseAdapter{
    List<NewsBean> list;
    Context mContext;
    LayoutInflater inflater;

    public ListAdapter(Context mContext, List<NewsBean> list) {
        this.mContext = mContext;
        this.list = list;
        inflater.from(mContext);
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if(convertView==null){
            convertView=inflater.inflate(R.layout.list_layout,parent,false);
            holder=new ViewHolder();
            holder.text1= (TextView) convertView.findViewById(R.id.text1);
            holder.text2= (TextView) convertView.findViewById(R.id.text2);
            holder.text3= (TextView) convertView.findViewById(R.id.text3);
            holder.imageView= (ImageView) convertView.findViewById(R.id.imageView);
            convertView.setTag(holder);
        }else{
           holder= (ViewHolder) convertView.getTag();
        }
        NewsBean bean=list.get(position);
        holder.text1.setText(bean.getTitle());
        holder.text2.setText(bean.getEditor());
        holder.text3.setText(bean.getReviewcount());
        holder.imageView.setImageResource(R.mipmap.ic_launcher);

        return convertView;
    }
    static class ViewHolder{
        TextView text1,text2,text3;
        ImageView imageView;
    }
}
//错误日志
 java.lang.NullPointerException
 ListV.ListAdapter.getView(ListAdapter.java:48)

2个回答

Marksinoberg
Marksinoberg   Ds   Rxr 2016.09.17 11:23

根据

 //错误日志
 java.lang.NullPointerException
 ListV.ListAdapter.getView(ListAdapter.java:48)
  ListAdapter adapter;
    ···
    list_lv.setAdapter(adapter);

而且你的那个创建方法上很明显使用了@nullable注解,默认不显示未初始化的错误。你应该先去初始化你的适配器。

sinat_35670989
sinat_35670989 具体要怎么初始化,
大约一年之前 回复
Marksinoberg
Marksinoberg 回复初心不负: 关键问题不是注解的问题,而是你代码中没有对引用到的变量初始化。这是出错的根源。
大约一年之前 回复
sinat_35670989
sinat_35670989 那把@nullable去掉可不可以
大约一年之前 回复
qq_35669205
qq_35669205   2016.09.17 11:41

listview 优化 有问题吗?
@Override
public View getView(int position, View convertView, final ViewGroup parent) {
View view = convertView;
ViewHolder viewHolder;

    if (convertView == null) {
        view = View.inflate(context, R.layout.item_tea, null);

        viewHolder = new ViewHolder();
        viewHolder.title = ((TextView) view.findViewById(R.id.item_tea_textview_title));
        viewHolder.source = ((TextView) view.findViewById(R.id.item_tea_textview_source));
        viewHolder.nickname = ((TextView) view.findViewById(R.id.item_tea_textview_nickname));
        viewHolder.create_time = (TextView) view.findViewById(R.id.item_tea_textview_create_time);
        viewHolder.wap_thumb = (ImageView) view.findViewById(R.id.item_tea_imageview_wap_thumb);

        view.setTag(viewHolder);

    } else {
        viewHolder = (ViewHolder) view.getTag();
    }


    final TeaItem teaItem = teaItemList.get(position);

    viewHolder.title.setText(teaItem.getTitle()+"  ");
    viewHolder.source.setText(teaItem.getSource()+"  ");
    viewHolder.nickname.setText(teaItem.getNickname()+"  ");
    viewHolder.create_time.setText(teaItem.getCreate_time()+"  ");
    viewHolder.wap_thumb.setTag(teaItem.getWap_thumb());

    new AsyncTask_pathToBitmap(new OnPathToBitmap() {
        @Override
        public void onPathToBitmap(Bitmap s) {
            ImageView imageView = (ImageView) parent.findViewWithTag(teaItem.getWap_thumb());
            imageView.setImageBitmap(s);
        }
    },context).execute(teaItem.getWap_thumb());
    return view;
}
sinat_35670989
sinat_35670989 应该没问题,但是不清楚哪的问题
大约一年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片