gpt4 book ai didi

java - 未显示可扩展 ListView Activity 。我该如何修复。?

转载 作者:行者123 更新时间:2023-12-01 09:35:36 26 4
gpt4 key购买 nike

我是 android studio 的初学者。我在我的方案 Activity 中添加了可扩展 ListView ,但是当单击主页上的按钮时,它不会导航方案 Activity ,我将强制关闭并移回登录页面。我该如何解决这个问题有人请帮助我吗?

我将在下面添加我的每个 Activity 和代码

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. Activity 方案

    <?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_width="match_parent"
android:layout_height="match_parent"
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_width="wrap_content"
android:layout_height="wrap_content"
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_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textClock"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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_width="fill_parent"
android:layout_height="wrap_content"
android:paddingLeft="?android:attr/expandableListPreferredItemPaddingLeft"
android:textSize="17dp"
android:textColor="#f9f93d" />

</LinearLayout>

5.子布局

     <TextView
android:id="@+id/lblListItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
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_width="match_parent"
android:layout_height="match_parent"
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_width="fill_parent"
android:layout_height="fill_parent"
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 ComponentInfo{com.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 方法)

在 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 方法)

在 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)

请帮助我

最佳答案

expListView 似乎有一个空引用。您确定您的 R.layout.activity_iistview Activity 布局中有此 ListView 吗?

所以我认为你应该改变:

setContentView(R.layout.activity_iistview);

setContentView(R.layout.activity_scheme);

关于java - 未显示可扩展 ListView Activity 。我该如何修复。?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38974056/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com