对我来说,这是一个相当奇怪的问题。我正在使用自定义列表来显示我的项目和另一个片段以显示所选项目的更多数据。但是,每当我按下并选择另一个项目时,该应用程序会给我一个非法的异常错误。我的代码中我做错了什么? 如果我不提交事务,则不会显示详细信息片段。
这是我的活动片段:
public static class EventsSectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
SharedPreferences settings = null;
Editor editor;
ListView list;
TextView noEventsTv;
public EventsSectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
recLifeCycle_with_savedInstanceState(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_events, container, false);
list = (ListView) rootView.findViewById(R.id.list);
noEventsTv = (TextView) rootView.findViewById(R.id.norecordsTV);
new GetEventsAsyncTask((MainActivity) getActivity()).execute(nyp.edu.sg.alumnigo.SharedPreferences.getUserName(getActivity()));
settings = PreferenceManager.getDefaultSharedPreferences(getActivity());
return rootView;
}
public void updateEventList(final List<Event> eventList)
{
Log.i("updateEventList:eventlist.count", String.valueOf(eventList.size()));
if (eventList.size() > 0)
{
CustomList_Events adapter = new CustomList_Events(getActivity(), eventList);
noEventsTv.setVisibility(View.GONE);
list.setVisibility(View.VISIBLE);
list.setAdapter(adapter);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
event_id = "";
event_title = "";
event_desc = "";
start_date = "";
end_date = "";
start_time = "";
end_time = "";
picture_path = "";
location = "";
Event event = new Event();
event = eventList.get(position);
Log.i("Event_details", event.getEvent_id());
event_id = eventList.get(position).getEvent_id();
event_title = eventList.get(position).getEvent_title();
event_desc = eventList.get(position).getEvent_desc();
start_date = eventList.get(position).getStart_date();
end_date = eventList.get(position).getEnd_date();
start_time = eventList.get(position).getStart_time();
end_time = eventList.get(position).getEnd_time();
picture_path = eventList.get(position).getPicture_path();
location = eventList.get(position).getLocation();
// direct to event details fragment
startEventDetailsFragment();
}
});
adapter.notifyDataSetChanged();
list.invalidateViews();
}
else
{
noEventsTv.setVisibility(View.VISIBLE);
list.setVisibility(View.GONE);
}
}
这是详细信息片段:
public static class EventDetailsFragment extends Fragment {
public EventDetailsFragment() {
}
ImageView banner;
TextView e_title, e_loc, e_date, e_time, e_desc;
String eventID;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
recLifeCycle_with_savedInstanceState(savedInstanceState);
View rootView = inflater.inflate(R.layout.fragment_event_details, container, false);
banner = (ImageView) rootView.findViewById(R.id.banner_img);
e_title = (TextView) rootView.findViewById(R.id.event_title);
e_loc = (TextView) rootView.findViewById(R.id.event_location);
e_date = (TextView) rootView.findViewById(R.id.event_date);
e_time = (TextView) rootView.findViewById(R.id.event_time);
e_desc = (TextView) rootView.findViewById(R.id.event_desc);
Bundle args = getArguments();
if (args != null && args.containsKey("event_id")) {
eventID = args.getString("event_id");
e_title.setText(args.getString("event_title"));
e_desc.setText(args.getString("event_desc"));
e_time.setText(args.getString("start_time") + " - " + args.getString("end_time"));
e_date.setText(args.getString("start_date") + " - " + args.getString("end_date"));
e_loc.setText(args.getString("event_location"));
new GetImageAsyncTask(banner).execute(Constants.HOST_NAME + "/" + Constants.CMS_NAME + "/" + args.getString("pic_path"));
}
return rootView;
}
然后,这是将数据传递给细节片段的方法:
public static void startEventDetailsFragment() {
EventDetailsFragment fragment = new EventDetailsFragment();
Bundle bundle = new Bundle();
bundle.putString("event_id", event_id);
bundle.putString("event_title", event_title);
bundle.putString("event_desc", event_desc);
bundle.putString("start_time", start_time);
bundle.putString("end_time", end_time);
bundle.putString("start_date", start_date);
bundle.putString("end_date", end_date);
bundle.putString("event_location", location);
bundle.putString("pic_path", picture_path);
fragment.setArguments(bundle);
android.support.v4.app.FragmentTransaction transac = ft;
transac.replace(R.id.fragment_events, fragment);
transac.addToBackStack(null);
transac.commit();
}
这是logcat:
10-17 01:54:01.494: I/Event_details(13856): EV00000018
10-17 01:54:01.494: I/MYTAG(13856): EventDetailsFragment.onCreateView / savedInstanceState == null
10-17 01:54:01.514: I/MYTAG(13856): EventDetailsFragment.onStart
10-17 01:54:01.514: I/MYTAG(13856): EventDetailsFragment.onResume
10-17 01:54:01.544: D/dalvikvm(13856): GC_FOR_ALLOC freed 2108K, 17% free 10890K/13044K, paused 11ms, total 11ms
10-17 01:54:12.894: I/MYTAG(13856): EventDetailsFragment.onPause
10-17 01:54:12.894: I/MYTAG(13856): EventDetailsFragment.onStop
10-17 01:54:12.894: I/MYTAG(13856): EventDetailsFragment.onDestroyView
10-17 01:54:12.904: I/MYTAG(13856): EventDetailsFragment.onDestroy
10-17 01:54:12.904: I/MYTAG(13856): EventDetailsFragment.onDetach
10-17 01:54:17.294: I/Event_details(13856): EV00000007
10-17 01:54:17.294: D/AndroidRuntime(13856): Shutting down VM
10-17 01:54:17.294: W/dalvikvm(13856): threadid=1: thread exiting with uncaught exception (group=0x430f9140)
10-17 01:54:17.304: E/AndroidRuntime(13856): FATAL EXCEPTION: main
10-17 01:54:17.304: E/AndroidRuntime(13856): Process: nyp.edu.sg.alumnigo, PID: 13856
10-17 01:54:17.304: E/AndroidRuntime(13856): java.lang.IllegalStateException: commit already called
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:582)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
10-17 01:54:17.304: E/AndroidRuntime(13856): at nyp.edu.sg.alumnigo.MainActivity.startEventDetailsFragment(MainActivity.java:1882)
10-17 01:54:17.304: E/AndroidRuntime(13856): at nyp.edu.sg.alumnigo.MainActivity$EventsSectionFragment$1.onItemClick(MainActivity.java:752)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.widget.AdapterView.performItemClick(AdapterView.java:299)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.widget.AbsListView.performItemClick(AbsListView.java:1156)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2947)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.widget.AbsListView$3.run(AbsListView.java:3681)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.os.Handler.handleCallback(Handler.java:733)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.os.Handler.dispatchMessage(Handler.java:95)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.os.Looper.loop(Looper.java:149)
10-17 01:54:17.304: E/AndroidRuntime(13856): at android.app.ActivityThread.main(ActivityThread.java:5257)
10-17 01:54:17.304: E/AndroidRuntime(13856): at java.lang.reflect.Method.invokeNative(Native Method)
10-17 01:54:17.304: E/AndroidRuntime(13856): at java.lang.reflect.Method.invoke(Method.java:515)
10-17 01:54:17.304: E/AndroidRuntime(13856): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
10-17 01:54:17.304: E/AndroidRuntime(13856): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
10-17 01:54:17.304: E/AndroidRuntime(13856): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
android.support.v4.app.FragmentTransaction transac = ft;
看起来你已经在ft
上调用了提交。
用
更改它android.support.v4.app.FragmentTransaction transac = getSupportFragmentManager().beginTransaction();