简体   繁体   中英

When clicked on any other images apart from first three in the gridview, app abruptly shuts down.

When I click on any of the first three images in the grid view, I can see that image in the full screen view. But when I click on any of the rest of the images, my app abruptly shuts down. Do you see why? Here is my code:

I am a beginner. I will appreciate any answer and detail explanation.

MainActivity.java

import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;

public class MainActivity extends ActionBarActivity {
    private GridView gridView;
    private GridViewAdapter gridAdapter;

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

        gridView = (GridView) findViewById(R.id.gridView);
        gridAdapter = new GridViewAdapter(this, R.layout.grid_item_layout, getData());
        gridView.setAdapter(gridAdapter);

        gridView.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
                ImageItem item = (ImageItem) parent.getItemAtPosition(position);

                //Create intent
                Intent intent = new Intent(MainActivity.this, DetailsActivity.class);
                intent.putExtra("title", item.getTitle());
                intent.putExtra("image", item.getImage());

                //Start details activity
                startActivity(intent);
            }
        });
    }

    /**
     * Prepare some dummy data for gridview
     */
    private ArrayList<ImageItem> getData() {
        final ArrayList<ImageItem> imageItems = new ArrayList<>();
        TypedArray imgs = getResources().obtainTypedArray(R.array.image_ids);
        for (int i = 0; i < imgs.length(); i++) {
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(), imgs.getResourceId(i, -1));
            imageItems.add(new ImageItem(bitmap, "Image#" + i));
        }
        return imageItems;
    }
}

DetailsActivity.java

public class DetailsActivity extends ActionBarActivity {

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

        String title = getIntent().getStringExtra("title");
        Bitmap bitmap = getIntent().getParcelableExtra("image");

        TextView titleTextView = (TextView) findViewById(R.id.title);
        titleTextView.setText(title);

        ImageView imageView = (ImageView) findViewById(R.id.image);
        imageView.setImageBitmap(bitmap);
    }
}

GridViewAdapter.java

public class GridViewAdapter extends ArrayAdapter<ImageItem> {

    private Context context;
    private int layoutResourceId;
    private ArrayList<ImageItem> data = new ArrayList<ImageItem>();

    public GridViewAdapter(Context context, int layoutResourceId, ArrayList<ImageItem> data) {
        super(context, layoutResourceId, data);
        this.layoutResourceId = layoutResourceId;
        this.context = context;
        this.data = data;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View row = convertView;
        ViewHolder holder;

        if (row == null) {
            LayoutInflater inflater = ((Activity) context).getLayoutInflater();
            row = inflater.inflate(layoutResourceId, parent, false);
            holder = new ViewHolder();
            holder.imageTitle = (TextView) row.findViewById(R.id.text);
            holder.image = (ImageView) row.findViewById(R.id.image);
            row.setTag(holder);
        } else {
            holder = (ViewHolder) row.getTag();
        }


        ImageItem item = data.get(position);
        holder.imageTitle.setText(item.getTitle());
        holder.image.setImageBitmap(item.getImage());
        return row;
    }

    static class ViewHolder {
        TextView imageTitle;
        ImageView image;
    }
}

ImageItem.java

public class ImageItem {
    private Bitmap image;
    private String title;

    public ImageItem(Bitmap image, String title) {
        super();
        this.image = image;
        this.title = title;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }
}

LogCat

