我的主要活动中有一个按钮,当它被点击时,它会导致我的GridView活动,但应用程序强制关闭。
这是我的activity_gridview.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapps.gridviewexample.GridViewActivity"
android:padding="4dp">
<GridView
android:id="@+id/gridview"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:numColumns="3"
android:columnWidth="150dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
>
</GridView>
</LinearLayout>
GridViewActivity.java:
package com.myapps.gridviewexample;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class GridViewActivity extends AppCompatActivity {
public Integer[] images = {
R.drawable.pic1,
R.drawable.pic2,
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_gridview);
GridView gridview = (GridView) findViewById(R.id.gridview);
if (gridview != null) {
gridview.setAdapter(new ImageAdapter(GridViewActivity.this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent intent = new Intent(GridViewActivity.this, TestActivity.class);
GridViewActivity.this.startActivity(intent);
intent.putExtra("POSITION", position + 1);
}
});
}
}
public static int convertDpToPixels(float dp, Context context){
Resources resources = context.getResources();
return (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP,
dp,
resources.getDisplayMetrics()
);
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public int getCount() {
return images.length;
}
public Object getItem(int position) {
return images[position];
}
public long getItemId(int position) {
return 0;
}
public ImageAdapter(Context c) {
mContext = c;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
float w = 100;
float h = 100;
int width_pixel = convertDpToPixels(w, GridViewActivity.this);
int height_pixel = convertDpToPixels(h, GridViewActivity.this);
imageView.setLayoutParams(new GridView.LayoutParams(width_pixel, height_pixel));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(8, 8, 8, 8);
imageView.setMaxHeight(60);
} else {
imageView = (ImageView) convertView;
}
imageView.setBackgroundResource(images[position]);
return imageView;
}
}
}
logcat输出:
10-07 01:26:05.358 1534-1534/com.myapps.gridviewexample E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:500)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:353)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
at android.content.res.Resources.loadDrawable(Resources.java:1930)
at android.content.res.Resources.getDrawable(Resources.java:659)
at android.view.View.setBackgroundResource(View.java:14179)
at com.myapps.gridviewexample.GridViewActivity$ImageAdapter.getView(GridViewActivity.java:89)
at android.widget.AbsListView.obtainView(AbsListView.java:2267)
at android.widget.GridView.makeAndAddView(GridView.java:1331)
at android.widget.GridView.makeRow(GridView.java:331)
at android.widget.GridView.fillDown(GridView.java:283)
at android.widget.GridView.fillFromTop(GridView.java:407)
at android.widget.GridView.layoutChildren(GridView.java:1219)
at android.widget.AbsListView.onLayout(AbsListView.java:2102)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1638)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1422)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
at android.view.View.layout(View.java:13754)
at android.view.ViewGroup.layout(ViewGroup.java:4362)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1866)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1687)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
at android.view.Choreographer.doCallbacks(Choreographer.java:555)
at android.view.Choreographer.doFrame(Choreographer.java:525)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
我尝试了一切,但仍然强行关闭 感谢
答案 0 :(得分:1)
对于遇到大图片问题的人,请查看Loading Large Bitmaps Efficiently上的google文档,了解如何在没有 OutOfMemoryError 的情况下加载大图片。另请查看修复相同问题的Adam Stelmaszczyk's code。