使用firebase的内存不足错误

时间:2015-10-27 12:01:44

标签: android firebase out-of-memory

从android上的firebase数据库读取时出现oom错误。我正在使用片段作为标签,这是其中一个标签的代码..

public class EventsTab extends Fragment {

ListView listView;
ArrayList<String> titles = new ArrayList<String>();
ArrayList<String> descriptions = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();
ArrayList<String> urls = new ArrayList<String>();
int counter = 0;
float x1,x2;
float y1, y2;

static ProgressDialog dialog;
static AnimationDrawable frameAnimation;
static ProgressBar mProgressBar;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    titles = new ArrayList<String>();
    urls = new ArrayList<String>();
    dates = new ArrayList<String>();
    descriptions = new ArrayList<String>();

    View rootView = inflater.inflate(R.layout.activity_events_tab, container, false);
    return rootView;

}

@Override 
public void onActivityCreated(Bundle savedInstanceState) {  
    super.onActivityCreated(savedInstanceState); 

        mProgressBar = (ProgressBar) getActivity().findViewById(R.id.my_progress);
        mProgressBar.setBackgroundResource(R.drawable.ani_icon);

        frameAnimation = (AnimationDrawable) mProgressBar.getBackground();

        frameAnimation.start();


    Firebase ref = new Firebase("https://dazzling-inferno-7627.firebaseio.com/yourLocal/events/");
    ref.addValueEventListener(new ValueEventListener() {

        @Override
          public void onDataChange(DataSnapshot snapshot) {
            System.out.println("There are " + snapshot.getChildrenCount() + " blog posts");


            for (DataSnapshot postSnapshot: snapshot.getChildren()) {
                Event post = postSnapshot.getValue(Event.class);
                titles.add(post.getTitle());
                dates.add(post.getDate());
                descriptions.add(post.getDescription());

              }

                listView = (ListView) getActivity().findViewById(R.id.listView3);

               CustomList adapter = new  CustomList(getActivity(), titles, dates);

                listView.setAdapter(adapter);

                listView.setOnItemClickListener(new OnItemClickListener() {
                    public void onItemClick(AdapterView<?> parent, View v,
                        int position, long id) {

                        Intent intent = new Intent(getActivity(), ShowEvent.class);
                        intent.putExtra("title", titles.get(position));
                        intent.putExtra("date", dates.get(position));
                        intent.putExtra("description", descriptions.get(position));
                        startActivity(intent);  

                    }
                });
                frameAnimation.stop();
                mProgressBar.setVisibility(View.INVISIBLE);
          }
          @Override
          public void onCancelled(FirebaseError firebaseError) {
              System.out.println("The read failed: " + firebaseError.getMessage());
          }
      });



}

}

这是错误......

10-27 11:46:36.444: E/AndroidRuntime(11090): FATAL EXCEPTION: main
10-27 11:46:36.444: E/AndroidRuntime(11090): java.lang.OutOfMemoryError
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:82)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.StringBuilder.<init>(StringBuilder.java:67)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.core.util.TextBuffer.contentsAsString(TextBuffer.java:350)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.core.json.ReaderBasedJsonParser.getValueAsString(ReaderBasedJsonParser.java:223)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:29)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.std.StringDeserializer.deserialize(StringDeserializer.java:11)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.SettableBeanProperty.deserialize(SettableBeanProperty.java:464)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.impl.MethodProperty.deserializeAndSet(MethodProperty.java:98)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserializeFromObject(BeanDeserializer.java:295)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.deser.BeanDeserializer.deserialize(BeanDeserializer.java:121)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.ObjectMapper._readMapAndClose(ObjectMapper.java:2888)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.shaded.fasterxml.jackson.databind.ObjectMapper.readValue(ObjectMapper.java:2034)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.DataSnapshot.getValue(DataSnapshot.java:181)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.brianmccabe.yournews.EventsTab$Test$1.onDataChange(EventsTab.java:107)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.ValueEventRegistration.fireEvent(ValueEventRegistration.java:45)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.view.DataEvent.fire(DataEvent.java:45)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.firebase.client.core.view.EventRaiser$1.run(EventRaiser.java:38)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Handler.handleCallback(Handler.java:615)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.os.Looper.loop(Looper.java:137)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at android.app.ActivityThread.main(ActivityThread.java:4867)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.reflect.Method.invokeNative(Native Method)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at java.lang.reflect.Method.invoke(Method.java:511)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1007)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:774)
10-27 11:46:36.444: E/AndroidRuntime(11090):    at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

您从Firebase下载的数据多于内存中的数据。尝试使用queries拆分数据,并减少读取操作的范围。