2017-06-13 57 views
0

我是這個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> 

請幫助我!

+0

的[可能的複製內存不足錯誤而加載位圖](https://stackoverflow.com/questions/14359024/out-of-memory-error - 同時加載位圖) – F43nd1r

+0

沒有得到你請你詳細說明...與解決方案 – hatim

+0

你可以發佈堆棧跟蹤異常從Android顯示器 –

回答

0

嘗試使用RecyclerViewViewHolder方法,你也可以使用PicassoGlide庫,以平滑圖像加載到ImageView。 您正在綁定以網格方式顯示圖像,所以我認爲您可以使用RecyclerView並使RecyclerViewLayoutManager爲堆棧格式。

compile 'com.github.bumptech.glide:glide:3.7.0' 
compile 'com.squareup.picasso:picasso:2.5.2' 
compile 'com.android.support:recyclerview-v7:25.3.1' 

看一看這個 - https://guides.codepath.com/android/using-the-recyclerview 讓我知道,如果有任何問題發生!

your_layout.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    tools:context="and.com.bakingtime.activity.MainActivity"> 

    <android.support.v7.widget.RecyclerView 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:id="@+id/recycler_view" /> 
</LinearLayout> 

list_item.xml

<android.support.v7.widget.CardView 
    xmlns:card_view="http://schemas.android.com/apk/res-auto" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/card_view" 
    android:layout_width="80dp" 
    android:layout_height="wrap_content" 
    card_view:cardUseCompatPadding="true" 
    card_view:cardCornerRadius="8dp" 
    android:layout_marginBottom="16dp"> 
     <ImageView 
      android:id="@+id/photo" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:scaleType="centerCrop" /> 
</android.support.v7.widget.CardView> 

your_adapter_class.java

public class your_adapter_class extends RecyclerView.Adapter<RecyclerViewHolders> { 

    private List<ItemObject> itemList; 
    private Context context; 

    public RecyclerViewAdapter(Context context, List<ItemObject> itemList) { 
     this.itemList = itemList; 
     this.context = context; 
    } 

    @Override 
    public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) { 

     View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_item, null); 
     RecyclerViewHolders rcv = new RecyclerViewHolders(layoutView); 
     return rcv; 
    } 

    @Override 
    public void onBindViewHolder(RecyclerViewHolders holder, int position) { 
     Picasso.with(context).load((itemList.get(position).getPhoto()).into(holder.thumbnail); 
    } 

    @Override 
    public int getItemCount() { 
     return this.itemList.size(); 
    } 
} 

your_ViewHolder.java

public class your_ViewHolder extends RecyclerView.ViewHolder{ 

    public ImageView thumbnail; 

    public RecyclerViewHolders(View itemView) { 
     super(itemView); 
     thumbnail = (ImageView)itemView.findViewById(R.id.photo); 
    } 
} 

MainActivity.java
在你MainActivity onCreate作出GridLayoutManager並將其連接到您的recyclerView

 private GridLayoutManager lLayout; 

     // inside onCreate 
     List<ItemObject> rowListItem = getAllItemList(); 
     lLayout = new GridLayoutManager(MainActivity.this, 2); 

     RecyclerView rView = (RecyclerView)findViewById(R.id.recycler_view); 
     rView.setLayoutManager(lLayout); 

     RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(MainActivity.this, rowListItem); 
     rView.setAdapter(rcAdapter); 

    //getAllItemList method 
    private List<ItemObject> getAllItemList(){ 

    List<ItemObject> itemObject = new ArrayList<ItemObject>(); 

    itemObject.add(new ItemObject("",R.drawable.a1)); 
    itemObject.add(new ItemObject("",R.drawable.a2)); 
    itemObject.add(new ItemObject("",R.drawable.a3)); 
    itemObject.add(new ItemObject("",R.drawable.a4)); 
    itemObject.add(new ItemObject("",R.drawable.a5)); 

    itemObject.add(new ItemObject("",R.drawable.a6)); 
    itemObject.add(new ItemObject("",R.drawable.a7)); 
    itemObject.add(new ItemObject("",R.drawable.a8)); 
    itemObject.add(new ItemObject("",R.drawable.a9)); 
    itemObject.add(new ItemObject("",R.drawable.a10)); 
    itemObject.add(new ItemObject("",R.drawable.a11)); 
    itemObject.add(new ItemObject("",R.drawable.a12)); 
    itemObject.add(new ItemObject("",R.drawable.a13)); 
    itemObject.add(new ItemObject("",R.drawable.a14)); 
    itemObject.add(new ItemObject("",R.drawable.a15)); 

    itemObject.add(new ItemObject("",R.drawable.a16)); 
    itemObject.add(new ItemObject("",R.drawable.a17)); 
    itemObject.add(new ItemObject("",R.drawable.a18)); 
    itemObject.add(new ItemObject("",R.drawable.a19)); 

    return ItemObject; 
} 
相關問題