我从最新的Android Studio创建默认的抽屉布局。我想在Firebase上放置一些图片和名称,然后从Firebase检索它,并在我的家庭片段回收器视图中显示带有名称的图片。我成功存储并检索了数据,但是当我在回收站上设置数据时,我的应用程序崩溃了。我提供了我编辑过的所有布局和Java文件。请帮我。我如何摆脱这种崩溃并将数据放在回收器视图中。
HomeFragment.java ,在这里,我认为当我使用此recyclerView.setAdapter(myAdapter)行时,它会崩溃。
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
RecyclerView recyclerView;
MyAdapter myAdapter;
List<Upload> uploadList;
DatabaseReference databaseReference;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = root.findViewById(R.id.recyclerviewid);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
uploadList = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference("Upload");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for(DataSnapshot dataSnapshot1 :dataSnapshot.getChildren())
{
Upload upload = dataSnapshot1.getValue(Upload.class);
uploadList.add(upload);
}
myAdapter = new MyAdapter(getActivity().getApplicationContext(),uploadList);
recyclerView.setAdapter(myAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(),"Error "+ databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
return root;
}
}
HomeViewModel.java
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
}
}
MainActivity.java ,我在创建默认抽屉布局后不对其进行编辑
public class MainActivity extends AppCompatActivity {
private AppBarConfiguration mAppBarConfiguration;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_home, R.id.nav_gallery, R.id.nav_slideshow)
.setDrawerLayout(drawer)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
public void goAdmin(MenuItem item) {
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
startActivity(intent);
}
}
** MyAdapter.java **
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.MyViewHolder> {
public Context context;
public List<Upload> uploadlist;
ViewGroup viewGroup;
MyViewHolder myViewHolder;
public MyAdapter(Context context, List<Upload> uploadimge) {
this.context = context;
this.uploadlist = uploadimge;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.listview, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Upload upload= uploadlist.get(position);
myViewHolder.textView.setText(upload.imageName);
Picasso.with(context).load(upload.uri).fit().centerCrop().into(myViewHolder.imageView);
}
@Override
public int getItemCount() {
return uploadlist.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
public TextView textView;
public ImageView imageView;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
textView =(TextView) itemView.findViewById(R.id.cardtvid);
imageView =(ImageView) itemView.findViewById(R.id.cardimgid);
}
}
}
Upload.java
public class Upload {
public String imageName;
public String uri;
public Upload()
{
}
public Upload(String s1,String s2)
{
this.imageName=s1;
this.uri=s2;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
fragment_home.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="match_parent"
tools:context=".ui.home.HomeFragment">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/recyclerviewid" />
</LinearLayout>
LogCat
03-17 16:17:54.657 17920-17920/? I/art: Late-enabling -Xcheck:jni
03-17 16:17:54.721 17920-17931/? I/art: Background sticky concurrent mark sweep GC freed 6421(305KB) AllocSpace objects, 0(0B) LOS objects, 33% free, 3MB/5MB, paused 10.119ms total 31.195ms
03-17 16:17:54.721 17920-17929/? I/System: FinalizerDaemon: finalize objects = 1
03-17 16:17:54.964 17920-17920/com.example.islamicproducts W/System: ClassLoader referenced unknown path: /data/app/com.example.islamicproducts-1/lib/arm
03-17 16:17:56.176 17920-17962/com.example.islamicproducts D/OpenSSLLib: OpensslErr:Module:12(116:176); file:external/boringssl/src/crypto/asn1/asn1_lib.c ;Line:186;Function:ASN1_get_object
03-17 16:17:56.405 17920-17931/com.example.islamicproducts I/art: Background partial concurrent mark sweep GC freed 5923(588KB) AllocSpace objects, 4(80KB) LOS objects, 24% free, 4MB/6MB, paused 674us total 190.059ms
03-17 16:17:56.407 17920-17920/com.example.islamicproducts W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter androidx.vectordrawable.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
03-17 16:17:56.714 17920-17920/com.example.islamicproducts I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
03-17 16:17:56.715 17920-17920/com.example.islamicproducts I/art: Rejecting re-init on previously-failed class java.lang.Class<androidx.core.view.ViewCompat$2>
03-17 16:17:57.898 17920-17920/com.example.islamicproducts D/OpenGLRenderer: Dumper init 2 threads <0xb97cc948>
03-17 16:17:57.899 17920-17920/com.example.islamicproducts D/OpenGLRenderer: <com.example.islamicproducts> is running.
03-17 16:17:57.902 17920-18015/com.example.islamicproducts D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: false
03-17 16:17:57.902 17920-18015/com.example.islamicproducts D/OpenGLRenderer: CanvasContext() 0xb97cda58
03-17 16:17:58.129 17920-17931/com.example.islamicproducts W/art: Suspending all threads took: 21.357ms
03-17 16:17:58.217 17920-17931/com.example.islamicproducts I/art: Background sticky concurrent mark sweep GC freed 6564(706KB) AllocSpace objects, 0(0B) LOS objects, 12% free, 5MB/6MB, paused 99.131ms total 187.979ms
03-17 16:17:58.267 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: hostname=islamicproducts-51994.firebaseio.com; servname=(null); netid=0; mark=0
03-17 16:17:58.267 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
03-17 16:17:58.274 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: hostname=islamicproducts-51994.firebaseio.com; servname=(null); netid=0; mark=0
03-17 16:17:58.274 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=1024; ai_family=0
03-17 16:17:58.382 17920-18015/com.example.islamicproducts D/OpenGLRenderer: CanvasContext() 0xb97cda58 initialize window=0xb97678e8, title=com.example.islamicproducts/com.example.islamicproducts.MainActivity
03-17 16:17:58.382 17920-17920/com.example.islamicproducts D/Surface: Surface::allocateBuffers(this=0xb97678e0)
03-17 16:17:58.464 17920-18015/com.example.islamicproducts I/OpenGLRenderer: Initialized EGL, version 1.4
03-17 16:17:58.465 17920-18015/com.example.islamicproducts D/OpenGLRenderer: Created EGL context (0xb8832830)
03-17 16:17:58.475 17920-18015/com.example.islamicproducts I/OpenGLRenderer: Get enable program binary service property (1)
03-17 16:17:58.475 17920-18015/com.example.islamicproducts I/OpenGLRenderer: Initializing program atlas...
03-17 16:17:58.476 17920-18015/com.example.islamicproducts D/ProgramBinary/Service: BpProgramBinaryService.getFileDescriptor
03-17 16:17:58.518 17920-18015/com.example.islamicproducts D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapLen
03-17 16:17:58.518 17920-18015/com.example.islamicproducts D/ProgramBinary/Service: BpProgramBinaryService.getProgramMapArray
03-17 16:17:58.520 17920-18015/com.example.islamicproducts D/ProgramBinary/Service: BpProgramBinaryService.getProgramBinaryLen
03-17 16:17:58.521 17920-18015/com.example.islamicproducts I/OpenGLRenderer: Program binary detail: Binary length is 111796, program map length is 152.
03-17 16:17:58.521 17920-18015/com.example.islamicproducts I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 35, and path is /dev/ashmem�}Hնh0˵�ۓ�.
03-17 16:17:58.521 17920-18015/com.example.islamicproducts I/OpenGLRenderer: No need to use file discriptor anymore, close fd(35).
03-17 16:17:58.533 17920-18015/com.example.islamicproducts D/OpenGLRenderer: Initializing program cache from 0xb6d83090, size = 9
03-17 16:17:58.548 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000000000000000)
03-17 16:17:58.549 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000000000000001)
03-17 16:17:58.550 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000000000500041)
03-17 16:17:58.550 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000000800000003)
03-17 16:17:58.551 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000001000000000)
03-17 16:17:58.551 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000001000000008)
03-17 16:17:58.552 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000001000500040)
03-17 16:17:58.553 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000001800000000)
03-17 16:17:58.553 17920-18015/com.example.islamicproducts D/OpenGLRenderer: -- init (key = 0x0000003800000000)
03-17 16:17:58.554 17920-18015/com.example.islamicproducts D/Surface: Surface::connect(this=0xb97678e0,api=1)
03-17 16:17:58.555 17920-18015/com.example.islamicproducts W/libEGL: [ANDROID_RECORDABLE] format: 1
03-17 16:17:58.565 17920-18015/com.example.islamicproducts D/Surface: Surface::setBufferCount(this=0xb97678e0,bufferCount=4)
03-17 16:17:58.654 17920-18015/com.example.islamicproducts D/GraphicBuffer: register, handle(0xb9801250) (w:480 h:800 s:480 f:0x1 u:0x000f02)
03-17 16:17:58.862 17920-18015/com.example.islamicproducts D/OpenGLRenderer: CacheTexture 3 upload: x, y, width height = 0, 0, 512, 39
03-17 16:17:58.917 17920-18015/com.example.islamicproducts D/OpenGLRenderer: ProgramCache save to disk, size = 9
03-17 16:17:58.965 17920-17931/com.example.islamicproducts I/art: Background sticky concurrent mark sweep GC freed 3954(351KB) AllocSpace objects, 4(80KB) LOS objects, 9% free, 5MB/6MB, paused 25.002ms total 101.198ms
03-17 16:17:58.974 17920-18015/com.example.islamicproducts D/GraphicBuffer: register, handle(0xb9848ac0) (w:480 h:800 s:480 f:0x1 u:0x000f02)
03-17 16:17:59.285 17920-18018/com.example.islamicproducts D/libc-netbsd: getaddrinfo: islamicproducts-51994.firebaseio.com get result from proxy gai_error = 0
03-17 16:17:59.286 17920-18018/com.example.islamicproducts D/Posix: [Posix_connect Debug]Process com.example.islamicproducts :443
03-17 16:17:59.884 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: hostname=islamicproducts-51994.firebaseio.com; servname=(null); netid=0; mark=0
03-17 16:17:59.884 17920-18018/com.example.islamicproducts D/libc-netbsd: [getaddrinfo]: ai_addrlen=0; ai_canonname=(null); ai_flags=4; ai_family=0
03-17 16:17:59.885 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 NativeCrypto_SSL_do_handshake fd=0xa10c4390 shc=0xa10c4394 timeout_millis=60000 client_mode=1 npn=0x0
03-17 16:17:59.885 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake ++
03-17 16:17:59.885 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 handshake start in CINIT before connect initialization
03-17 16:17:59.885 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 info_callback calling handshakeCompleted
03-17 16:17:59.885 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 info_callback completed
03-17 16:17:59.886 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 SSL_connect:error exit in 3RSH_A SSLv3 read server hello A
03-17 16:17:59.886 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake -- ret=-1
03-17 16:17:59.886 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 NativeCrypto_SSL_do_handshake ret=-1 errno=11 sslError=2 timeout_millis=60000
03-17 16:18:01.987 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake ++
03-17 16:18:01.992 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 SSL_connect:error exit in 3RSC_A SSLv3 read server certificate A
03-17 16:18:01.993 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake -- ret=-1
03-17 16:18:01.993 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 NativeCrypto_SSL_do_handshake ret=-1 errno=11 sslError=2 timeout_millis=60000
03-17 16:18:01.993 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake ++
03-17 16:18:02.001 17920-18018/com.example.islamicproducts E/NativeCrypto: ssl=0xb9835668 cert_verify_callback x509_store_ctx=0xa10c41bc arg=0x0
03-17 16:18:02.001 17920-18018/com.example.islamicproducts E/NativeCrypto: ssl=0xb9835668 cert_verify_callback calling verifyCertificateChain authMethod=ECDHE_RSA
03-17 16:18:02.124 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 cert_verify_callback => 1
03-17 16:18:02.149 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 SSL_connect:error exit in 3RFINA SSLv3 read finished A
03-17 16:18:02.149 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake -- ret=-1
03-17 16:18:02.149 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 NativeCrypto_SSL_do_handshake ret=-1 errno=11 sslError=2 timeout_millis=60000
03-17 16:18:02.463 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake ++
03-17 16:18:02.463 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 handshake done in SSLOK SSL negotiation finished successfully
03-17 16:18:02.463 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 info_callback calling handshakeCompleted
03-17 16:18:02.464 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 info_callback completed
03-17 16:18:02.464 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 SSL_connect:ok exit in SSLOK SSL negotiation finished successfully
03-17 16:18:02.464 17920-18018/com.example.islamicproducts D/NativeCrypto: doing handshake -- ret=1
03-17 16:18:02.464 17920-18018/com.example.islamicproducts D/NativeCrypto: ssl=0xb9835668 NativeCrypto_SSL_get_certificate => NULL
答案 0 :(得分:0)
myAdapter = new MyAdapter(root.getContext(),uploadList);
recyclerView.setLayoutManager(new LinearLayoutManager(root.getContext()));
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Upload upload= uploadlist.get(position);
holder.textView.setText(upload.imageName);
Picasso.with(context).load(upload.uri).placeholder(R.mipmap.ic_launcher).fit().centerCrop().into(holder.imageView);
}