该程序想要调用另一个活动爆炸

时间:2015-08-24 05:14:49

标签: android android-intent android-gridview

我想从MainActivity转到大学活动,但它总是给我这个错误。

我意识到错误的原因是GridView,但如果你能弄明白的话,我将不胜感激。

public class MainActivity extends AppCompatActivity {
    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.anasayfa);

        imageView = (ImageView)findViewById(R.id.imageView);
        View.OnClickListener clickListener = new View.OnClickListener() {
            public void onClick(View v) {
                if (v.equals(imageView)) {
                    Intent nextScreen = new Intent(getApplicationContext(), Universiteler.class);
                    startActivity(nextScreen);
                }
            }
        };
        imageView.setOnClickListener(clickListener);
    }
}

Universiteler code:

public class Universiteler extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_universiteler);

        GridView gridView = (GridView)findViewById(R.id.gridView);
        gridView.setAdapter((BaseAdapter) new ImageAdapter(this));
    }

    public class ImageAdapter extends BaseAdapter {
        Context mcontext;
        public ImageAdapter(Context c)
        {
            mcontext=c;
        }
        public int getCount() {  //eleman sayısını tutuyo
            // TODO Auto-generated method stub
            return dizi.length;
        }
        //secilen elemanın konumunu tutuyo
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return null;
        }

        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return 0;
        }


        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub

            ImageView imageview;
            if(convertView==null){
                imageview=new ImageView(mcontext);
                imageview.setLayoutParams(new GridView.LayoutParams(85,85));
                imageview.setScaleType(ImageView.ScaleType.CENTER_CROP);
                imageview.setPadding(8, 8, 8, 8);
            }
            else
                imageview=(ImageView) convertView;
            imageview.setImageResource(dizi[position]);
            return imageview;
        }
        private Integer[] dizi={
                R.drawable.abantizzetuni,R.drawable.abdullahguluni,R.drawable.adanabilimuni,R.drawable.adnanmenderesuni,
                R.drawable.afyonuni,R.drawable.agriibrahimuni,R.drawable.ahievranuni,R.drawable.aidyamanuni,R.drawable.akdenizuni,
                R.drawable.aksarayuni
        };
 /*Burada ekranda gözükmesini istediğimiz resimleri bir diziye atıyoruz.Bu resimleri de drawable klasöründen çekiyoruz*/

    }
}

这是我的LOGCAT:

08-21 02:30:44.337  16610-16610/com.example.abdullah.unihaber D/AndroidRuntime﹕ Shutting down VM
    08-21 02:30:44.349  16610-16610/com.example.abdullah.unihaber W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xa4cdbb20)
    08-21 02:30:44.361  16610-16610/com.example.abdullah.unihaber E/AndroidRuntime﹕ FATAL EXCEPTION: main
        Process: com.example.abdullah.unihaber, PID: 16610
        java.lang.OutOfMemoryError
                at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
                at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:594)
                at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:429)
                at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
                at android.content.res.Resources.loadDrawable(Resources.java:2110)
                at android.content.res.Resources.getDrawable(Resources.java:700)
                at android.widget.ImageView.resolveUri(ImageView.java:638)
                at android.widget.ImageView.setImageResource(ImageView.java:367)
                at com.example.abdullah.unihaber.Universiteler$ImageAdapter.getView(Universiteler.java:95)
                at android.widget.AbsListView.obtainView(AbsListView.java:2255)
                at android.widget.GridView.makeAndAddView(GridView.java:1345)
                at android.widget.GridView.makeRow(GridView.java:345)
                at android.widget.GridView.fillDown(GridView.java:287)
                at android.widget.GridView.fillFromTop(GridView.java:421)
                at android.widget.GridView.layoutChildren(GridView.java:1233)
                at android.widget.AbsListView.onLayout(AbsListView.java:2087)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:493)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
                at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
                at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
                at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
                at android.view.View.layout(View.java:14817)
                at android.view.ViewGroup.layout(ViewGroup.java:4631)
                at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
                at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
                at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
                at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
                at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
                at android.view.Choreographer.doCallbacks(Choreographer.java:574)
                at android.view.Choreographer.doFrame(Choreographer.java:544)
                at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
                at android.os.Handler.handleCallback(Handler.java:733)
                at android.os.Handler.dispatchMessage(Handler.java:95)
                at android.os.Looper.loop(Looper.java:136)
                at android.app.ActivityThread.main(ActivityThread.java:5001)
                at java.lang.reflect.Method.invokeNative(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:515)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
                at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:2)

解决方案1:您可以在activity的清单中使用android:largeHeap =“true”来请求更大的堆大小,但这不适用于任何预装的Honeycomb设备。

解决方案2:使用像Universal Image Loader

这样的Image Loader库

这使得加载大型图像文件时头痛不已,还支持一些额外的功能,如缓存和转换。

here是示例用法。