我遇到一个错误,能有人帮我吗?

huangapple go评论86阅读模式
英文:

I am getting an error ->.can someone help me plz

问题

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.example.pcetsnmiet.Fragments.DashBoardFragment.onCreateView(DashBoardFragment.java:34)
dashboardFragment.xml

<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"/>

</RelativeLayout>
DashboardFragment.java

public class DashBoardFragment extends Fragment {

    ImageSlider imageSlider;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_dash_board, container, false);

        imageSlider = rootView.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 rootView;
    }
}
英文:

Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference at com.example.pcetsnmiet.Fragments.DashBoardFragment.onCreateView(DashBoardFragment.java:34)

dashboardFragment.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;

&lt;RelativeLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
tools:context=&quot;.Fragments.DashBoardFragment&quot;
android:background=&quot;#ECB1B1&quot;&gt;

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

    /&gt;

</RelativeLayout>

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&lt;SlideModel&gt; remoteimages = new ArrayList&lt;&gt;();

    FirebaseDatabase.getInstance().getReference().child(&quot;Slider&quot;)
            .addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(@NonNull DataSnapshot snapshot) {
                    for(DataSnapshot data:snapshot.getChildren())
                    {
                        remoteimages.add(new SlideModel(data.child(&quot;url&quot;).getValue().toString(),data.child(&quot;title&quot;).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);

}

}

答案1

得分: 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;
    }
}
英文:

Your code should look like this :

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&lt;SlideModel&gt; remoteimages = new ArrayList&lt;&gt;();

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

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

                    }
                });
        return view;
    }
}

</details>



# 答案2
**得分**: 0

你正在使用错误的方法。你需要首先定义你的布局,然后按照以下方式从中获取视图 -

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

// 其他代码保持不变

return view;
英文:

You are using wrong approach. You need to define your layout at first , then get views from it as below -

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;

huangapple
  • 本文由 发表于 2020年10月19日 13:46:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/64421762.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定