在Fragment中包含图像和文本的Android ListView

我在Android Studio中创建了一个导航抽屉。 导航抽屉的内部片段我想添加一个带有图像和文本的ListView。但是在我运行程序并点击片段程序时,文本崩溃“不幸的是,SerialMania已停止”。 我做错了什么?

package boiko.taisiia.com.serialmania; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.widget.SimpleAdapter; public class Date extends Fragment { // Array of strings storing country names String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; // Array of integers points to images stored in /res/drawable-ldpi/ int[] flags = new int[]{ R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera }; // Array of strings to store currencies String[] currency = new String[]{ "Indian Rupee", "Pakistani Rupee", "Sri Lankan Rupee", "Renminbi", "Bangladeshi Taka", "Nepalese Rupee", "Afghani", "North Korean Won", "South Korean Won", "Japanese Yen" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); getActivity().setContentView(R.layout.fragment_date); // Each row in the list stores country name, currency and flag List<HashMap> aList = new ArrayList<HashMap>(); for(int i=0;i<10;i++){ HashMap hm = new HashMap(); hm.put("txt", "Country : " + countries[i]); hm.put("cur","Currency : " + currency[i]); hm.put("flag", Integer.toString(flags[i]) ); aList.add(hm); } // Keys used in Hashmap String[] from = { "flag","txt","cur" }; // Ids of views in listview_layout int[] to = { R.id.flag,R.id.txt,R.id.cur}; // Instantiating an adapter to store each items // R.layout.listview_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to); // Getting a reference to listview of main.xml layout file ListView listView = ( ListView ) getActivity().findViewById(R.id.listview); // Setting the adapter to the listView listView.setAdapter(adapter); } } 

阅读有关片段的信息更改代码,可以使用onCreateView,onViewCreated和onActivityCreated.onCreate方法是使用初始化变量,对象或接收来自Acivity的值或对象。

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.your_layout, container, false); tvTitle = (TextView) v.findViewById(R.id.tvTitle); ivLogo = (ImageView) v.findViewById(R.id.ivLogo); // Each row in the list stores country name, currency and flag List> aList = new ArrayList>(); for(int i=0;i<10;i++){ HashMap hm = new HashMap(); hm.put("txt", "Country : " + countries[i]); hm.put("cur","Currency : " + currency[i]); hm.put("flag", Integer.toString(flags[i]) ); aList.add(hm); } // Keys used in Hashmap String[] from = { "flag","txt","cur" }; // Ids of views in listview_layout int[] to = { R.id.flag,R.id.txt,R.id.cur}; // Instantiating an adapter to store each items // R.layout.listview_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(v.getContext(), aList, R.layout.listview_layout, from, to); // Getting a reference to listview of main.xml layout file ListView listView = ( ListView ) v.findViewById(R.id.listview); // Setting the adapter to the listView listView.setAdapter(adapter); return v; } 

onCreate删除此代码:

 getActivity().setContentView(R.layout.fragment_date); 

并添加此方法:

 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_date, container, false); } 

UPD

这是你修改过的代码:

 package boiko.taisiia.com.serialmania; import android.content.Context; import android.net.Uri; import android.os.Bundle; import android.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ListView; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import android.widget.SimpleAdapter; public class Date extends Fragment { // Array of strings storing country names String[] countries = new String[] { "India", "Pakistan", "Sri Lanka", "China", "Bangladesh", "Nepal", "Afghanistan", "North Korea", "South Korea", "Japan" }; // Array of integers points to images stored in /res/drawable-ldpi/ int[] flags = new int[]{ R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera, R.drawable.ic_menu_camera }; // Array of strings to store currencies String[] currency = new String[]{ "Indian Rupee", "Pakistani Rupee", "Sri Lankan Rupee", "Renminbi", "Bangladeshi Taka", "Nepalese Rupee", "Afghani", "North Korean Won", "South Korean Won", "Japanese Yen" }; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Each row in the list stores country name, currency and flag List> aList = new ArrayList>(); for(int i=0;i<10;i++){ HashMap hm = new HashMap(); hm.put("txt", "Country : " + countries[i]); hm.put("cur","Currency : " + currency[i]); hm.put("flag", Integer.toString(flags[i]) ); aList.add(hm); } // Keys used in Hashmap String[] from = { "flag","txt","cur" }; // Ids of views in listview_layout int[] to = { R.id.flag,R.id.txt,R.id.cur}; // Instantiating an adapter to store each items // R.layout.listview_layout defines the layout of each item SimpleAdapter adapter = new SimpleAdapter(getActivity().getBaseContext(), aList, R.layout.listview_layout, from, to); // Getting a reference to listview of main.xml layout file ListView listView = ( ListView ) getActivity().findViewById(R.id.listview); // Setting the adapter to the listView listView.setAdapter(adapter); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_date, container, false); } } 

这不是在片段中设置内容的好方法!

 public final class YourFragment extends Fragment{ private TextView tvTitle; private ImageView ivLogo; /** * default and empty constructor * The constructor should stay empty */ public YourFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { super.onCreateView(inflater, container, savedInstanceState); View v = inflater.inflate(R.layout.your_layout, container, false); this.tvTitle = (TextView) v.findViewById(R.id.tvTitle); this.ivLogo = (ImageView) v.findViewById(R.id.ivLogo); return v; } 

// …片段的其余部分

它应该做的工作!

希望这个帮助。