为什么我的GridView不起作用?

时间:2016-04-14 04:22:53

标签: java android xml gridview

我试图练习在Android Studio中使用GridView,我不知道为什么我会一直得到这个例外。

这是我主要课程的代码。

public class MainActivity extends AppCompatActivity {
private GridView turtle;
private List<Map<String,Object>> datalist;
private int[] icon = {R.drawable.address_book,R.drawable.calendar,R.drawable.camera,R.drawable.clock,R.drawable.games_control,R.drawable.messenger,R.drawable.ringtone,R.drawable.settings,R.drawable.speech_balloon,R.drawable.weather,R.drawable.world,R.drawable.youtube};
private String  [] iconName = {"Contackt","Calendar","Camera","Clock","game","messager","ring","setting","voice","weather","browser","video"};
private SimpleAdapter adapter;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    turtle = (GridView) findViewById(R.id.turtle);
    datalist = new ArrayList<Map<String,Object>>();
    adapter = new SimpleAdapter(this,getdata(),R.layout.item,new String[]{"pic","txt"}, new int[]{R.id.pic,R.id.text});
    turtle.setAdapter(adapter);
}
private  List<Map<String,Object>> getdata(){
    for(int i = 0; i<icon.length;i++) {
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("pic", icon[i]);
        map.put("text",iconName[i]);
        datalist.add(map);


    }


    return datalist;
}
}

以下是我的主要XML文件的代码

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.jinyu.gridleview.MainActivity">

<GridView
    android:numColumns="3"
    android:verticalSpacing="10dp"
    android:horizontalSpacing="10dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/turtle"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>

以下是我的项目布局的代码:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
>
<ImageView
    android:src="@drawable/ic_launcher"
    android:id="@+id/pic"
    android:layout_width="60dp"
    android:layout_height="60dp" />
<TextView
    android:layout_marginTop="5dp"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#ffffff"
    android:id="@+id/text"
    android:text="text"
    android:textSize="12dp"/>


</LinearLayout>

我的日志:

  

04-13 21:14:21.940 2558-2558 / com.example.jinyu.gridleview   E / AndroidRuntime:致命异常:主要                                                                               过程:com.example.jinyu.gridleview,PID:2558                                                                               java.lang.OutOfMemoryError:无法分配9437196字节   分配4194304空闲字节和8MB直到OOM                                                                                   at dalvik.system.VMRuntime.newNonMovableArray(Native Method)                                                                                   在android.graphics.BitmapFactory.nativeDecodeAsset(本机方法)                                                                                   在android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)                                                                                   在   android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)                                                                                   在   android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080)                                                                                   在   android.content.res.Resources.loadDrawableForCookie(Resources.java:2635)                                                                                   在android.content.res.Resources.loadDrawable(Resources.java:2540)                                                                                   在android.content.res.Resources.getDrawable(Resources.java:806)                                                                                   在android.content.Context.getDrawable(Context.java:458)                                                                                   在android.widget.ImageView.resolveUri(ImageView.java:811)                                                                                   在android.widget.ImageView.setImageResource(ImageView.java:418)                                                                                   在android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:276)                                                                                   在android.widget.SimpleAdapter.bindView(SimpleAdapter.java:226)                                                                                   在   android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:133)                                                                                   在android.widget.SimpleAdapter.getView(SimpleAdapter.java:121)                                                                                   在android.widget.AbsListView.obtainView(AbsListView.java:2346)                                                                                   在android.widget.GridView.makeAndAddView(GridView.java:1439)                                                                                   在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:1304)                                                                                   在android.widget.AbsListView.onLayout(AbsListView.java:2148)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在   android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)                                                                                   在android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)                                                                                   在android.widget.LinearLayout.onLayout(LinearLayout.java:1495)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)                                                                                   在android.widget.FrameLayout.onLayout(FrameLayout.java:273)                                                                                   在   com.android.internal.policy.PhoneWindow $ DecorView.onLayout(PhoneWindow.java:2678)                                                                                   在android.view.View.layout(View.java:16630)                                                                                   在android.view.ViewGroup.layout(ViewGroup.java:5437)                                                                                   在android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)                                                                                   在android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)                                                                                   在android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)                                                                                   在   android.view.ViewRootImpl $ TraversalRunnable.run(ViewRootImpl.java:6013)                                                                                   在   android.view.Choreographer $ CallbackRecord.run(Choreographer.java:858)                                                                                   在android.view.Choreographer.doCallbacks(Choreographer.java:670)                                                                                   在android.view.Choreographer.doFrame(Choreographer.java:606)                                                                                   在   android.view.Choreographer $ FrameDisplayEventReceiver.run(Choreographer.java:844)                                                                                   在android.os.Handler.handleCallback(Handler.java:739)                                                                                   在android.os.Handler.dispatchMessage(Handler.java:95)                                                                                   在android.os.Looper.loop(Looper.java:148)                                                                                   在android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                   at java.lang.reflect.Method.invoke(Native Method)                                                                                     在c

2 个答案:

答案 0 :(得分:0)

您无法动态增加堆大小,但可以使用。

请求使用更多
android:largeHeap="true"
中的

https://stackoverflow.com/a/25719828/4853552

答案 1 :(得分:0)

谢谢大家,我通过在手机上运行此应用来解决问题。