我是這個Android世界的新手..所以請幫助... 我正在寫我自己的自定義適配器使用GridView每一個上述錯誤崩潰我app..enclosing我的MainActivity文件時,COdeAdapterFilejava.lang.outofmemoryerror:未能分配15451212字節分配11809808自由字節和11mb,直到oom
填充images..but這是MainActivityCode com.example.android.internship;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import java.util.ArrayList;
public class Handicrafts extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
//THIS IS ARTIFACTS FILE
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
NavigationView nv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_handicrafts);
nv=(NavigationView)findViewById(R.id.mynavigation);
nv.setNavigationItemSelectedListener(this);
mToolbar=(Toolbar)findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout=(DrawerLayout) findViewById(R.id.drawerLayout);
mToggle=new ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDefaultDisplayHomeAsUpEnabled(true);
final ArrayList<Items> itemObject = new ArrayList<Items>();
itemObject.add(new Items("",R.drawable.a1));
itemObject.add(new Items("",R.drawable.a2));
itemObject.add(new Items("",R.drawable.a3));
itemObject.add(new Items("",R.drawable.a4));
itemObject.add(new Items("",R.drawable.a5));
itemObject.add(new Items("",R.drawable.a6));
itemObject.add(new Items("",R.drawable.a7));
itemObject.add(new Items("",R.drawable.a8));
itemObject.add(new Items("",R.drawable.a9));
itemObject.add(new Items("",R.drawable.a10));
itemObject.add(new Items("",R.drawable.a11));
itemObject.add(new Items("",R.drawable.a12));
itemObject.add(new Items("",R.drawable.a13));
itemObject.add(new Items("",R.drawable.a14));
itemObject.add(new Items("",R.drawable.a15));
itemObject.add(new Items("",R.drawable.a16));
itemObject.add(new Items("",R.drawable.a17));
itemObject.add(new Items("",R.drawable.a18));
itemObject.add(new Items("",R.drawable.a19));
ItemAdapter adapter = new ItemAdapter(this, itemObject);
// Get a reference to the ListView, and attach the adapter to the listView.
GridView newGrdidView = (GridView) findViewById(R.id.gridview);
newGrdidView.setAdapter(adapter);
final int []images={R.drawable.a1,R.drawable.a2,R.drawable.a3,R.drawable.a4,R.drawable.a5,R.drawable.a6,R.drawable.a7,R.drawable.a8,
R.drawable.a9,R.drawable.a10,R.drawable.a11,R.drawable.a12,R.drawable.a13,R.drawable.a14,R.drawable.a15,R.drawable.a16,R.drawable.a17,
R.drawable.a18,R.drawable.a19,};
newGrdidView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(Handicrafts.this,
Main3Activity.class);
int mImageId =0;
intent.putExtra("img",images[position]);
startActivity(intent);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mToggle.onOptionsItemSelected(item))
{return true;}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch(item.getItemId())
{
case R.id.paint:
Intent paintIntent=new Intent(Handicrafts.this,MainActivity.class);
startActivity(paintIntent);
break;
case R.id.covers:
Intent collageIntent=new Intent(Handicrafts.this,collages.class);
startActivity(collageIntent);
break;
case R.id.artifacts:
Intent craftsIntent=new Intent(Handicrafts.this,Handicrafts.class);
startActivity(craftsIntent);
break;
}
return true;
}
}
這是customAdapter代碼
package com.example.android.internship;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import static android.R.attr.id;
import static android.R.attr.resource;
/**
* Created by hatim on 6/6/2017.
*/
public class ItemAdapter extends ArrayAdapter<Items> {
public ItemAdapter(Activity context,ArrayList<Items> itemObject) {
super(context,0,itemObject);
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
View gridview=convertView;
if(gridview==null)
{
gridview = LayoutInflater.from(getContext()).inflate(
R.layout.item_layout, parent, false);
}
Items currentItems = getItem(position);
TextView nameTextView = null;
if (gridview != null) {
nameTextView = (TextView) gridview.findViewById(R.id.product_name);
}
// Get the version name from the current AndroidFlavor object and
// set this text on the name TextView
nameTextView.setText(currentItems.getItemName());
ImageView iconView = null;
if (gridview != null) {
iconView = (ImageView) gridview.findViewById(R.id.product_images);
}
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentItems.getImageId());
return gridview;
}
}
這是我的Android清單文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.internship">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:largeHeap="true"
android:hardwareAccelerated="false"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity" />
<activity android:name=".Main2Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Main3Activity" />
<activity
android:name=".collages"
android:label="Cushion covers" />
<activity
android:name=".Handicrafts"
android:label="Artefacts" />
</application>
</manifest>
請幫助我!
的[可能的複製內存不足錯誤而加載位圖](https://stackoverflow.com/questions/14359024/out-of-memory-error - 同時加載位圖) – F43nd1r
沒有得到你請你詳細說明...與解決方案 – hatim
你可以發佈堆棧跟蹤異常從Android顯示器 –