- Java 双重比较
- java - 比较器与 Apache BeanComparator
- Objective-C 完成 block 导致额外的方法调用?
- database - RESTful URI 是否应该公开数据库主键?
目前正在使用 RecyclerView
在 android 中开发一个 ExpandableListView
。我已经完成了几乎所有的事情,但不知何故我得到了一个我无法解决的 NullpointerException
。任何帮助将不胜感激。
我正在分享代码 fragment 和 git 链接
应用程序在 apadter 类中的这一行发生错误
public DriverScheduleExpandableAdapter(Context mContext, @NonNull
List<DriverSchedule.Schedules> parentList) {
super(parentList);//////**this is where the app is crashing**
mRecipeList = parentList;
mInflater = LayoutInflater.from(mContext);
}
错误是:
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'java.util.Iterator java.util.List.iterator()' on a null object reference
at com.bignerdranch.expandablerecyclerview.model.ExpandableWrapper.generateChildItemList(ExpandableWrapper.java:99)
at com.bignerdranch.expandablerecyclerview.model.ExpandableWrapper.<init>(ExpandableWrapper.java:33)
at com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter.generateParentWrapper(ExpandableRecyclerAdapter.java:1357)
at com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter.generateFlattenedParentChildList(ExpandableRecyclerAdapter.java:1326)
at com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter.<init>(ExpandableRecyclerAdapter.java:120)
at com.rtstl.expandablelistview.adapter.DriverScheduleExpandableAdapter.<init>(DriverScheduleExpandableAdapter.java:23)
at com.rtstl.expandablelistview.MainActivity.inflateadapter(MainActivity.java:50)
at com.rtstl.expandablelistview.MainActivity.initview(MainActivity.java:41)
at com.rtstl.expandablelistview.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:6357)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2408)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2515)
at android.app.ActivityThread.access$1000(ActivityThread.java:154)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1379)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5571)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:635)
MainActivity.java
package com.rtstl.expandablelistview;
import android.content.Context;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.widget.Toast;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.rtstl.expandablelistview.adapter.DriverScheduleAdapter;
import com.rtstl.expandablelistview.adapter.DriverScheduleExpandableAdapter;
import com.rtstl.expandablelistview.databinding.ActivityMainBinding;
import com.rtstl.expandablelistview.model.DriverSchedule;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
ActivityMainBinding binding;
Context mContext;
DriverSchedule list_driver;
DriverScheduleAdapter adapter;
DriverScheduleExpandableAdapter adapterExp;
Gson gson;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mContext=this;
gson=new Gson();
initview();
}
private void initview() {
binding= DataBindingUtil.setContentView(this, R.layout.activity_main);
inflateadapter();
}
private void inflateadapter() {
////for reading file from raw folder otherwise it's not required
list_driver= gson.fromJson(readFileFromRawDirectory(R.raw.driverschedule), new TypeToken<DriverSchedule>(){}.getType());
////////////////////////////////////////
Toast.makeText(mContext,""+list_driver.getData().getSclist().size(),Toast.LENGTH_SHORT).show();
adapterExp = new DriverScheduleExpandableAdapter(mContext, list_driver.getData().getSclist());
binding.rvRecycle.setLayoutManager(new LinearLayoutManager(this));
binding.rvRecycle.setAdapter(adapter);
}
private String readFileFromRawDirectory(int resourceId){
InputStream iStream = this.getResources().openRawResource(resourceId);
ByteArrayOutputStream byteStream = null;
try {
byte[] buffer = new byte[iStream.available()];
iStream.read(buffer);
byteStream = new ByteArrayOutputStream();
byteStream.write(buffer);
byteStream.close();
iStream.close();
//inflateadapter();
} catch (IOException e) {
e.printStackTrace();
}
return byteStream.toString();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.v7.widget.RecyclerView
android:id="@+id/rv_Recycle"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</layout>
适配器类
package com.rtstl.expandablelistview.adapter;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.bignerdranch.expandablerecyclerview.ExpandableRecyclerAdapter;
import com.rtstl.expandablelistview.R;
import com.rtstl.expandablelistview.model.DriverSchedule;
import java.util.List;
public class DriverScheduleExpandableAdapter extends ExpandableRecyclerAdapter<DriverSchedule.Schedules, DriverSchedule.Alloted_kids, RouteViewHolder, KidViewHolder> {
private LayoutInflater mInflater;
private List<DriverSchedule.Schedules> mRecipeList;
private static final int PARENT_NORMAL = 1;
private static final int CHILD_NORMAL = 3;
public DriverScheduleExpandableAdapter(Context mContext, @NonNull List<DriverSchedule.Schedules> parentList) {
super(parentList);//////**this is where the app is crashing**
mRecipeList = parentList;
mInflater = LayoutInflater.from(mContext);
}
@NonNull
@Override
public RouteViewHolder onCreateParentViewHolder(@NonNull ViewGroup parentViewGroup, int viewType) {
View recipeView;
switch (viewType) {
default:
case PARENT_NORMAL:
recipeView = mInflater.inflate(R.layout.group_item, parentViewGroup, false);
break;
}
return new RouteViewHolder(recipeView);
}
@NonNull
@Override
public KidViewHolder onCreateChildViewHolder(@NonNull ViewGroup childViewGroup, int viewType) {
View ingredientView;
switch (viewType) {
default:
case CHILD_NORMAL:
ingredientView = mInflater.inflate(R.layout.child_item, childViewGroup, false);
break;
}
return new KidViewHolder(ingredientView);
}
@Override
public void onBindParentViewHolder(@NonNull RouteViewHolder parentViewHolder, int parentPosition, @NonNull DriverSchedule.Schedules parent) {
parentViewHolder.bind(parent);
}
@Override
public void onBindChildViewHolder(@NonNull KidViewHolder childViewHolder, int parentPosition, int childPosition, @NonNull DriverSchedule.Alloted_kids child) {
childViewHolder.bind(child);
}
@Override
public int getParentViewType(int parentPosition) {
return PARENT_NORMAL;
}
@Override
public int getChildViewType(int parentPosition, int childPosition) {
return CHILD_NORMAL;
}
@Override
public boolean isParentViewType(int viewType) {
return viewType == PARENT_NORMAL;
}
}
DriverSchedule.java
package com.rtstl.expandablelistview.model;
import android.databinding.BaseObservable;
import com.bignerdranch.expandablerecyclerview.model.Parent;
import com.google.gson.annotations.Expose;
import com.google.gson.annotations.SerializedName;
import java.util.List;
/**
* Created by User1 on 09-03-2018.
*/
public class DriverSchedule extends BaseObservable {
@SerializedName("status")
@Expose
public String status;
@SerializedName("msg")
@Expose
public String msg;
@SerializedName("data")
@Expose
public Data data;
public String getStatus() {
return status;
}
public void setStatus(String status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Data getData() {
return data;
}
public void setData(Data data) {
this.data = data;
}
public class Data {
@SerializedName("schedules")
@Expose
List<Schedules> sclist;
@SerializedName("driver_details")
@Expose
Driver_details driver_details;
public List<Schedules> getSclist() {
return sclist;
}
public void setSclist(List<Schedules> sclist) {
this.sclist = sclist;
}
public Driver_details getDriver_details() {
return driver_details;
}
public void setDriver_details(Driver_details driver_details) {
this.driver_details = driver_details;
}
}
public class Schedules implements Parent<Alloted_kids> {
@SerializedName("is_active")
@Expose
public String is_active;
@SerializedName("route_details")
@Expose
public Route_details route_details;
@SerializedName("alloted_kids")
@Expose
public List<Alloted_kids> alloted_kids;
public String getIs_active() {
return is_active;
}
public void setIs_active(String is_active) {
this.is_active = is_active;
}
public Route_details getRoute_details() {
return route_details;
}
public void setRoute_details(Route_details route_details) {
this.route_details = route_details;
}
public List<Alloted_kids> getAlloted_kids() {
return alloted_kids;
}
public void setAlloted_kids(List<Alloted_kids> alloted_kids) {
this.alloted_kids = alloted_kids;
}
@Override
public List<Alloted_kids> getChildList() {
return null;
}
@Override
public boolean isInitiallyExpanded() {
return false;
}
}
public class Driver_details {
@SerializedName("driver_details")
@Expose
public Driver_details1 driver_details;
public Driver_details1 getDriver_details() {
return driver_details;
}
public void setDriver_details(Driver_details1 driver_details) {
this.driver_details = driver_details;
}
}
public class Route_details {
@SerializedName("ds_id")
@Expose
public String ds_id;
@SerializedName("kidpool_route_id")
@Expose
public String kidpool_route_id;
public String getDs_id() {
return ds_id;
}
public void setDs_id(String ds_id) {
this.ds_id = ds_id;
}
public String getKidpool_route_id() {
return kidpool_route_id;
}
public void setKidpool_route_id(String kidpool_route_id) {
this.kidpool_route_id = kidpool_route_id;
}
}
public class Alloted_kids {
@SerializedName("kid_name")
@Expose
public String kid_name;
@SerializedName("kid_image")
@Expose
public String kid_image;
public String getKid_name() {
return kid_name;
}
public void setKid_name(String kid_name) {
this.kid_name = kid_name;
}
public String getKid_image() {
return kid_image;
}
public void setKid_image(String kid_image) {
this.kid_image = kid_image;
}
}
public class Driver_details1 {
@SerializedName("driver_id")
@Expose
public String driver_id;
@SerializedName("driver_name")
@Expose
public String driver_name;
public String getDriver_id() {
return driver_id;
}
public void setDriver_id(String driver_id) {
this.driver_id = driver_id;
}
public String getDriver_name() {
return driver_name;
}
public void setDriver_name(String driver_name) {
this.driver_name = driver_name;
}
}
}
KidViewHolder.java
package com.rtstl.expandablelistview.adapter;
import android.support.annotation.NonNull;
import android.view.View;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ChildViewHolder;
import com.rtstl.expandablelistview.R;
import com.rtstl.expandablelistview.model.DriverSchedule;
class KidViewHolder extends ChildViewHolder{
private TextView mIngredientTextView;
public KidViewHolder(@NonNull View itemView) {
super(itemView);
mIngredientTextView = (TextView) itemView.findViewById(R.id.tv_childname);
}
public void bind(@NonNull DriverSchedule.Alloted_kids ingredient) {
mIngredientTextView.setText(ingredient.getKid_name());
}
}
RouteViewHolder.java
package com.rtstl.expandablelistview.adapter;
import android.annotation.SuppressLint;
import android.os.Build;
import android.support.annotation.NonNull;
import android.view.View;
import android.view.animation.RotateAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.bignerdranch.expandablerecyclerview.ParentViewHolder;
import com.rtstl.expandablelistview.R;
import com.rtstl.expandablelistview.model.DriverSchedule;
class RouteViewHolder extends ParentViewHolder {
private static final float INITIAL_POSITION = 0.0f;
private static final float ROTATED_POSITION = 180f;
@NonNull
private final ImageView mArrowExpandImageView;
private TextView mRecipeTextView;
public RouteViewHolder(@NonNull View itemView) {
super(itemView);
mRecipeTextView = (TextView) itemView.findViewById(R.id.group_name);
mArrowExpandImageView = (ImageView) itemView.findViewById(R.id.iv_exp);
}
public void bind(@NonNull DriverSchedule.Schedules recipe) {
mRecipeTextView.setText(recipe.getRoute_details().getKidpool_route_id());
}
@SuppressLint("NewApi")
@Override
public void setExpanded(boolean expanded) {
super.setExpanded(expanded);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
if (expanded) {
mArrowExpandImageView.setRotation(ROTATED_POSITION);
} else {
mArrowExpandImageView.setRotation(INITIAL_POSITION);
}
}
}
@Override
public void onExpansionToggled(boolean expanded) {
super.onExpansionToggled(expanded);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
RotateAnimation rotateAnimation;
if (expanded) { // rotate clockwise
rotateAnimation = new RotateAnimation(ROTATED_POSITION,
INITIAL_POSITION,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
} else { // rotate counterclockwise
rotateAnimation = new RotateAnimation(-1 * ROTATED_POSITION,
INITIAL_POSITION,
RotateAnimation.RELATIVE_TO_SELF, 0.5f,
RotateAnimation.RELATIVE_TO_SELF, 0.5f);
}
rotateAnimation.setDuration(200);
rotateAnimation.setFillAfter(true);
mArrowExpandImageView.startAnimation(rotateAnimation);
}
}
}
最佳答案
看到这个问题似乎你的列表有一些空值 https://github.com/bignerdranch/expandable-recycler-view/issues/321
我运行了您的代码并记录了您的列表,您的列表中有空值
检查这个方法
@Override
public List<Alloted_kids> getChildList() {
return null;
}
这个方法应该返回非空值这个方法只会导致问题
使用 return Collections.emptyList();而不是在那里返回 null
关于android - 来自 Json 的可扩展 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50502544/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 4 个月前关闭。 Improve
我已经为这个概念苦苦挣扎了一段时间。我正在尝试提出一种松散耦合的 Azure 组件设计,该设计可以使用队列和辅助角色完全可扩展,从而使项目出列并处理。我可以随意扩展工作角色,并且发布到队列从来都不是问
尝试在 android 中创建可扩展 ListView 。我希望每个组都扩展到一个预设的 XML 布局文件。例如。 A组{ 展开时显示 XML 文件 A:按钮、搜索栏、微调器等 } 乙组{ 展开时显示
在 android 可扩展 ListView 中,我需要显示子项,仅在按下展开折叠节点时..即;我不想让 children 按下可扩展 ListView 的行。我该如何完成? 问候,罗尼 最佳答案 我
我正在开发一个Android代码编辑器,但是当我对它应用语法突出显示时,它会变得很滞后,是否有任何方法可以对其进行优化? 这是我的代码: protected void onCreate(Bundle
我正在开发安卓应用程序。我发现下面的输出图像显示了以下问题。请帮我解决我的问题。 Xml file 输出截图- 最佳答案 使用 setIndicatorBounds。 开发链接:http://deve
最近我在尝试使用 android 的可扩展 ListView ,所以我四处搜索并偶然发现了这个 http://about-android.blogspot.com/2010/04/steps-to-i
在 Control.Exception 的文档中在 base 4.4.0.0 中有一个如何创建异常层次结构的示例。该示例展示了如何通过根据父异常声明 Exception 类的实例来捕获特定异常的概括。
我正在开发一个将显示一些可扩展列表的应用程序。事实上,有时该对象不会有任何子对象。所以我想在我的可扩展列表中仅显示具有子元素的元素。我尝试在 getGroupView 函数中放入 if ,如果该对象没
这是一个有点开放式的问题,但是,制作良好的可扩展 Electron 应用程序的好方法是什么? VSCode、Atom 和许多其他工具都支持扩展,但它们的代码库太大,我无法理解发生了什么。我对 Jupy
我和一些 friend 为 Facebook 编写了一款游戏,但没有过多考虑游戏的实际架构 - 想象一下当我们每月吸引超过 300,000 名独立玩家并且我们的服务器崩溃时我们的惊喜。 现在我们正在努
您好,我正在使用 Android Expandable listview 并在其中用不同的 View 膨胀 child 。我遇到的问题是,当我展开一个 View 然后打开另一个父 View 时,布局中
我的场景是,我有一个可扩展 ListView ,在列表中动态添加我和 child 。我的子布局就是这样。 当我点击相应项目的编辑按钮时,我打开对话框从用户那里获取输入,然后点击更新相应的详细信息更新到
我想使用 RecyclerView 创建项目列表,并希望在单击时展开特定项目(如电话列表)。我想在不使用任何库的情况下实现这一目标。谁能帮忙? 最佳答案 获取子数据列表作为数据集中父数据的成员。并且,
我使用 ExpandableListview ... 我能够将从 Web 服务检索到的值设置为子布局的单个 Textview。现在需要在 Web 服务的子布局中设置两个不同的 Textviews 值我
我的可扩展 ListView 的顺序似乎不正确。 这是我的数据提供者: public class DataProvider { public static HashMap> getInfo() {
所以我试图在我的可扩展列表的子项列表中创建一行,但它不起作用,这是我的布局:
我有一个需要自定义箭头的可扩展 ListView 。我尝试将 groupIndicator 设置为这样的选择器: 但是,出于某种原因,这会扭曲箭头的尺寸,请参见下文: 知道它们为
我正在尝试实现具有 2 种不同布局的 ExpandableListView,到目前为止,我花了一段时间才真正让 1 个 View 组使用一个布局,而其他 2 个 View 组使用另一个。到目前为止,外
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 要求我们推荐或查找工具、库或最喜欢的场外资源的问题对于 Stack Overflow 来说是偏离主题的,
我是一名优秀的程序员,十分优秀!