我想基于子类别节点获取内容节点。意味着所有带有有趣子类别的内容都应显示在列表视图中,并且为此我要进行过滤,但是当我获取内容节点时,我会得到NullPointerException。
这是我的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
imageView = findViewById(R.id.backbtncntent);
sharebtn = findViewById(R.id.sharebtn);
textView = findViewById(R.id.contenttext);
listView = findViewById(R.id.listview);
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(ContentActivity.this, android.R.layout.simple_list_item_1,contentname);
listView.setAdapter(arrayAdapter);
String classname = getIntent().getStringExtra("classname");
if(classname.equals("A")){
content = getIntent().getStringExtra("jokecontent");
funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes").orderByChild("subcategory")
.equalTo(content);
funnyJokesQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
for(DataSnapshot item : dataSnapshot.getChildren()) {
String contentdisplay = item.child("content").getValue().toString(); //in this line i am getting error
contentname.add(contentdisplay);
arrayAdapter.notifyDataSetChanged();
}
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}else if(classname.equals("B")){
content = getIntent().getStringExtra("shayaricontent");
textView.setText(content);
}
imageView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(ContentActivity.this, HomeActivity.class);
startActivity(i);
}
});
sharebtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Hey Read Some Interesting Content");
intent.putExtra(android.content.Intent.EXTRA_TEXT, content);
startActivity(Intent.createChooser(intent, "Share Via"));
}
});
}
}
我收到此错误:
2020-06-24 11:05:59.155 27216-27216/example.example1.shayariapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: example.example1.shayariapp, PID: 27216
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
at example.example1.shayariapp.Acitivties.ContentActivity$1.onChildAdded(ContentActivity.java:52)
at com.google.firebase.database.core.ChildEventRegistration.fireEvent(com.google.firebase:firebase-database@@16.0.4:79)
at com.google.firebase.database.core.view.DataEvent.fire(com.google.firebase:firebase-database@@16.0.4:63)
at com.google.firebase.database.core.view.EventRaiser$1.run(com.google.firebase:firebase-database@@16.0.4:55)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:202)
at android.app.ActivityThread.main(ActivityThread.java:6962)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:528)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:888)
请告诉我如何解决此错误。这将是很大的帮助。
答案 0 :(得分:0)
由于您正在侦听子节点,因此您的 account name balance clasif_1 clasif_2 clasif_3 clasif_4 level last_level
0 1.00.00.00 Total Assets 1000 Total Assets 1 False
1 1.01.00.00 Cash 200 Total Assets Cash 2 True
2 1.02.00.00 Inventory 800 Total Assets Inventory 2 False
3 1.02.01.00 Goods in process 300 Total Assets Inventory Goods in process 3 True
4 1.02.02.00 Goods for sale 500 Total Assets Inventory Goods for sale 3 True
5 2.00.00.00 Liabilities 750 Liabilities 1 False
6 2.01.00.00 Commercial Liabilities 700 Liabilities Commercial Liabilities 2 True
7 2.02.00.00 Other liabilities 50 Liabilities Other liabilities 2 True
8 3.00.00.00 Net equity 250 Net equity 1 True
方法将为每个子节点调用onChild...
。因此,无需遍历其中的DataSnapshot
。
snapshot.getChildren()
如果您将funnyJokesQuery = FirebaseDatabase.getInstance().getReference("ContentJokes")
.orderByChild("subcategory")
.equalTo(content);
funnyJokesQuery.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String contentdisplay = item.child("content").getValue(String.class);
contentname.add(contentdisplay);
arrayAdapter.notifyDataSetChanged();
}
附加到相同的查询/引用,则需要循环,因为在这种情况下,ValueEventListener
会被所有孩子使用一个onDataChange
来调用。 >