我正在尝试从firebase数据库中检索值并将其存储在列表视图中但应用程序正在关闭。 logCat中的异常即将发生
java.lang.NullPointerException:尝试调用虚方法'void android.widget.ListView.setAdapter(android.widget.ListAdapter)'上的一个 null对象引用 在 com.example.dell.week11.MainActivity $ 1.onDataChange(MainActivity.java:60)
这是主要活动
package com.example.dell.week11;
import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import org.w3c.dom.Text;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ListView listView;
DatabaseReference databasefaq;
List<Faq> faqList;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databasefaq= FirebaseDatabase.getInstance().getReference("FAQ");
listView=(ListView)findViewById(R.id.listView);
faqList=new ArrayList<>();
}
@Override
protected void onStart() {
super.onStart();
databasefaq.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
faqList.clear();
for(DataSnapshot faqSnapshot:dataSnapshot.getChildren())
{
Faq faq=faqSnapshot.getValue(Faq.class);
faqList.add(faq);
}
FaqList adapter=new FaqList(MainActivity.this,faqList);
listView.setAdapter(adapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.menu,menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==R.id.add)
{
Intent i=new Intent(MainActivity.this,AddActivity.class);
startActivity(i);
}
return super.onOptionsItemSelected(item);
}
}
接下来是CustomAdapter FaqList:
package com.example.dell.week11;
import android.app.Activity;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
/**
* Created by Dell on 4/7/2017.
*/
public class FaqList extends ArrayAdapter<Faq> {
private Activity context;
List<Faq> faqList;
public FaqList(Activity context,List<Faq> faqList){
super(context,R.layout.list_layout,faqList);
this.context=context;
this.faqList=faqList;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=context.getLayoutInflater();
View listViewItem=inflater.inflate(R.layout.list_layout,null,true);
TextView q=(TextView)listViewItem.findViewById(R.id.questiontv);
TextView a=(TextView)listViewItem.findViewById(R.id.answertv);
Faq faq=faqList.get(position);
q.setText(faq.getFaqQuestion());
a.setText(faq.getFaqAnswer());
return listViewItem;
}
}
这是Model类
package com.example.dell.week11;
/**
* Created by Dell on 4/5/2017.
*/
public class Faq {
String faqid,faqQuestion,faqAnswer;
public Faq() {
}
public Faq(String faqid, String faqQuestion, String faqAnswer) {
this.faqid = faqid;
this.faqQuestion = faqQuestion;
this.faqAnswer = faqAnswer;
}
public String getFaqid() {
return faqid;
}
public String getFaqQuestion() {
return faqQuestion;
}
public String getFaqAnswer() {
return faqAnswer;
}
}
答案 0 :(得分:0)
您没有在onCreate方法中调用setContentView
。
答案 1 :(得分:0)
确保在找到任何ID之前调用setContentView(R.whatever)
并在布局XML中找到ListView,必须定义为
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databasefaq= FirebaseDatabase.getInstance().getReference("FAQ");
setContentView(R.layout.xyz); //you missed this line
listView=(ListView)findViewById(R.id.listView);
faqList=new ArrayList<>();
}