02-19 22:32:13.814: I/ActivityManager(661): Killing 2165:com.javatechig.gridviewexample/u0a61 (adj 0): error during init
02-19 22:32:13.861: I/art(2183): Late-enabling -Xcheck:jni
02-19 22:32:13.881: I/ActivityManager(661): Start proc com.javatechig.gridviewexample for activity com.javatechig.gridviewexample/.DetailsActivity: pid=2183 uid=10061 gids={50061, 9997} abi=x86
02-19 22:32:13.885: W/ActivityManager(661): Spurious death for ProcessRecord{14ec9f6c 0:com.javatechig.gridviewexample/u0a61}, curProc for 2165: null
02-19 22:32:13.937: E/JavaBinder(661): !!! FAILED BINDER TRANSACTION !!!
02-19 22:32:13.937: W/ActivityManager(661): Exception in new application when starting activity com.javatechig.gridviewexample/.DetailsActivity
02-19 22:32:13.937: W/ActivityManager(661): android.os.TransactionTooLargeException
02-19 22:32:13.937: W/ActivityManager(661):     at android.os.BinderProxy.transactNative(Native Method)
02-19 22:32:13.937: W/ActivityManager(661):     at android.os.BinderProxy.transact(Binder.java:496)
02-19 22:32:13.937: W/ActivityManager(661):     at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:793)
02-19 22:32:13.937: W/ActivityManager(661):     at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1157)
02-19 22:32:13.937: W/ActivityManager(661):     at com.android.server.am.ActivityStackSupervisor.attachApplicationLocked(ActivityStackSupervisor.java:526)
02-19 22:32:13.937: W/ActivityManager(661):     at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:6192)
02-19 22:32:13.937: W/ActivityManager(661):     at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:6254)
02-19 22:32:13.937: W/ActivityManager(661):     at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:481)
02-19 22:32:13.937: W/ActivityManager(661):     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2407)
02-19 22:32:13.937: W/ActivityManager(661):     at android.os.Binder.execTransact(Binder.java:446)
02-19 22:32:13.937: A/ActivityManager(661): Exception thrown launching activities in ProcessRecord{f202f3b 2183:com.javatechig.gridviewexample/u0a61}
02-19 22:32:13.937: A/ActivityManager(661): android.os.TransactionTooLargeException
02-19 22:32:13.937: A/ActivityManager(661):     at android.os.BinderProxy.transactNative(Native Method)
02-19 22:32:13.937: A/ActivityManager(661):     at android.os.BinderProxy.transact(Binder.java:496)
02-19 22:32:13.937: A/ActivityManager(661):     at android.app.ApplicationThreadProxy.scheduleLaunchActivity(ApplicationThreadNative.java:793)
02-19 22:32:13.937: A/ActivityManager(661):     at com.android.server.am.ActivityStackSupervisor.realStartActivityLocked(ActivityStackSupervisor.java:1157)
02-19 22:32:13.937: A/ActivityManager(661):     at com.android.server.am.ActivityStackSupervisor.attachApplicationLocked(ActivityStackSupervisor.java:526)
02-19 22:32:13.937: A/ActivityManager(661):     at com.android.server.am.ActivityManagerService.attachApplicationLocked(ActivityManagerService.java:6192)
02-19 22:32:13.937: A/ActivityManager(661):     at com.android.server.am.ActivityManagerService.attachApplication(ActivityManagerService.java:6254)
02-19 22:32:13.937: A/ActivityManager(661):     at android.app.ActivityManagerNative.onTransact(ActivityManagerNative.java:481)
02-19 22:32:13.937: A/ActivityManager(661):     at com.android.server.am.ActivityManagerService.onTransact(ActivityManagerService.java:2407)
02-19 22:32:13.937: A/ActivityManager(661):     at android.os.Binder.execTransact(Binder.java:446)
02-19 22:32:13.937: I/ActivityManager(661): Killing 2183:com.javatechig.gridviewexample/u0a61 (adj 0): error during init
02-19 22:32:14.001: W/ActivityManager(661): Force removing ActivityRecord{2922ee79 u0 com.javatechig.gridviewexample/.DetailsActivity t91}: app died, no saved state
02-19 22:32:14.024: W/ActivityManager(661): Spurious death for ProcessRecord{f202f3b 0:com.javatechig.gridviewexample/u0a61}, curProc for 2183: null
02-19 22:32:14.105: W/EGL_emulation(1016): eglSurfaceAttrib not implemented
02-19 22:32:14.105: W/OpenGLRenderer(1016): Failed to set EGL_SWAP_BEHAVIOR on surface 0xe29a1260, error=EGL_SUCCESS
02-19 22:32:14.152: W/InputMethodManagerService(661): Got RemoteException sending setActive(false) notification to pid 2130 uid 10061
02-19 22:36:33.443: I/UsageStatsService(661): User[0] Flushing usage stats to disk
02-19 22:46:35.208: I/ProcessStatsService(661): Prepared write state in 0ms
02-19 22:46:35.208: I/ProcessStatsService(661): Prepared write state in 0ms
02-19 22:46:54.854: I/ActivityManager(661): Killing 1044:com.android.printspooler/u0a50 (adj 13): empty for 1813s
02-19 22:46:54.882: I/ActivityManager(661): Killing 1587:com.android.calendar/u0a21 (adj 13): empty for 1813s
02-19 22:46:54.915: I/ActivityManager(661): Killing 1640:com.android.email/u0a31 (adj 13): empty for 1813s
02-19 22:46:54.946: I/ActivityManager(661): Killing 1669:com.android.exchange/u0a32 (adj 13): empty for 1813s
02-19 22:46:54.975: I/ActivityManager(661): Killing 1506:com.android.mms/u0a8 (adj 13): empty for 1813s
02-19 22:46:55.093: D/CountryDetector(661): No listener is left
02-19 22:46:55.103: I/ActivityManager(661): Killing 1241:android.process.media/u0a5 (adj 15): empty for 1813s
02-19 22:46:55.159: W/art(661): Long monitor contention event with owner method=void com.android.server.am.BroadcastQueue.processNextBroadcast(boolean) from BroadcastQueue.java:542 waiters=1 for 244ms
02-19 22:46:55.159: W/libprocessgroup(661): failed to open /acct/uid_10021/pid_1587/cgroup.procs: No such file or directory
02-19 22:46:55.159: W/art(661): Long monitor contention event with owner method=void com.android.server.am.BroadcastQueue.processNextBroadcast(boolean) from BroadcastQueue.java:542 waiters=2 for 217ms
02-19 22:46:55.159: W/libprocessgroup(661): failed to open /acct/uid_10031/pid_1640/cgroup.procs: No such file or directory
02-19 22:46:55.161: W/art(661): Long monitor contention event with owner method=void com.android.server.am.BroadcastQueue.processNextBroadcast(boolean) from BroadcastQueue.java:542 waiters=3 for 183ms
02-19 22:46:55.161: W/libprocessgroup(661): failed to open /acct/uid_10032/pid_1669/cgroup.procs: No such file or directory
02-19 22:46:55.162: W/libprocessgroup(661): failed to open /acct/uid_10008/pid_1506/cgroup.procs: No such file or directory
02-19 22:46:55.163: W/libprocessgroup(661): failed to open /acct/uid_10005/pid_1241/cgroup.procs: No such file or directory
02-19 22:46:55.164: W/libprocessgroup(661): failed to open /acct/uid_10050/pid_1044/cgroup.procs: No such file or directory
02-19 22:47:34.854: I/ActivityManager(661): Killing 1403:com.android.providers.calendar/u0a1 (adj 15): empty for 1819s
02-19 22:47:34.909: W/libprocessgroup(661): failed to open /acct/uid_10001/pid_1403/cgroup.procs: No such file or directory

It's a android.os.TransactionTooLargeException and from the name it is because you are passing big objects like Bitmaps on the intent. You should only pass a reference to it, like the URL or the drawable id from the resources like in your case.

More Details on when you get the TransactionTooLargeException

During a remote procedure call, the arguments and the return value of the call are transferred as Parcel objects stored in the Binder transaction buffer. If the arguments or the return value are too large to fit in the transaction buffer, then the call will fail and TransactionTooLargeException will be thrown.

The Binder transaction buffer has a limited fixed size, currently 1Mb , which is shared by all transactions in progress for the process. Consequently this exception can be thrown when there are many transactions in progress even when most of the individual transactions are of moderate size.

You are facing this exception because other images except the first 3 images are too large to pass with intent.

You can just pass the image uri or image id of clicked item with the intent then and use that on DetailsActivity.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM