在这里,我成功地添加了一个实时Firebase数据库图像滑块,它检索图像并在图像滑块中显示它们。一切正常。所有更新的图像和所有。除非我删除Firebase实时数据库中的子节点,否则应用程序会崩溃。
家庭碎片
public class HomeFragment extends Fragment {
DatabaseReference databaseReference;
List<ImageSliderModel> slideLists;
SliderView sliderView;
int TotalCounts;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference();
slideLists = new ArrayList<>();
sliderView = view.findViewById(R.id.imageSlider);
FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot != null) {
Long counts = dataSnapshot.getChildrenCount();
TotalCounts = counts.intValue();
sliderView.setSliderAdapter(new ImageSliderAdapter(getActivity(), TotalCounts));
sliderView.setIndicatorAnimation(IndicatorAnimations.WORM);
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
sliderView.setIndicatorSelectedColor(Color.WHITE);
sliderView.setIndicatorUnselectedColor(Color.LTGRAY);
sliderView.setScrollTimeInSec(3);
sliderView.startAutoCycle();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
}
XML代码
<ViewFlipper
android:id="@+id/viewflipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"
android:flipInterval="2000">
<com.smarteist.autoimageslider.SliderView
android:id="@+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="200dp"
app:sliderAutoCycleDirection="back_and_forth"
app:sliderIndicatorAnimationDuration="600"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorSelectedColor="#FFFFFFFF"
app:sliderIndicatorUnselectedColor="#FFCCCCCC"
app:sliderScrollTimeInSec="3"
app:sliderStartAutoCycle="true" />
</ViewFlipper>
图像滑块适配器
public class ImageSliderAdapter extends SliderViewAdapter<SliderviewHolder> {
Context context;
int setTotalCount;
String ImageLink;
public ImageSliderAdapter(Context context, int setTotalCount) {
this.setTotalCount = setTotalCount;
this.context = context;
}
@Override
public SliderviewHolder onCreateViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item_layout,parent,false);
return new SliderviewHolder(view) ;
}
@Override
public void onBindViewHolder(final SliderviewHolder viewHolder, final int position) {
FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
switch (position){
case 0:
ImageLink = dataSnapshot.child("1").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 1:
ImageLink = dataSnapshot.child("2").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 2:
ImageLink = dataSnapshot.child("3").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 3:
ImageLink = dataSnapshot.child("4").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 4:
ImageLink = dataSnapshot.child("5").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 5:
ImageLink = dataSnapshot.child("6").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 6:
ImageLink = dataSnapshot.child("7").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 7:
ImageLink = dataSnapshot.child("8").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 8:
ImageLink = dataSnapshot.child("9").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
case 9:
ImageLink = dataSnapshot.child("10").getValue().toString();
Glide.with(viewHolder.itemView).load(ImageLink).into(viewHolder.sliderImageview);
break;
default:
return;
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public int getCount() {
return setTotalCount;
}
}
class SliderviewHolder extends SliderViewAdapter.ViewHolder {
ImageView sliderImageview;
View itemView;
public SliderviewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
sliderImageview = itemView.findViewById(R.id.imageview02);
}
}
图像滑块模型
public class ImageSliderModel {
String imageUrl;
public ImageSliderModel() {
}
public ImageSliderModel(String imageUrl){
this.imageUrl = imageUrl;
}
public String getImageUrl() {
return imageUrl;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
这些都是源代码。注意:firebase根子节点中的所有值都在正确更新,根节点中的子节点除外。
这是崩溃日志报告
enter code here
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.demo.test, PID: 30101
java.lang.NullPointerException: Attempt to invoke virtual method
'java.lang.String java.lang.Object.toString()' on a null object reference
at com.demo.test.HomePage.ImageSliderAdapter$1.onDataChange(ImageSliderAdapter.java:50)
at com.google.android.gms.internal.zzegf.zza(Unknown Source:13)
at com.google.android.gms.internal.zzeia.zzbyc(Unknown Source:2)
at com.google.android.gms.internal.zzeig.run(Unknown Source:71)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6626)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)
Disconnected from the target VM, address: 'localhost:8600', transport: 'socket'
火基模型
在ImageSliderAdapter.java
文件中,当调用dataSnapshot.child("X"). getValue()
时,由于"X"是硬编码的,因此在删除子节点后,它可能会返回null
。因此,为了使其工作,请更新ImageSliderAdapter.java
如下:
ImageSliderAdapter.java
public class ImageSliderAdapter extends SliderViewAdapter < SliderviewHolder > {
Context context;
List<String> slideLists;
public ImageSliderAdapter(Context context, List<String> slideLists) {
this.slideLists = slideLists;
this.context = context;
}
@Override
public SliderviewHolder onCreateViewHolder(ViewGroup parent) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.slider_item_layout, parent, false);
return new SliderviewHolder(view);
}
@Override
public void onBindViewHolder(final SliderviewHolder viewHolder, final int position) {
String imageUrl = slideLists.get(position);
Glide.with(viewHolder.itemView).load(imageUrl).into(viewHolder.sliderImageview);
}
@Override
public int getCount() {
return slideLists.size();
}
}
class SliderviewHolder extends SliderViewAdapter.ViewHolder {
ImageView sliderImageview;
View itemView;
public SliderviewHolder(View itemView) {
super(itemView);
this.itemView = itemView;
sliderImageview = itemView.findViewById(R.id.imageview02);
}
}
并更新HomeFragment。java
如下所示。我正在将子节点数据转换为字符串,并将其作为列表传递给适配器。因为在onBindViewHolder()
内部调用addValueEventListener()
不是一个好主意,因为它会创建多个监听器,在这种上下文中不需要这些监听器。
家庭碎片。JAVA
public class HomeFragment extends Fragment {
DatabaseReference databaseReference;
List<String> slideLists;
SliderView sliderView;
int TotalCounts;
public HomeFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
databaseReference = FirebaseDatabase.getInstance().getReference();
slideLists = new ArrayList<>();
sliderView = view.findViewById(R.id.imageSlider);
FirebaseDatabase.getInstance().getReference("ImagesLinks").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
slideLists.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
String model = snapshot.getValue().toString();
slideLists.add(model);
}
sliderView.setSliderAdapter(new ImageSliderAdapter(getActivity(), slideLists));
sliderView.setIndicatorAnimation(IndicatorAnimations.WORM);
sliderView.setSliderTransformAnimation(SliderAnimations.SIMPLETRANSFORMATION);
sliderView.setAutoCycleDirection(SliderView.AUTO_CYCLE_DIRECTION_BACK_AND_FORTH);
sliderView.setIndicatorSelectedColor(Color.WHITE);
sliderView.setIndicatorUnselectedColor(Color.LTGRAY);
sliderView.setScrollTimeInSec(3);
sliderView.startAutoCycle();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return view;
}
}
在我的应用程序中,我在回收商视图中显示项目列表。我为回收商视图中的每个项目创建了两个功能编辑和删除。使用此应用程序,用户可以创建任务列表,可以编辑项目,也可以从回收器视图中删除项目。现在,如果我单击“编辑”,它就可以完美地工作。但是,如果我单击删除选项,则应用程序崩溃,并且消息数组索引在adaper类中因rance异常而崩溃。 这是我的适配器代码。我还提到了 代码中的行 ,其中我遇到了应用程序崩溃
我使用下面的代码插入了捕获图像,并获得了< code >“not null”,这表明数据不为null。 然而,当我试图检索捕获的图像时,应用程序崩溃了。 MyDatabaseHelper.java 原因:Java . lang . illegalstateexception:无法从CursorWindow读取第0行第0列。在从游标访问数据之前,请确保游标已正确初始化。at Android . da
我有一个使用Twitter API开发的Nodejs应用程序。当托管在Heroku上时,它会出现以下错误: 2018-12-14T10:00:03.678180 00:00 heroku[web.1]:错误 R10(启动超时)- 在搜索时,我遇到了这些StackOverflow问题q1 q2,但它们都使用ExpressJS并监听固定端口,而不是Heroku提供的端口,而我的应用程序没有使用Expr
我遵循此处的指南从firebase数据库中删除值。这是我的数据结构。 这是我正在使用的代码。 发生的情况是,在这个调用中删除了整个/bookmarks节点,而不仅仅是对所需书签的引用。如何实现只删除一个书签而不是删除整个节点?
下面的代码是针对我的single.php的。php if(have_posts())调用标准wordpress功能图像,函数_exists('dfi_get_featured_images')部分获取以nivo滑块样式包装的滑块图像。 php if(have_posts())循环正在破坏脚本。我试图将其包含在if-else语句中,以仅在帖子if($featuredImages!=NULL)中没有幻