耗尽内存android

时间:2014-08-20 15:32:24

标签: java android out-of-memory

当我在真实设备上启动并运行我的应用程序时,它说不幸停止了,并且在logcat中说java.lang.runoutofmemory并且我不知道如何修复它

这是我的主要活动类:

package com.example.hotlinesegypt;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Array;
import java.util.ArrayList;
import mo.hot.hotlinak.R;
import android.R.anim;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient.CustomViewCallback;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;

public class MainActivity extends Activity implements OnItemClickListener,
    TextWatcher {


String[] category_name;
int[] category_id;
Bitmap[] pics;
GridView v1;

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@SuppressLint("NewApi")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    ActionBar bar = getActionBar();
    // for color
    bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#5c7afe")));
    // for image

    setContentView(R.layout.activity_main);

    v1 = (GridView) findViewById(R.id.gridvieww);
    SetDB();
    v1.setOnItemClickListener(this);
}

public void SetDB() {
    SQLiteDatabase DB = openOrCreateDatabase("test", MODE_PRIVATE, null);// to
                                                                            // create
                                                                            // db
                                                                            // folder
    InputStream is = getResources().openRawResource(R.raw.hotline_data);
    try {
        byte[] b = new byte[is.available()];
        is.read(b);
        FileOutputStream fos = new FileOutputStream("/data/data/"
                + getPackageName() + "/databases/Hotline.sqlite");
        fos.write(b);
        fos.close();

        SQLiteDatabase hotline_db = openOrCreateDatabase("Hotline.sqlite",
                MODE_PRIVATE, null);
        Cursor c = hotline_db.rawQuery("select * from category", null);
        if (c.moveToFirst()) {
            category_name = new String[c.getCount()];
            category_id = new int[c.getCount()];
            pics = new Bitmap[c.getCount()];
            int i = 0;
            do {
                int id = c.getInt(c.getColumnIndex("id"));
                String name = c.getString(c.getColumnIndex("name"));
                byte[] b1 = c.getBlob(c.getColumnIndex("pic"));
                Bitmap bm = BitmapFactory.decodeByteArray(b1, 0, b1.length);
                category_name[i] = name;
                category_id[i] = id;
                pics[i] = bm;
                i++;
            } while (c.moveToNext());
            CustomGridAdapter cga = new CustomGridAdapter(this,
                    category_name, pics);
            v1.setAdapter(cga);
        }

    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    // TODO Auto-generated method stub
    Intent i = new Intent(this, Company.class);
    i.putExtra("cat_id", category_id[arg2]);
    startActivity(i);
}

@Override
public void afterTextChanged(Editable s) {
    // TODO Auto-generated method stub

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
        int after) {
    // TODO Auto-generated method stub

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
    // TODO Auto-generated method stub

}

}

这是我的网格视图

    package com.example.hotlinesegypt;
    import mo.hot.hotlinak.R;
    import android.content.Context;
    import android.graphics.Bitmap;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.ImageView;
    import android.widget.TextView;


    public class CustomGridAdapter extends BaseAdapter {
    private Context context;
    private final String [] gridValues;
    private Bitmap[] pic;
    public CustomGridAdapter (Context context,String[] gridValues,Bitmap[] pic){
    this.context= context;
    this.gridValues=gridValues;
    this.pic=pic;


    }
    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return gridValues.length;
    }

    @Override
    public Object getItem(int arg0) {
        // TODO Auto-generated method stub
        return null;
    }

    @Override
    public long getItemId(int arg0) {
        // TODO Auto-generated method stub
        return 0;
    }

    @Override
    public View getView(int arg0, View arg1, ViewGroup arg2) {
        // TODO Auto-generated method stub

        LayoutInflater li=(LayoutInflater)        
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View v=li.inflate(R.layout.grid, arg2, false);
        ImageView iv=(ImageView) v.findViewById(R.id.gridimage);

        iv.setImageBitmap(pic[arg0]);

        return v;
    }

    }

这是log cat

08-20 18:09:41.738: E/AndroidRuntime(11021):    at      android.view.Choreographer.doFrame(Choreographer.java:573)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789) 08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Handler.handleCallback(Handler.java:733)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Handler.dispatchMessage(Handler.java:95)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.os.Looper.loop(Looper.java:157)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.app.ActivityThread.main(ActivityThread.java:5356)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Method.invokeNative(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Method.invoke(Method.java:515)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at dalvik.system.NativeStart.main(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021): Caused by: java.lang.reflect.InvocationTargetException
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Constructor.constructNative(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.view.LayoutInflater.createView(LayoutInflater.java:600)
08-20 18:09:41.738: E/AndroidRuntime(11021):    ... 41 more
08-20 18:09:41.738: E/AndroidRuntime(11021): Caused by: java.lang.OutOfMemoryError
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:677)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:507)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:872)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.content.res.Resources.loadDrawable(Resources.java:3056)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.widget.ImageView.<init>(ImageView.java:133)
08-20 18:09:41.738: E/AndroidRuntime(11021):    at android.widget.ImageView.<init>(ImageView.java:123)
08-20 18:09:41.738: E/AndroidRuntime(11021):    ... 44 more

0 个答案:

没有答案