当我点击网格视图中的前三个图像中的任何一个时,我可以在全屏视图中看到该图像。但是,当我点击任何其他图像时,我的应用程序突然关闭。你明白为什么吗?这是我的代码:
我是初学者。我将不胜感激任何答案和详细解释。
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
答案 0 :(得分:0)
它是一个android.os.TransactionTooLargeException
,因为你在意图上传递像Bitmaps这样的大对象。您应该只传递对它的引用,例如来自资源的URL或drawable id,如您的情况。
有关何时获得TransactionTooLargeException
的详细信息在远程过程调用期间,参数和返回值 该调用将作为存储在Binder中的Parcel对象进行传输 事务缓冲区。如果参数或返回值太大 为了适应事务缓冲区,那么调用将失败并且 将抛出TransactionTooLargeException。
Binder事务缓冲区的固定大小有限,目前 1Mb , 由进程中的所有正在进行的事务共享。 因此,当存在许多异常时,可以抛出此异常 即使在大多数个别交易中,交易仍在进行中 中等大小。
答案 1 :(得分:0)
您正面临此异常,因为除了前3张图像之外的其他图像太大而无法通过意图传递。
您可以使用intent传递单击项目的图像uri或图像ID,然后在DetailsActivity上使用它。