TransactionTooLargeException从android中的片段启动活动时

时间:2017-01-04 17:21:32

标签: android android-fragments android-activity

我在一个片段里面有一个ListView,它应该在点击时启动另一个活动。这是代码:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                                long id) {
            Intent myIntent = new Intent(getActivity(), GroupActivity.class);
            startActivity(myIntent);
        }

    });

它似乎会在仿真器屏幕上加载GroupActivity,然后崩溃并出现以下异常(完整堆栈跟踪):

    !!! FAILED BINDER TRANSACTION !!!  (parcel size = 861936)
Shutting down VM
FATAL EXCEPTION: main
java.lang.RuntimeException: android.os.TransactionTooLargeException: data parcel size 861936 bytes
 at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3752)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6077)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
Caused by: android.os.TransactionTooLargeException: data parcel size 861936 bytes
 at android.os.BinderProxy.transactNative(Native Method)
 at android.os.BinderProxy.transact(Binder.java:615)
 at android.app.ActivityManagerProxy.activityStopped(ActivityManagerNative.java:3606)
 at android.app.ActivityThread$StopInfo.run(ActivityThread.java:3744)
 at android.os.Handler.handleCallback(Handler.java:751) 
 at android.os.Handler.dispatchMessage(Handler.java:95) 
 at android.os.Looper.loop(Looper.java:154) 
 at android.app.ActivityThread.main(ActivityThread.java:6077) 
 at java.lang.reflect.Method.invoke(Native Method) 
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

我已经尝试从这里加载其他活动并且我得到了相同的崩溃,所以它似乎不是GroupActivity中的问题,而是在片段中。以下是整个片段的代码:

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.ListView;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.packagename.Activities.GroupActivity;
import com.packagename.Models.Group;
import com.packagename.Models.User;
import com.packagename.R;
import com.packagename.util.ArrayAdapters.GroupsListArrayAdapter;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * A simple {@link Fragment} subclass.
 */
public class GroupsFragment extends Fragment {

    // List view
    private ListView listView;
    // Listview Adapter
    public GroupsListArrayAdapter adapter;
    // Search EditText
    EditText inputSearch;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_groups, container, false);
    return view;
}

@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
    listView = (ListView) view.findViewById(R.id.groups_list_view);
    inputSearch = (EditText) view.findViewById(R.id.inputSearch);
    loadGroupsForUser();
    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int position,
                                long id) {
            Intent myIntent = new Intent(getActivity(), GroupActivity.class);
            startActivity(myIntent);
        }

    });
    //enabling searching groups
    inputSearch.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
            // When user changed the Text
            GroupsFragment.this.adapter.getFilter().filter(cs);
        }

        @Override
        public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
                                      int arg3) {
            // TODO Auto-generated method stub
        }

        @Override
        public void afterTextChanged(Editable arg0) {
            // TODO Auto-generated method stub
        }
    });

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.add_group, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case R.id.add:
            Log.i("log", "add clicked");
            break;
    }
    return true;
}

private void loadGroupsForUser() {
    RequestQueue queue = Volley.newRequestQueue(getContext());
    String url = myurl;
    JsonObjectRequest jsonRequest = new JsonObjectRequest(url, null,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    try {
                        ArrayList<Group> groups = Group.fromJson(response.getJSONArray("groups"));
                        adapter = new GroupsListArrayAdapter(getContext(), groups);
                        listView.setAdapter(adapter);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    // Add the request to the RequestQueue.
    queue.add(jsonRequest);
}

private void moveToGroup() {
    Intent intent = new Intent(getActivity(), GroupActivity.class);
    startActivityForResult(intent, 0);
}
}

我已经尝试了尽可能多的解决方案,但似乎无法解决问题。在其中一个onViewCreate方法中以相同的方式启动一个活动就可以了。只有在我上面的听众触发它时,甚至只是一个单独的按钮。谢谢你的帮助

0 个答案:

没有答案