gpt4 book ai didi

java - 尝试调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)

转载 作者:行者123 更新时间:2023-12-01 18:25:05 26 4
gpt4 key购买 nike

大家好,我正在尝试从 Firebase 数据库进行实时搜索并显示数据,但每次都会收到以下错误,我不明白如何修复它。谁能帮我解决它吗?我是 Android 新手,不明白发生了什么。如果有人能向我建议我应该做什么,那就太不可思议了。先感谢您。

Firebase 搜索 Activity

package com.example.ipill;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;


public class FirebaseSearch extends AppCompatActivity {

private EditText mSearchField;
private ImageButton mSearchBtn;

private RecyclerView mResultList;

private DatabaseReference mUserDatabase;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mUserDatabase = FirebaseDatabase.getInstance().getReference("Users");


mSearchField = (EditText) findViewById(R.id.search_field);

mSearchBtn = findViewById(R.id.imageButton);
mResultList = (RecyclerView) findViewById(R.id.view);

mResultList.setHasFixedSize(true);
mResultList.setLayoutManager(new LinearLayoutManager(this));

mSearchBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

String searchText = mSearchField.getText().toString();

firebaseUserSearch(searchText);

}
});

}

private void firebaseUserSearch(String searchText) {


Query query = FirebaseDatabase.getInstance()
.getReference("ipill2")
.child("Nome")
.startAt(searchText)
.endAt(searchText + "\uf8ff");

FirebaseRecyclerOptions<Users> options =
new FirebaseRecyclerOptions.Builder<Users>()
.setQuery(query, Users.class)
.build();


FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Users, UsersViewHolder>(options) {

@NonNull
@Override
public UsersViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return null;
}

@Override
protected void onBindViewHolder(@NonNull UsersViewHolder viewHolder, int position, @NonNull Users model) {
viewHolder.setDetails(getApplicationContext(), model.getNome(), model.getCognome());
}
};

mResultList.setAdapter(adapter);

}


// View Holder Class

public static class UsersViewHolder extends RecyclerView.ViewHolder {

View mView;

public UsersViewHolder(View itemView) {
super(itemView);

mView = itemView;

}

public void setDetails(Context ctx, String nome, String cognome) {

TextView user_name = (TextView) mView.findViewById(R.id.NomeDettagli);
TextView user_cognome = (TextView) mView.findViewById(R.id.cognomeDetail);


user_name.setText(nome);
user_cognome.setText(cognome);


}

}

}

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="match_parent"
tools:context=".FirebaseSearch">


<TextView
android:id="@+id/heading_label"
android:layout_width="327dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="43dp"
android:layout_marginTop="38dp"
android:layout_marginEnd="41dp"
android:layout_marginBottom="642dp"
android:text="Firebase Search"
android:textSize="36sp" />

<EditText
android:id="@+id/search_field"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="41dp"
android:layout_marginTop="115dp"
android:layout_marginEnd="156dp"
android:layout_marginBottom="571dp"
android:background="@drawable/search_layout"
android:ems="10"
android:inputType="textPersonName"
android:paddingLeft="20dp"
android:paddingTop="10dp"
android:paddingRight="20dp"
android:paddingBottom="10dp"
android:text="Search here"
android:textSize="12sp" />

<ImageButton
android:id="@+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="290dp"
android:layout_marginTop="117dp"
android:layout_marginEnd="73dp"
android:layout_marginBottom="571dp"
app:srcCompat="@drawable/ic_search_black_24dp" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/view"
android:layout_width="wrap_content"
android:layout_height="550dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="-4dp"
android:layout_marginTop="181dp"
android:layout_marginEnd="0dp"
android:layout_marginBottom="0dp" />

</RelativeLayout>

依赖关系

  apply plugin: 'com.android.application'
apply plugin: 'com.google.gms.google-services'

android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.ipill"
minSdkVersion 29
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-
rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.android.support:appcompat-v7:29.1.0'
implementation 'com.android.support:design:29.1.0'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.picasso:picasso:2.5.2'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'

implementation 'androidx.constraintlayout:constraintlayout:1.1.3'

implementation 'com.google.android.gms:play-services:8.4.0'
implementation 'com.google.android.gms:play-services-maps:15.0.1'
implementation 'com.google.android.gms:play-services-location:15.0.1'
implementation 'com.google.android.gms:play-services-auth:9.2.1'
// Add the Firebase SDK for Google Analytics
// Add the SDKs for any other Firebase products you want to use in your app
// For example, to use Firebase Authentication and Cloud Firestore
implementation 'com.google.firebase:firebase-auth:17.0.0'
implementation 'com.google.firebase:firebase-firestore:21.3.1'
implementation 'com.google.firebase:firebase-analytics:17.2.2'
implementation 'com.google.firebase:firebase-core:17.2.0'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-storage:19.1.0'
testImplementation 'junit:junit:4.12'

androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'

implementation 'com.google.android.gms:play-services-ads:18.3.0'
implementation 'com.google.android.gms:play-services-auth:17.0.0'

implementation 'com.github.bumptech.glide:glide:4.11.0'

annotationProcessor 'com.github.bumptech.glide:compiler:4.11.0'

implementation 'com.firebaseui:firebase-ui-database:6.2.0'

}

我遇到的错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ipill, PID: 11437
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ipill/com.example.ipill.FirebaseSearch}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3271)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7403)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.ipill.FirebaseSearch.onCreate(FirebaseSearch.java:43)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1307)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3246)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3410) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at
android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2017) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7403) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:935) enter code here

最佳答案

更改您的 recyclerview ID 名称不使用 View 。是关键字

关于java - 尝试调用虚拟方法 'void androidx.recyclerview.widget.RecyclerView.setHasFixedSize(boolean),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60241693/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com