public class Gallery extends AppCompatActivity {
static ArrayList<String> images;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gallery);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Gallery");
toolbar.setTitleTextColor(ContextCompat.getColor(Gallery.this, R.color.white));
images = new ArrayList<>();
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onBackPressed();
}
});
final GridView gridview = (GridView) findViewById(R.id.gridview);
final ProgressDialog progressDialog = new ProgressDialog(Gallery.this);
progressDialog.setMessage("Loading data...");
progressDialog.show();
ConnectivityManager manager = (ConnectivityManager) getApplicationContext().getSystemService(CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo();
final boolean isconnected = info != null && info.isConnectedOrConnecting();
Firebase.setAndroidContext(Gallery.this);
if (Firebase.getDefaultConfig().isPersistenceEnabled() == false) //check if persistence is enabled or disbled
Firebase.getDefaultConfig().setPersistenceEnabled(true);
String URL = "someurl";
Firebase firebase = new Firebase(URL);
final ImageAdapter adapter = new ImageAdapter(Gallery.this, images);
final Firebase imagefolder = firebase.child("imagesfolder");
imagefolder.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
images.add(snapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(FirebaseError firebaseError) {
}
});
} }
这是来自Android设备监视器的崩溃报告
java.lang.RuntimeException:出了点问题,请报告给 support@firebase.com 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 在android.app.ActivityThread.access $ 800(ActivityThread.java:135) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1196) 在android.os.Handler.dispatchMessage(Handler.java:102) 在android.os.Looper.loop(Looper.java:136) 在android.app.ActivityThread.main(ActivityThread.java:5017) at java.lang.reflect.Method.invokeNative(Method.java) 在java.lang.reflect.Method.invoke(Method.java:515) 在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 在dalvik.system.NativeStart.main(NativeStart.java)
答案 0 :(得分:0)
使用DatabaseReference类而不是使用Firebase
解决了错误private DatabaseReference mDatabase;
FirebaseDatabase database = FirebaseDatabase.getInstance();
mDatabase=database.getReference().child("imagesfolder");
final ImageAdapter adapter = new ImageAdapter(Gallery.this, images);
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
images.add(snapshot.getValue(String.class));
adapter.notifyDataSetChanged();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});