可扩展列表视图在 android 的片段活动中不起作用
Posted
技术标签:
【中文标题】可扩展列表视图在 android 的片段活动中不起作用【英文标题】:Exapandable listview not working in fragment activity in android 【发布时间】:2013-10-24 04:10:23 【问题描述】:我在片段中使用了可扩展的列表视图。但它无法为 LayoutInflater 获取空指针异常。
public class DemoActivity extends Fragment
public static final String ARG_SECTION_NUMBER = "section_number";
ExpandableListAdapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
Context mContext;
public DemoActivity(Context context)
this.mContext=DemoActivity.this.getActivity();;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.demo, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label_demo);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
// get the listview
expListView = (ExpandableListView) rootView.findViewById(R.id.lvExp);
// preparing list data
prepareListData();
listAdapter = new ExpandableListAdapter(mContext, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
// Listview Group click listener
expListView.setOnGroupClickListener(new OnGroupClickListener()
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
return false;
);
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener()
@Override
public void onGroupExpand(int groupPosition)
Toast.makeText(mContext,
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
);
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener()
@Override
public void onGroupCollapse(int groupPosition)
Toast.makeText(mContext,listDataHeader.get(groupPosition) + " Collapsed",
Toast.LENGTH_SHORT).show();
);
// Listview on child click listener
expListView.setOnChildClickListener(new OnChildClickListener()
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
// TODO Auto-generated method stub
Toast.makeText(mContext,listDataHeader.get(groupPosition)+ " : "+ listDataChild.get(
listDataHeader.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
return false;
);
return rootView;
private void prepareListData()
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
List<String> list1 = new ArrayList<String>();
list1.add("The Shawshank Redemption");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("Despicable Me 2");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
String[] data=search();
// Adding child data
for(int i=0;i<search().length;i++)
listDataHeader.add(""+data[i]);
//Toast.makeText(mContext,data[i], Toast.LENGTH_SHORT).show();
listDataChild.put(listDataHeader.get(i), list1);
public String[] search()
String[] filenames;
File file = new File(Environment.getExternalStorageDirectory(), "/Recording");
if(!file.exists())
file.mkdirs();
//filenames = new String[30];
if(!file.list().equals(""))
filenames = file.list();
int total = 0;
for (int i = 0; i< filenames.length; i++)
if (filenames[i].contains(".3gp"))
total++;
else
String[] filearray=new String[]"";
filenames=filearray;
return filenames;
适配器类-
public class ExpandableListAdapter extends BaseExpandableListAdapter
private Context _context;
private List<String> _listDataHeader;
private HashMap<String, List<String>> _listDataChild;
private EditText editTask;
private CheckBox mCB;
private ImageView mImageView;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData)
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
@Override
public Object getChild(int groupPosition, int childPosititon)
return this._listDataChild.get(_listDataHeader.get(groupPosition))
.get(childPosititon);
@Override
public long getChildId(int groupPosition, int childPosition)
return childPosition;
@Override
public View getChildView(final int groupPosition, final int childPosition,
boolean isLastChild, View convertView, final ViewGroup parent)
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) _context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
ImageView txtListChild = (ImageView) convertView.findViewById(R.id.lblListItem);
txtListChild.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
String data[]=search();
Intent i=new Intent(_context,DBRoulette.class);
i.putExtra("file_name", data[groupPosition]);
_context.startActivity(i);
);
ImageView send = (ImageView) convertView.findViewById(R.id.send);
send.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
// TODO Auto-generated method stub
String data[]=search();
Intent i=new Intent(_context,XPlayer.class);
i.putExtra("file_name", data[groupPosition]);
_context.startActivity(i);
);
mImageView=(ImageView) convertView.findViewById(R.id.editImage);
mImageView.setOnClickListener(new OnClickListener()
@Override
public void onClick(View v)
String data[]=search();
Intent i=new Intent(_context,GoogleDrive.class);
i.putExtra("file_name", data[groupPosition]);
_context.startActivity(i);
);
return convertView;
@Override
public int getChildrenCount(int groupPosition)
return _listDataChild.get(_listDataHeader.get(groupPosition)).size();
@Override
public Object getGroup(int groupPosition)
return _listDataHeader.get(groupPosition);
@Override
public int getGroupCount()
return _listDataHeader.size();
@Override
public long getGroupId(int groupPosition)
return groupPosition;
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent)
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater)_context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
/***HERE I'M GETTING NULL POINTER EXCEPTION***/
convertView = infalInflater.inflate(R.layout.list_group, null);
TextView lblListHeader = (TextView) convertView.findViewById(R.id.lblListHeader);
lblListHeader.setText(headerTitle);
return convertView;
@Override
public boolean hasStableIds()
return false;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
public String[] search()
String[] filenames;
File file = new File(Environment.getExternalStorageDirectory(), "/Recording");
if(!file.exists())
file.mkdirs();
//filenames = new String[30];
if(!file.list().equals(null))
filenames = file.list();
int total = 0;
for (int i = 0; i< filenames.length; i++)
if (filenames[i].contains(".3gp"))
total++;
else
String[] filearray=new String[]"";
filenames=filearray;
return filenames;
更新的 logcat-
10-16 15:15:44.011: E/androidRuntime(731): FATAL EXCEPTION: main
10-16 15:15:44.011: E/AndroidRuntime(731): java.lang.NullPointerException
10-16 15:15:44.011: E/AndroidRuntime(731): at com.example.android.recorder.ExpandableListAdapter.getGroupView(ExpandableListAdapter.java:127)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.ExpandableListConnector.getView(ExpandableListConnector.java:445)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.ListView.makeAndAddView(ListView.java:1727)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.ListView.fillDown(ListView.java:652)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.ListView.fillFromTop(ListView.java:709)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.ListView.layoutChildren(ListView.java:1580)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.AbsListView.onLayout(AbsListView.java:1147)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:909)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1249)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1125)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.LinearLayout.onLayout(LinearLayout.java:1042)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.View.layout(View.java:7035)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.ViewRoot.performTraversals(ViewRoot.java:1045)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.os.Handler.dispatchMessage(Handler.java:99)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.os.Looper.loop(Looper.java:123)
10-16 15:15:44.011: E/AndroidRuntime(731): at android.app.ActivityThread.main(ActivityThread.java:4627)
10-16 15:15:44.011: E/AndroidRuntime(731): at java.lang.reflect.Method.invokeNative(Native Method)
10-16 15:15:44.011: E/AndroidRuntime(731): at java.lang.reflect.Method.invoke(Method.java:521)
10-16 15:15:44.011: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-16 15:15:44.011: E/AndroidRuntime(731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-16 15:15:44.011: E/AndroidRuntime(731): at dalvik.system.NativeStart.main(Native Method)
【问题讨论】:
你在哪里得到空指针异常?你的 logcat 在哪里?发布 logcat。 我在注释块中提到了我得到空指针异常的地方 只有评论没有人能够理解。发布 logcat 有什么问题吗? @GrIsHu 检查 logcat.i 已经提到 从您的DemoActivity
定义上下文为mContext=DemoActivity.this.getActivity();
而不仅仅是上下文。
【参考方案1】:
将上下文传递给适配器,如下所示-
listAdapter = new ExpandableListAdapter(getActivity(), listDataHeader, listDataChild);
【讨论】:
【参考方案2】:从您的DemoActivity
将上下文定义为mContext=DemoActivity.this.getActivity();
而不仅仅是Context
。
您必须使用getActivity()
,它返回与片段关联的活动。
Activity 是一个上下文(因为 Activity 扩展了 Context)。
已编辑:
在您的上下文中执行以下操作:
public DemoActivity(FragmentActivity context)
this.mContext=context;
【讨论】:
公共演示活动(上下文上下文) this.mContext=context;这里的上下文是我从片段活动传递给这个片段类构造函数的上下文 Yupp.getting 相同的异常 是的,这就是我要再次说的。即使在您的更新得到相同的异常之后 我已经更新了..我尝试了两种方式 this.mContext=DemoActivity.this.getActivity();和 this.mContext=context 在构造函数中以上是关于可扩展列表视图在 android 的片段活动中不起作用的主要内容,如果未能解决你的问题,请参考以下文章