加载图像时出现java.lang.OutOfMemoryError

时间:2016-07-27 06:46:06

标签: android out-of-memory runtime-error

我使用Genymotion和oracle VirtualBox在虚拟设备中运行一个简单的词典应用程序。

我有4GB ram,我与虚拟设备共享了2 Gb。我的问题是当我使用1920x1080分辨率的jpg图像,其大小为231 kb时,我会在我的LogCat中得到关注。

有时即使我没有使用图像,我也会遇到同样的错误。会有什么解决方案?

/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.database.CursorWindow.nativeGetString(Native Method)
at android.database.CursorWindow.getString(CursorWindow.java:434)
at android.database.AbstractWindowedCursor.getString(AbstractWindowedCursor.java:51)
at com.example.sharecorn.wordydictionary.DictionaryDatabaseHelper.getAllWords(DictionaryDatabaseHelper.java:90)
at com.example.sharecorn.wordydictionary.Dictionary_list.onCreate(Dictionary_list.java:64)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

//here is my dictionary_list's oncreate() function         

     protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_dictionary_list);

    Log.d("DICTIONARY","SECOND ACTIVITY STARTED");

    userTextView = (TextView) findViewById(R.id.personTextView);
    userTextView.setText(getIntent().getStringExtra(MainActivity.USER_NAME_STRING)+"'s Dictionary");

    searchEditText =(EditText)findViewById(R.id.searchEditText);
    searchButton = (Button) findViewById(R.id.searchButton);
    dictionaryListView = (ListView) findViewById(R.id.dictionaryListView);

    myDictionaryDatabaseHelper = new DictionaryDatabaseHelper(this,"Dictionary",null,1);

    sharedPreferences = getSharedPreferences(MainActivity.SHARED_NAME_STRING,MODE_PRIVATE);
    //save if DB is initialized before in sharedPreferences

    boolean initialized = sharedPreferences.getBoolean("initialized",false);

    if(initialized == false){
        Log.d(logTagString,"Initialized for the first time");
        initializedDatabase();
    }else {
        Log.d(logTagString,"DB already intialized");

    }

    allWordDefination = myDictionaryDatabaseHelper.getAllWords();
    dictionaryListView.setAdapter(new BaseAdapter() {
        @Override
        public int getCount() {
            return 0;
        }

        @Override
        public Object getItem(int position) {
            return null;
        }

        @Override
        public long getItemId(int position) {
            return 0;
        }

        @Override
        public View getView(int position, View view, ViewGroup parent) {
            if(view == null){
                view=getLayoutInflater().inflate(R.layout.activity_list_item,null);
            }
            TextView textView = (TextView)view.findViewById(R.id.listItemTextView);
            textView.setText(allWordDefination.get(position).word);
            return view;
        }
    });

    dictionaryListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            Intent intent = new Intent(Dictionary_list.this,WordDefinationDetail.class);
            intent.putExtra("word",allWordDefination.get(position).word);
            intent.putExtra("defination",allWordDefination.get(position).defination);
        }

    });

    searchButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
           String string = searchEditText.getText().toString();
            WordDefination wordDefination = myDictionaryDatabaseHelper.getWordDefination(string);

            if(wordDefination==null){
                Toast.makeText(Dictionary_list.this,"Word not found",Toast.LENGTH_LONG).show();
            }else {
                Intent intent = new Intent(Dictionary_list.this,WordDefinationDetail.class);
                intent.putExtra("word",wordDefination.word);
                intent.putExtra("defination",wordDefination.defination);
                startActivity(intent);
            }
        }
    });
}

//Here is the DictionaryDatabaseHelper's getAllWords()

        public ArrayList<WordDefination> getAllWords(){
    ArrayList<WordDefination> arrayList = new ArrayList<WordDefination>();
    SQLiteDatabase database = this.getReadableDatabase();
    String selectAllQueryString = "SELECT * FROM "+DICTIONARY_DATABASE;//SELECT EVERYTHING FROM DICTIONARY DATABASE
    Cursor cursor = database.rawQuery(selectAllQueryString,null);
    if(cursor.moveToFirst()){//moving the cursor to first
        do{
            WordDefination wordDefination = new WordDefination(cursor.getString(cursor.getColumnIndex(WORD_COLUMN)),cursor.getString(cursor.getColumnIndex(DEFINATION_COLUMN)));//object of arraylist with word and defination
            arrayList.add(wordDefination);//adding words and defination in arraylist

        }while (cursor.moveToNext());//loop untill next element is present
    }

    return  arrayList;
}

0 个答案:

没有答案