英文:
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
<?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"
/>
</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<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);
}
}
答案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<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;
}
}
</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;
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论