未显示可扩展列表视图活动。我该如何解决。?
Posted
技术标签:
【中文标题】未显示可扩展列表视图活动。我该如何解决。?【英文标题】:Expandable list view activity does not shown.How can i fix.? 【发布时间】:2016-12-22 18:41:33 【问题描述】:我是 android studio 的初学者。我在我的方案活动中添加了可扩展列表视图,但是当单击主页上的按钮时,它不会导航方案活动,我将强制关闭并返回登录页面。我该如何解决这个问题有人请帮帮我吗?
我将在下面添加我的每个活动和代码
1.Scheme.java
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ExpandableListView.OnGroupClickListener;
import android.widget.ExpandableListView.OnGroupCollapseListener;
import android.widget.ExpandableListView.OnGroupExpandListener;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
public class Scheme extends Activity
My_Adapter listAdapter;
ExpandableListView expListView;
List<String> listDataHeader;
HashMap<String, List<String>> listDataChild;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_iistview);
// get the listview
expListView = (ExpandableListView) findViewById(R.id.expandableListView);
// preparing list data
prepareListData();
listAdapter = new My_Adapter(this, 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)
// Toast.makeText(getApplicationContext(),
// "Group Clicked " + listDataHeader.get(groupPosition),
// Toast.LENGTH_SHORT).show();
return false;
);
// Listview Group expanded listener
expListView.setOnGroupExpandListener(new OnGroupExpandListener()
@Override
public void onGroupExpand(int groupPosition)
Toast.makeText(getApplicationContext(),
listDataHeader.get(groupPosition) + " Expanded",
Toast.LENGTH_SHORT).show();
);
// Listview Group collasped listener
expListView.setOnGroupCollapseListener(new OnGroupCollapseListener()
@Override
public void onGroupCollapse(int groupPosition)
Toast.makeText(getApplicationContext(),
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(
getApplicationContext(),
listDataHeader.get(groupPosition)
+ " : "
+ listDataChild.get(
listDataHeader.get(groupPosition)).get(
childPosition), Toast.LENGTH_SHORT)
.show();
return false;
);
/*
* Preparing the list data
*/
private void prepareListData()
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Top 250");
listDataHeader.add("Now Showing");
listDataHeader.add("Coming Soon..");
// Adding child data
List<String> top250 = new ArrayList<String>();
top250.add("The Shawshank Redemption");
top250.add("The Godfather");
top250.add("The Godfather: Part II");
top250.add("Pulp Fiction");
top250.add("The Good, the Bad and the Ugly");
top250.add("The Dark Knight");
top250.add("12 Angry Men");
List<String> nowShowing = new ArrayList<String>();
nowShowing.add("The Conjuring");
nowShowing.add("Despicable Me 2");
nowShowing.add("Turbo");
nowShowing.add("Grown Ups 2");
nowShowing.add("Red 2");
nowShowing.add("The Wolverine");
List<String> comingSoon = new ArrayList<String>();
comingSoon.add("2 Guns");
comingSoon.add("The Smurfs 2");
comingSoon.add("The Spectacular Now");
comingSoon.add("The Canyons");
comingSoon.add("Europa Report");
listDataChild.put(listDataHeader.get(0), top250); // Header, Child data
listDataChild.put(listDataHeader.get(1), nowShowing);
listDataChild.put(listDataHeader.get(2), comingSoon);
2.活动方案
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:background="@drawable/sce"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.agte.vivo.Scheme">
<TextView
android:layout_
android:layout_
android:text="Current Schemes"
android:id="@+id/textView"
android:textSize="30dp"
android:layout_below="@+id/textClock"
android:layout_centerHorizontal="true"
android:textColor="#040404" />
<TextClock
android:layout_
android:layout_
android:id="@+id/textClock"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ExpandableListView
android:layout_
android:layout_
android:id="@+id/expandableListView"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
android:divider="#A4C739"
android:dividerHeight="0.5dp"
android:layout_below="@+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="86dp" />
</RelativeLayout>
3.myadapter
package com.agte.vivo;
import android.content.Context;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.HashMap;
import java.util.List;
public class My_Adapter extends BaseExpandableListAdapter
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public My_Adapter(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(this._listDataHeader.get(groupPosition))
.get(childPosititon);
@Override
public long getChildId(int groupPosition, int childPosition)
return childPosition;
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null)
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.child_layout,parent, false);
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
@Override
public int getChildrenCount(int groupPosition)
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
@Override
public Object getGroup(int groupPosition)
return this._listDataHeader.get(groupPosition);
@Override
public int getGroupCount()
return this._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) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.parent_layout,parent, false);
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
@Override
public boolean hasStableIds()
return false;
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
return true;
4.父布局
<TextView
android:id="@+id/lblListHeader"
android:layout_
android:layout_
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />
</LinearLayout>
5.child 布局
<TextView
android:id="@+id/lblListItem"
android:layout_
android:layout_
android:textSize="17dip"
android:paddingTop="5dp"
android:paddingBottom="5dp"
android:paddingLeft="?android:attr/expandableListPreferredChildPaddingLeft" />
</LinearLayout>
6.activity iistview
<?xml version="1.0" encoding="utf-8"?>
<!--<RelativeLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.agte.vivo.iistview">
</RelativeLayout>-->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_
android:layout_
android:text="New Text"
android:id="@+id/textView4"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
没有错误显示但运行监视器时会显示空指针问题
08-16 16:43:24.211 25818-25818/com.agte.vivo E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.agte.vivo, PID: 25818
java.lang.RuntimeException: Unable to start activity ComponentInfocom.agte.vivo/com.agte.vivo.Scheme: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)' on a null object reference
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2342)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404)
在 android.app.ActivityThread.access$800(ActivityThread.java:154)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
在 android.os.Handler.dispatchMessage(Handler.java:102)
在 android.os.Looper.loop(Looper.java:135)
在 android.app.ActivityThread.main(ActivityThread.java:5290)
在 java.lang.reflect.Method.invoke(Native Method)
在 java.lang.reflect.Method.invoke(Method.java:372)
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“void android.widget.ExpandableListView.setAdapter(android.widget.ExpandableListAdapter)”
在 com.agte.vivo.Scheme.onCreate(Scheme.java:40)
在 android.app.Activity.performCreate(Activity.java:6039)
在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2404) 处
在 android.app.ActivityThread.access$800(ActivityThread.java:154)
在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1315)
在 android.os.Handler.dispatchMessage(Handler.java:102) 处
在 android.os.Looper.loop(Looper.java:135) 处
在 android.app.ActivityThread.main(ActivityThread.java:5290)
在 java.lang.reflect.Method.invoke(Native Method)
在 java.lang.reflect.Method.invoke(Method.java:372) 处
在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:908)
在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:703)
请帮帮忙
【问题讨论】:
"但是当点击主页上的按钮时" > 什么意思?而且您的问题太长,只显示重要的内容。 是的,这意味着我在主页方案产品中有 4 个按钮,其他 2 个按钮在单击方案时我需要转到方案页面,但它没有导航到该页面 我尝试了不同的方法,但我无法获得解决方案。请帮助,在方案活动中我添加了一个可扩展列表视图,但它不起作用 【参考方案1】:expListView
似乎有一个空引用。你确定你的R.layout.activity_iistview activity
布局中有这个列表视图吗?
所以我认为你应该改变:
setContentView(R.layout.activity_iistview);
到
setContentView(R.layout.activity_scheme);
【讨论】:
尝试如上所述重命名该行 我在哪里改的名字我看不懂 在你的 Scheme.java 中 ya in scheme 改名麻烦了,能不能给个想改的部分 先生,我怎样才能在这个列表中添加额外的一个列表以上是关于未显示可扩展列表视图活动。我该如何解决。?的主要内容,如果未能解决你的问题,请参考以下文章