我正在通过RecyclerView适配器将数据从Firebase数据库传递到Activity中的RecyclerView。
我看不到任何错误,但是每次我尝试访问此活动时,应用都会崩溃。
此活动还具有一些微调器,可用于创建过滤器以仅从数据库中选择一些对象。但是即使我删除了这部分代码,它仍然无法正常工作。
RecyclerView活动:
public class Lista extends AppCompatActivity {
//ListView listatocha;
DatabaseReference bbdd;
Button btnreestablecer;
RecyclerView rcvg;
Adaptagrande rAdapterg;
RecyclerView.LayoutManager layoutManagerg;
List<Juego> juegostocha;
Spinner spncons;
Spinner spnform;
Spinner spnpos;
Spinner spnavan;
Spinner spnvec;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lista);
// listatocha = (ListView) findViewById(R.id.lstvwlistgrand);
btnreestablecer = (Button) findViewById(R.id.btntitlsrc);
rcvg = (RecyclerView) findViewById(R.id.recyclergrande);
rcvg.setLayoutManager(new LinearLayoutManager(getApplicationContext())); //a ver si
juegostocha = new ArrayList<>();
rAdapterg = new Adaptagrande(getApplicationContext(),juegostocha);
rcvg.setAdapter(rAdapterg);
bbdd = FirebaseDatabase.getInstance().getReference("juegos");
spncons = (Spinner) findViewById(R.id.spnplataformal);
spnform = (Spinner) findViewById(R.id.spnformatol);
spnpos = (Spinner) findViewById(R.id.spnposesionl);
spnavan = (Spinner) findViewById(R.id.spnavancel);
spnvec = (Spinner) findViewById(R.id.spnvecesl);
/*
spncons.setSelection(0);
spnform.setSelection(0);
spnpos.setSelection(0);
spnavan.setSelection(0);
spnvec.setSelection(0);
*/
String[] plataformas = new String[] {"Plataforma","PC", "PS2", "PS3", "PS4", "N64", "Gamecube", "Wii", "WiiU", "NSwitch", "Xbox", "Xbox360", "XboxOne", "PSone", "GameBoy", "GBA", "DS", "3DS", "PSP", "PSVita"}; //2-8 | 2.2 3.8 4.2 5.1 6.1 7.4 8.1
String[] avance = new String[] {"Avance","Nulo", "Probado", "Intermedio", "Pasado", "Completado", "Irrelevante"}; //4-11 | 4.1 6.1 7.1 10.2 11.1
String[] veces = new String[] {"Veces","0","1","2","3-5","+5"}; //0-3
String[] posesion = new String[] {"Posesión","Si","No"}; //siempre igual
String[] formato = new String[] {"Físico","Si","No"}; //siempre igual
ArrayAdapter<String> adaptaforms = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, plataformas);
ArrayAdapter<String> adaptavanc = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, avance);
ArrayAdapter<String> adaptveces = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, veces);
ArrayAdapter<String> adaptapose = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, posesion);
ArrayAdapter<String> adaptaform = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, formato);
spncons.setAdapter(adaptaforms);
spnvec.setAdapter(adaptveces);
spnavan.setAdapter(adaptavanc);
spnpos.setAdapter(adaptapose);
spnform.setAdapter(adaptaform);
spncons.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cosa();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spnpos.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cosa();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spnform.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cosa();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spnavan.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cosa();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
spnvec.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
cosa();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
btnreestablecer.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
spncons.setSelection(0);
spnform.setSelection(0);
spnpos.setSelection(0);
spnavan.setSelection(0);
spnvec.setSelection(0);
}
});
}
protected void cosa(){
bbdd.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
juegostocha.clear();
//ArrayAdapter<String> adaptadorr;
//ArrayList<String> lista = new ArrayList<String>();
String busqueda = "Buscando ";
for (DataSnapshot datasnapshot: dataSnapshot.getChildren()) {
dataSnapshot.getValue(Juego.class);
Juego juego = datasnapshot.getValue(Juego.class);
String titulo = juego.getTitulo();
String plataforma = juego.getPlataforma();
String poses = juego.getPosesion();
String forma = juego.getFisico();
String avanc = juego.getAvance();
String veces = juego.getVeces();
//String juegolst = titulo;
//esto antes iba por listview
//juegolst = juegolst+" | "+" | "+plataforma+" | "+poses+" | "+forma+" | "+avanc+" | "+veces;
if (spncons.getSelectedItem().toString().equals("Plataforma")&&spnform.getSelectedItem().toString().equals("Físico")&&spnpos.getSelectedItem().toString().equals("Posesión")&&spnavan.getSelectedItem().toString().equals("Avance")&&spnvec.getSelectedItem().toString().equals("Veces")){
//lista.add(juegolst);
juegostocha.add(juego);
busqueda = "Buscando todos los ";
}else{
if (plataforma.equals(spncons.getSelectedItem().toString())|| spncons.getSelectedItem().toString().equals("Plataforma")){
if (poses.equals(spnpos.getSelectedItem().toString())|| spnpos.getSelectedItem().toString().equals("Posesión")){
if (forma.equals(spnform.getSelectedItem().toString())|| spnform.getSelectedItem().toString().equals("Físico")){
if (avanc.equals(spnavan.getSelectedItem().toString())|| spnavan.getSelectedItem().toString().equals("Avance")){
int numveces = Integer.parseInt(veces);
if (spnvec.getSelectedItem().toString().equals("Veces")){
//lista.add(juegolst);
juegostocha.add(juego);
}else{
switch (numveces) {
case 0:
if (spnvec.getSelectedItem().toString().equals("0")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
case 1:
if (spnvec.getSelectedItem().toString().equals("1")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
case 2:
if (spnvec.getSelectedItem().toString().equals("2")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
case 3:
if (spnvec.getSelectedItem().toString().equals("3-5")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
case 4:
if (spnvec.getSelectedItem().toString().equals("3-5")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
case 5:
if (spnvec.getSelectedItem().toString().equals("3-5")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
default:
if (spnvec.getSelectedItem().toString().equals("+5")){
//lista.add(juegolst);
juegostocha.add(juego);
}
break;
}
}
}
}
}
}
}
}
rAdapterg.notifyDataSetChanged();
//adaptadorr = new ArrayAdapter<String>(Lista.this,android.R.layout.simple_list_item_1,lista);
// listatocha.setAdapter(adaptadorr);
if (spncons.getSelectedItem().toString().equals("Plataforma")){
busqueda = busqueda + "juegos para cualquier plataforma, ";
}else{
busqueda = busqueda + "juegos para "+spncons.getSelectedItem().toString()+", ";
}
if (spnpos.getSelectedItem().toString().equals("Si")){
busqueda = busqueda + "en posesión, ";
}else{
if (spnpos.getSelectedItem().toString().equals("No")){
busqueda = busqueda + "NO en posesión, ";
}
}
if (spnform.getSelectedItem().toString().equals("Si")){
busqueda = busqueda + "en físico, ";
}else{
if (spnform.getSelectedItem().toString().equals("No")){
busqueda = busqueda + "en digital, ";
}else{
busqueda = busqueda + "en cualquier formato, ";
}
}
busqueda = busqueda +"con un grado de avance ";
if (spnavan.getSelectedItem().toString().equals("Avance")){
busqueda = busqueda+"cualquiera y pasado ";
}else{
busqueda = busqueda+spnavan.getSelectedItem().toString()+" y pasado ";
}
if (spnvec.getSelectedItem().toString().equals("Veces")){
busqueda = busqueda+"cuantas veces sea";
}else{
busqueda = busqueda+spnvec.getSelectedItem().toString()+" veces.";
}
//Toast toast = Toast.makeText(getApplicationContext(),busqueda,Toast.LENGTH_LONG);
//toast.show();
//Ahora es un snackbar lo que se crea con la info. de la búsqueda, y se queda hasta que la cierres
final Snackbar snackBar = Snackbar.make(findViewById(android.R.id.content), busqueda, Snackbar.LENGTH_INDEFINITE);
snackBar.setAction("Entendido", new View.OnClickListener() {
@Override
public void onClick(View v) {
snackBar.dismiss();
}
});
snackBar.show();
} //aqui acaba el on data change
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
Adapter Java文件:
public class Adaptagrande extends RecyclerView.Adapter<Adaptagrande.jHolder>{
List<Juego> jueguiss;
private Context micontext;
public Adaptagrande(Context micontext, List<Juego> jueguiss){
this.jueguiss = jueguiss;
this.micontext = micontext;
}
@NonNull
@Override
public jHolder onCreateViewHolder(ViewGroup grupo, int i){
View vv = LayoutInflater.from(grupo.getContext()).inflate(R.layout.layout_juegofull, grupo, false);
jHolder holdeer = new jHolder(vv);
return holdeer;
}
@Override
public void onBindViewHolder(final jHolder holder, final int pos){
final Juego j1 = jueguiss.get(pos);
holder.titxtlist.setText(j1.getTitulo());
holder.platxtlist.setText(j1.getPlataforma());
holder.posetxtlist.setText(j1.getPosesion());
holder.formatxtlist.setText(j1.getFisico());
holder.avatxtlist.setText(j1.getAvance());
holder.vectxtlist.setText(j1.getVeces());
}
@Override
public int getItemCount() {
return jueguiss.size();
}
public static class jHolder extends RecyclerView.ViewHolder{
public TextView titxtlist;
public TextView platxtlist;
public TextView posetxtlist;
public TextView formatxtlist;
public TextView avatxtlist;
public TextView vectxtlist;
public jHolder(View itemViewer){
super(itemViewer);
titxtlist=(TextView) itemView.findViewById(R.id.titltxt);
platxtlist=(TextView) itemView.findViewById(R.id.platxtlist);
posetxtlist=(TextView) itemView.findViewById(R.id.posetxtlist);
formatxtlist=(TextView) itemView.findViewById(R.id.formatxtlist);
avatxtlist=(TextView) itemView.findViewById(R.id.avatxtlist);
vectxtlist=(TextView) itemView.findViewById(R.id.vectxtlist);
}
}
}
每个Recycler对象的XML文件:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#505050"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:id="@+id/titxtlist"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/platxtlist"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/posetxtlist"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/formatxtlist"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/avatxtlist"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="@+id/vectxtlist"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
RecyclerView活动的XML文件:
<LinearLayout 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"
android:background="#919191"
android:orientation="vertical"
tools:context=".Lista">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top"
android:orientation="horizontal"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp">
<Button
android:id="@+id/btntitlsrc"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Todos" />
<Spinner
android:id="@+id/spnplataformal"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B6A9A9" />
<Spinner
android:id="@+id/spnposesionl"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B6A9A9" />
<Spinner
android:id="@+id/spnformatol"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B6A9A9" />
<Spinner
android:id="@+id/spnavancel"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B6A9A9" />
<Spinner
android:id="@+id/spnvecesl"
android:layout_width="30dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#B6A9A9" />
</LinearLayout>
<!--
<ListView
android:id="@+id/lstvwlistgrand"
android:layout_width="match_parent"
android:layout_height="match_parent" />
-->
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclergrande"
/>
</LinearLayout>
Logcat这样说:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.prueba.Adaptagrande.onBindViewHolder(Adaptagrande.java:37)
at com.example.prueba.Adaptagrande.onBindViewHolder(Adaptagrande.java:15)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:6781)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:6823)
at android.support.v7.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6019)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5858)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5854)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2230)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1557)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1517)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:612)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3924)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3641)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4194)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:444)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1812)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1656)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1565)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:323)
at android.widget.FrameLayout.onLayout(FrameLayout.java:261)
at com.android.internal.policy.DecorView.onLayout(DecorView.java:753)
at android.view.View.layout(View.java:20672)
at android.view.ViewGroup.layout(ViewGroup.java:6194)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2792)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2319)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1460)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:7183)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:949)
at android.view.Choreographer.doCallbacks(Choreographer.java:761)
at android.view.Choreographer.doFrame(Choreographer.java:696)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:935)
at android.os.Handler.handleCallback(Handler.java:873)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:193)
2019-05-31 18:04:00.987 6377-6377/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
但是我看不到空引用在哪里或接收或转换数据的错误
答案 0 :(得分:1)
可能只是一个错字.. titxtlist的ID错误。您正在通过使用id作为
在您的视图持有者中找到textviewtitxtlist=(TextView) itemView.findViewById(R.id.titltxt);
但是在您的布局文件中是
android:id="@+id/titxtlist"
您的观看者中的ID不同