weixin_36767032
weixin_36767032
2019-04-22 15:27

Android 中写了一个listview,运行就闪退

  • android-studio
  • android

一个MainActivity中有三个Fragment(其中有个为MovieFrament),下面这个ViewingFragment是MovieFragment中的一个嵌套的Fragment

package com.example.movie;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterViewFlipper;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**

  • A simple {@link Fragment} subclass.
    */
    public class ViewingFragment extends Fragment {
    private ListView listView;
    private List movieList;
    private List> data = new ArrayList>();
    public ViewingFragment() {
    // Required empty public constructor
    }
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_viewing,container,false);
    listView = (ListView) view.findViewById(R.id.mov_listview);
    movieList=initmovies(movieList);
    for (int i = 0; i < movieList.size(); i++) {
    HashMap map = new HashMap();
    map.put("name", movieList.get(i).getName());
    map.put("imageId", movieList.get(i).getImageId());
    map.put("director", movieList.get(i).getDirector());
    map.put("charactors", movieList.get(i).getCharactors());
    data.add(map);
    }
    SimpleAdapter adapter = new SimpleAdapter(getParentFragment().getActivity(), data, R.layout.movie2_item,
    new String[]{ "name", "imageId", "director" ,"charactors"}, new int[]{ R.id.txt_name,
    R.id.imageId,
    R.id.txt_director,
    R.id.txt_charactors});
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
    //我们需要的内容,跳转页面或显示详细信息
    Intent intent=new Intent(getActivity(),Mov_InfActivity.class);
    startActivity(intent);
    }
    });

    return view;
    

    }
    private static List initmovies(List movieList) {

    Movie2 apple = new Movie2("悲伤逆流成河", R.mipmap.beishang,"导演:落落","主演:赵英博 任敏 辛云来");
    movieList.add(apple);
    Movie2 banana = new Movie2("新灰姑娘", R.mipmap.huigul,"导演:琳恩.萨瑟兰","主演:Kristen");
    movieList.add(banana);
    Movie2 orange = new Movie2("李茶的姑妈", R.mipmap.licha,"导演:吴昱翰","主演:黄才伦 艾伦 宋阳");
    movieList.add(orange);
    Movie2 watermelon = new Movie2("胖子行动队", R.mipmap.pangzi,"导演:包贝尔 秦小珍","主演:包贝尔 文章 克拉拉");
    movieList.add(watermelon);
    Movie2 pear = new Movie2("无双", R.mipmap.wushuang,"导演:庄文强","主演:周润发 郭富城 张静初");
    movieList.add(pear);
    Movie2 grape = new Movie2("嘻哈英熊", R.mipmap.xiha,"导演:王琦","主演:林强 刘康 徐梦娟");
    movieList.add(grape);
    Movie2 pineapple = new Movie2("影", R.mipmap.ying,"导演:张艺谋","主演:邓超 孙俪 郑恺");
    movieList.add(pineapple);
    Movie2 strawberry = new Movie2("找到你", R.mipmap.zhaodaoni,"导演:吕乐","主演:姚晨 马伊俐 袁文康");
    movieList.add(strawberry);
      return movieList;
    

    }
    }

  • 点赞
  • 回答
  • 收藏
  • 复制链接分享

2条回答

为你推荐

换一换