网格视图和适配器在android中不起作用

时间:2019-02-04 13:40:33

标签: android gridview

我正在创建一个简单的应用程序,其中包含适配器中的图像 它可以在模拟器中正常运行,但不能在手机上运行

网格和适配器,显示一个错误 我花了3天的时间在Google中找到错误

gridAdapter

public class Grid_Adapter extends BaseAdapter {
    Integer[] image_grid;
    String[] info;
    private String[] disc;
    private Context c;
    private LayoutInflater inflater;


    Grid_Adapter(Application applicationContext, Integer[] image_grid, String[] info, String[] disc) {
        this.c = applicationContext;
        this.image_grid = image_grid;
        this.info = info;
        this.disc = disc;

        inflater = (LayoutInflater.from(applicationContext));

    }


    @Override
    public int getCount() {
        return image_grid.length;
    }

    @Override
    public Object getItem(int position) {
        return image_grid[position];
    }

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

    @SuppressLint({"ViewHolder", "InflateParams"})
    @Override
    public View getView(int position, View view, ViewGroup parent) {
        if (inflater == null)
            inflater = LayoutInflater.from(this.c);

        if (view == null)

            view = inflater.inflate(R.layout.grid_item, null);
        ImageView image_g = view.findViewById(R.id.image_grid);
        image_g.setImageResource(image_grid[position]);
        TextView tv = view.findViewById(R.id.txthint);
        tv.setText(info[position]);
        TextView tv2 = view.findViewById(R.id.txthint2);
        tv2.setText(disc[position]);

        return view;
    }
}

gridActivity

protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_grid_activities1);
        gridView=findViewById(R.id.gridview);

        Grid_Adapter gridAdapter=new Grid_Adapter(getApplication(),image_grid,info,disc);
        gridView.setAdapter(gridAdapter);
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent,View view,int position,long id){
        Intent intent=new Intent(Grid_activities1.this,Image_show.class);
        intent.putExtra("images",image_grid[position]);
        intent.putExtra("text",info[position]);
        intent.putExtra("text2",disc[position]);

        startActivity(intent);
        }
        });


        }

错误

  

E / AndroidRuntime:致命异常:main>进程:com.activities.children,PID:1040> java.lang.OutOfMemoryError:无法分配dalvik.system上的44800012字节分配与12576840可用字节和11MB,直到OOM> .VMRuntime.newNonMovableArray(本机方法)> android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)> android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:856)> android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java :675)   在android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:2228)   在android.content.res.Resources.loadDrawableForCookie(Resources.java:4215)   在android.content.res.Resources.loadDrawable(Resources.java:4089)>在android.content.res.Resources.getDrawable(Resources.java:2005)>在android.content.res.Resources.getDrawable(Resources.java :1987)> android.content.Context.getDrawable(Context.java:464)> android.widget.ImageView.resolveUri(ImageView.java:827)> android.widget.ImageView.setImageResource(ImageView.java:434 )> com.activities.children.Grid_Adapter.getView(Grid_Adapter.java:61)> android.widget.AbsListView.obtainView(AbsListView.java:2929)> android.widget.GridView.makeAndAddView(GridView.java:1463) )> android.widget.GridView.makeRow(GridView.java:366)> android.widget.GridView.fillDown(GridView.java:307)> android.widget.GridView.fillFromTop(GridView.java:442)>在android.widget.GridView.layoutChildren(GridView.java:1291)>在android.widget.AbsListView.onLayout(AbsListView.java:2723)>在android.view.View.layout(View.java:17938)>在android查看.Vie wGroup.layout(ViewGroup.java:5812)> android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)> android.widget.LinearLayout.layoutHorizo​​ntal(LinearLayout.java:1731)   在android.widget.LinearLayout.onLayout(LinearLayout.java:1496)>在android.view.View.layout(View.java:17938)>在android.view.ViewGroup.layout(ViewGroup.java:5812)>在android .widget.FrameLayout.layoutChildren(FrameLayout.java:344)>在android.widget.FrameLayout.onLayout(FrameLayout.java:281)>在android.view.View.layout(View.java:17938)>在android.view .ViewGroup.layout(ViewGroup.java:5812)> android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)> android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)   在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)>在android.view.View.layout(View.java:17938)>在android.view.ViewGroup.layout(ViewGroup.java:5812)>在android .widget.FrameLayout.layoutChildren(FrameLayout.java:344)>在android.widget.FrameLayout.onLayout(FrameLayout.java:281)>在android.view.View.layout(View.java:17938)>在android.view .ViewGroup.layout(ViewGroup.java:5812)> android.widget.LinearLayout.setChildFrame(LinearLayout.java:1742)> android.widget.LinearLayout.layoutVertical(LinearLayout.java:1585)   在android.widget.LinearLayout.onLayout(LinearLayout.java:1494)>在android.view.View.layout(View.java:17938)>在android.view.ViewGroup.layout(ViewGroup.java:5812)>在android .widget.FrameLayout.layoutChildren(FrameLayout.java:344)>在android.widget.FrameLayout.onLayout(FrameLayout.java:281)>在com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:3193 )   在android.view.View.layout(View.java:17938)>在android.view.ViewGroup.layout(ViewGroup.java:5812)>在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2666)>在android .view.ViewRootImpl.performTraversals(ViewRootImpl.java:2367)> android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1437)> android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:7397)   在android.view.Choreographer $ CallbackRecord.run(Choreographer.java:920)   在android.view.Choreographer.doCallbacks(Choreographer.java:695)>在android.view.Choreographer.doFrame(Choreographer.java:631)>在android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:906)   在android.os.Handler.handleCallback(Handler.java:739)>在android.os.Handler.dispatchMessage(Handler.java:95)>在android.os.Looper.loop(Looper.java:158)>在android .app.Acti

0 个答案:

没有答案