我試圖在Android Studio中練習使用GridView,我不知道爲什麼我不斷收到這個異常。爲什麼我的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: FATAL EXCEPTION: main Process: com.example.jinyu.gridleview, PID: 2558 java.lang.OutOfMemoryError: Failed to allocate a 9437196 byte allocation with 4194304 free bytes and 8MB until OOM at dalvik.system.VMRuntime.newNonMovableArray(Native Method) at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method) at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609) at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444) at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1080) at android.content.res.Resources.loadDrawableForCookie(Resources.java:2635) at android.content.res.Resources.loadDrawable(Resources.java:2540) at android.content.res.Resources.getDrawable(Resources.java:806) at android.content.Context.getDrawable(Context.java:458) at android.widget.ImageView.resolveUri(ImageView.java:811) at android.widget.ImageView.setImageResource(ImageView.java:418) at android.widget.SimpleAdapter.setViewImage(SimpleAdapter.java:276) at android.widget.SimpleAdapter.bindView(SimpleAdapter.java:226) at android.widget.SimpleAdapter.createViewFromResource(SimpleAdapter.java:133) at android.widget.SimpleAdapter.getView(SimpleAdapter.java:121) at android.widget.AbsListView.obtainView(AbsListView.java:2346) at android.widget.GridView.makeAndAddView(GridView.java:1439) at android.widget.GridView.makeRow(GridView.java:366) at android.widget.GridView.fillDown(GridView.java:307) at android.widget.GridView.fillFromTop(GridView.java:442) at android.widget.GridView.layoutChildren(GridView.java:1304) at android.widget.AbsListView.onLayout(AbsListView.java:2148) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743) at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586) at android.widget.LinearLayout.onLayout(LinearLayout.java:1495) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336) at android.widget.FrameLayout.onLayout(FrameLayout.java:273) at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678) at android.view.View.layout(View.java:16630) at android.view.ViewGroup.layout(ViewGroup.java:5437) at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171) at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931) at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) at android.view.Choreographer.doCallbacks(Choreographer.java:670) at android.view.Choreographer.doFrame(Choreographer.java:606) at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at c
顯示適配器代碼。 –
adapter = new SimpleAdapter(this,getdata(),R.layout.item,new String [] {「pic」,「txt」},new int [] {R.id.pic,R.id.text}) ;這個? –
問題是Outofmemory不是適配器。你有沒有在Android中表現出大堆? –