我遇到错误->。有人可以帮助我吗

时间:2020-10-19 05:46:45

标签: java android android-fragments

尝试在com.example.pcetsnmiet.Fragments.DashBoardFragment.onCreateView(DashBoardFragment.java:34)上的空对象引用上调用虚拟方法'android.view.View android.view.View.findViewById(int)' / p>

dashboardFragment.xml

<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".Fragments.DashBoardFragment"
android:background="#ECB1B1">

<com.denzcoskun.imageslider.ImageSlider
    android:id="@+id/image_slider"
    android:layout_width="match_parent"
    android:layout_height="200dp"
    app:iss_auto_cycle="true"
    app:iss_period="1000"
    app:iss_delay="0"
    android:background="#fff"

    />

DashboardFragment.java

public class DashBoardFragment extends Fragment {

ImageSlider imageSlider;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {



    imageSlider = (ImageSlider) getView().findViewById(R.id.image_slider); //this is the line error pointing at

    final List<SlideModel> remoteimages = new ArrayList<>();

    FirebaseDatabase.getInstance().getReference().child("Slider")
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for(DataSnapshot data:snapshot.getChildren())
                    {
                        remoteimages.add(new SlideModel(data.child("url").getValue().toString(),data.child("title").getValue().toString(), ScaleTypes.FIT));

                    }
                    imageSlider.setImageList(remoteimages,ScaleTypes.FIT);
                }

                @Override
                public void onCancelled(@NonNull DatabaseError error) {

                }
            });





    // Inflate the layout for this fragment
    return inflater.inflate(R.layout.fragment_dash_board, container, false);

}

}

2 个答案:

答案 0 :(得分:0)

您的代码应如下所示:

public class DashBoardFragment extends Fragment {

    ImageSlider imageSlider;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_dash_board, container, false);
        imageSlider = (ImageSlider) view.findViewById(R.id.image_slider);

        final List<SlideModel> remoteimages = new ArrayList<>();

        FirebaseDatabase.getInstance().getReference().child("Slider")
                .addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(@NonNull DataSnapshot snapshot) {
                        for (DataSnapshot data : snapshot.getChildren()) {
                            remoteimages.add(new SlideModel(data.child("url").getValue().toString(), data.child("title").getValue().toString(), ScaleTypes.FIT));
                        }
                        imageSlider.setImageList(remoteimages, ScaleTypes.FIT);
                    }

                    @Override
                    public void onCancelled(@NonNull DatabaseError error) {

                    }
                });
        return view;
    }
}

答案 1 :(得分:0)

您使用了错误的方法。您首先需要定义布局,然后从中获取视图,如下所示-

View view = inflater.inflate(R.layout.fragment_dash_board, container, false);
imageSlider = (ImageSlider) view.findViewById(R.id.image_slider); 

//let other code remain same


return view;