我必须使用android studio将上传到Firebase存储的图像检索到应用中以制作该应用。这基本上就像是应用程序的实时公告板,上传到Firebase存储中的图像应自动刷新到应用程序中。我是Firebase的新手,这已分配给我。谁能指导我?
我已经尝试创建Model类和ViewHolder类,并尝试了回收站视图,但是它没有响应。该应用程序始终会强制关闭。列出了存储图像的格式。
编辑(使用Recycler View的代码) activity_notices.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=".Notices"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/recyclerview">
</androidx.recyclerview.widget.RecyclerView>
</LinearLayout>
Model.java
package com.example.finalapkx;
public class Model {
String title,image,description;
public Model() {}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
}
row.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="3dp"
app:cardElevation="3dp"
app:contentPadding="5dp"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/rTitleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<ImageView
android:id="@+id/rImageView"
android:layout_width="match_parent"
android:layout_height="200dp"
android:adjustViewBounds="true"
android:background="@drawable/loading"
android:scaleType="centerCrop" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/rDescriptionTv"
android:text="This will be description of the Notice"
android:textSize="20sp"
/>
</LinearLayout>
</androidx.cardview.widget.CardView>
ViewHolder.java
package com.example.finalapkx;
import android.content.Context;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.recyclerview.widget.RecyclerView;
import com.squareup.picasso.Picasso;
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDetails(Context ctx,String title,String description, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDetailTv = mView.findViewById(R.id.rDescriptionTv);
ImageView mImageIv = mView.findViewById(R.id.rImageView);
mTitleTv.setText(title);
mDetailTv.setText(description);
Picasso.get().load(image).into(mImageIv);
}
}
Notices.java
package com.example.finalapkx;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
import com.firebase.client.DataSnapshot;
import com.firebase.client.FirebaseError;
import com.firebase.client.ValueEventListener;
import com.firebase.client.core.Context;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class Notices extends AppCompatActivity {
RecyclerView mrecyclerView;
FirebaseDatabase mfirebaseDatabase;
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notices);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle("Notices List");
mrecyclerView = findViewById(R.id.recyclerview);
mrecyclerView.setHasFixedSize(true);
mrecyclerView.setLayoutManager(new LinearLayoutManager(this));
mfirebaseDatabase = FirebaseDatabase.getInstance();
mRef = mfirebaseDatabase.getReference("images");
}
@Override
protected void onStart() {
super.onStart();
FirebaseRecyclerAdapter<Model, ViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<Model, ViewHolder>(Model.class,R.layout.row,ViewHolder.class,mRef) {
@Override
protected void populateViewHolder(ViewHolder viewHolder, Model model, int i) {
viewHolder.setDetails(getApplicationContext(),model.getTitle(),model.getDescription(),model.getImage());
}
};
mrecyclerView.setAdapter(firebaseRecyclerAdapter);
}
}
Logcat错误
2019-08-02 10:56:21.013 7174-7174/com.example.finalapkx E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.finalapkx, PID: 7174
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finalapkx/com.example.finalapkx.Notices}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.appcompat.app.ActionBar.setTitle(java.lang.CharSequence)' on a null object reference
at com.example.finalapkx.Notices.onCreate(Notices.java:32)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
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)
2019-08-02 10:56:21.030 7174-7174/com.example.finalapkx I/Process: Sending signal. PID: 7174 SIG: 9
Androidmanifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.finalapkx">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:usesCleartextTraffic="true">
<activity android:name=".AC4"></activity>
<activity android:name=".AC3" />
<activity android:name=".AC2" />
<activity android:name=".AC1" />
<activity android:name=".pdfyearone" />
<activity android:name=".Visit" />
<activity android:name=".Administration" />
<activity android:name=".Leadership" />
<activity android:name=".VisMis" />
<activity android:name=".ACCA" />
<activity android:name=".Genesis" />
<activity android:name=".Results" />
<activity android:name=".PHARMA3" />
<activity android:name=".MECH3" />
<activity android:name=".IT3" />
<activity android:name=".EEE3" />
<activity android:name=".ECE3" />
<activity android:name=".CSE3" />
<activity android:name=".CIVIL3" />
<activity android:name=".CHEMICAL3" />
<activity android:name=".Year3" />
<activity android:name=".CHEMICAL2" />
<activity android:name=".CIVIL2" />
<activity android:name=".MECH2" />
<activity android:name=".PHARMA2" />
<activity android:name=".IT2" />
<activity android:name=".EEE2" />
<activity android:name=".ECE2" />
<activity android:name=".CSE2" />
<activity android:name=".CHEMICAL" />
<activity android:name=".CIVIL" />
<activity android:name=".MECH" />
<activity android:name=".PHARMA" />
<activity android:name=".IT" />
<activity android:name=".EEE" />
<activity android:name=".ECE" />
<activity android:name=".CSE" />
<activity android:name=".Year4" />
<activity android:name=".Year2" />
<activity android:name=".Year1" />
<activity android:name=".ExamPrep" />
<activity android:name=".About" />
<activity android:name=".Notices" />
<activity android:name=".Events" />
<activity android:name=".Calendar" />
<activity android:name=".Attendance" />
<activity android:name=".Launch" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>