android - 我正在尝试调用 DisplayMatch 给 listviewMatch.setAdapter(adapter) 中的错误,如果可能的话,请帮助我提供任何替代方案
问题描述
**bottom_nav_bar.xml**
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:id="@+id/fragment_container">
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="@+id/bottom_nav_menu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cnb_menuResource="@menu/bottom_nav_menu"
android:layout_alignParentBottom="true"
android:background="@drawable/round_corners"
app:cnb_unselectedColor="@color/black"
app:cnb_radius="8dp"
app:cnb_orientationMode="horizontal"
android:layout_margin="30dp"
android:elevation="8dp"
/>
</RelativeLayout>
**list_view_data.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/lst"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
**match_view.xml**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/black"
android:orientation="vertical">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="5dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="@+id/imageView2"
android:layout_width="match_parent"
android:layout_height="150dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
app:srcCompat="@drawable/pubg_test" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
**bottom_nav_menu.xml**
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="@+id/bottom_nav_dashboard"
android:title="Dashboard"
android:icon="@drawable/bottom_nav_dashboard_icon"
app:cnb_backgroundColor="#FFC107"
/>
<item
android:id="@+id/bottom_nav_manage"
android:title="Manage"
android:icon="@drawable/bottom_nav_manage_icon"
app:cnb_backgroundColor="#FFC107"
/>
<item
android:id="@+id/bottom_nav_profile"
android:title="Profile"
android:icon="@drawable/bottom_nav_profile_icon"
app:cnb_backgroundColor="#FFC107"
/>
</menu>
public class DashBoardTest extends AppCompatActivity {
ChipNavigationBar chipNavigationBar;
ListView listviewMatch;
DatabaseReference displayMatch;
List<MatchData> matchData;
private Boolean ID=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bottom_nav_bar);
chipNavigationBar = findViewById(R.id.bottom_nav_menu);
matchData = new ArrayList<>();
displayMatch = FirebaseDatabase.getInstance().getReference("match");
chipNavigationBar.setItemSelected(R.id.bottom_nav_manage, true);
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new UserTournment2()).commit();
NavbottomMenu();
if (!ID)
{
DisplayMatch();
}
}
@SuppressLint("NonConstantResourceId")
private void NavbottomMenu() {
chipNavigationBar.setOnItemSelectedListener(i -> {
Fragment fragment = null;
switch (i) {
case R.id.bottom_nav_dashboard:
fragment = new UserHome();
break;
case R.id.bottom_nav_manage:
fragment = new UserTournment2();
break;
case R.id.bottom_nav_profile:
fragment = new UserDetails();
break;
}
assert fragment != null;
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, fragment).commit();
});
}
public void DisplayMatch() {
listviewMatch = findViewById(R.id.lst);
displayMatch.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
matchData.clear();
for (DataSnapshot artistSnapshot : dataSnapshot.getChildren()) {
MatchData videos = artistSnapshot.getValue(MatchData.class);
matchData.add(videos);
}
MyListAdapter adapter = new MyListAdapter(DashBoardTest.thi`enter code here`s, matchData);
listviewMatch.setAdapter(adapter);
//retrive data
//listviewMatch.setOnItemClickListener(this));
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
toastMessage("oncanelled");
}
});
}
public void toastMessage(String i) {
Toast.makeText(this, i, Toast.LENGTH_SHORT).show();
}
}
public class UserTournment2 extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Toast.makeText(getContext(), "fragment called", Toast.LENGTH_SHORT).show();
// Inflate the layout for this fragment
return inflater.inflate(R.layout.list_view_data, container, false);
}
}
public class MyListAdapter extends ArrayAdapter<MatchData> {
private Activity context;
private List<MatchData> matchData;
public MyListAdapter(Activity context, List<MatchData> matchData) {
super(context, R.layout.match_view, matchData);
// TODO Auto-generated constructor stub
this.context=context;
this.matchData=matchData;
}
public View getView(int position, View view, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View rowView=inflater.inflate(R.layout.match_view, null,true);
return rowView;
};
}
public class MatchData {
String name, register_id, time, type;
public MatchData() {
}
public MatchData(String name, String register_id, String time, String type) {
this.name = name;
this.register_id = register_id;
this.time = time;
this.type = type;
}
public String getName() {
return name;
}
public String getRegister_id() {
return register_id;
}
public String getTime() {
return time;
}
public String getType() {
return type;
}
}
错误
2021-07-10 01:04:02.114 15625-15625/me.aksamitsah.pubgtest E/AndroidRuntime:致命异常:主进程:me.aksamitsah.pubgtest,PID:15625 java.lang.NullPointerException:尝试调用虚拟方法' void android.widget.ListView.setAdapter(android.widget.ListAdapter)' 在 com.google.firebase.database.core 的 me.aksamitsah.pubgtest.DashBoardTest$1.onDataChange(DashBoardTest.java:100) 的空对象引用上。 ValueEventRegistration.fireEvent(ValueEventRegistration.java:75) at com.google.firebase.database.core.view.DataEvent.fire(DataEvent.java:63) at com.google.firebase.database.core.view.EventRaiser$1.run (EventRaiser.java:55) 在 android.os.Handler.handleCallback(Handler.java:914) 在 android.os.Handler.dispatchMessage(Handler.java:100) 在 android.os.Looper.loop(Looper.java: 224) 在 android.app.ActivityThread.main(ActivityThread.java:7551) 在 java.lang.reflect.Method.invoke(Native Method) 在 com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:539) 在 com.android.internal.os.ZygoteInit.main( ZygoteInit.java:995)
解决方案
推荐阅读
- java - 从 POSTMAN 收到的数据与 JAVA rest api 调用相比不匹配
- elasticsearch - 什么是 ElasticSearch 中的最大大小索引
- azure - Azure YAML 部署非常慢
- spring - XPath 语句没有生成节点来验证 Spring Boot SOAP 请求
- android - 使用泛型实现 Kotlin 动态数组
- blueprism - 如何在 BluePrism Calculation 中替换字符串
- c++ - 可视化向量范数的问题(OpenGL C++)
- c# - Jira .NET SDK:issue.Savechange() 抛出未知异常
- go - 在 Go 中获取最新的“星期一 05:30”Unix 时间?
- node.js - 如何从 Cloud Storage for Firebase 获取在特定时间创建的文件?