2014-10-16 109 views
0

這對我來說是一個相當奇怪的問題。我正在使用自定義列表來顯示我的項目和另一個片段以顯示更多選定項目的數據。然而,每當我按下並選擇另一個項目,該應用程序給我一個非法的異常錯誤。我在代碼中做了什麼錯誤? 如果我沒有提交該事務,則不會顯示細節片段。片段在onBackPressed後選擇另一個項目後崩潰

這是我的活動片段:

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) 

回答

1
android.support.v4.app.FragmentTransaction transac = ft; 

看起來好像您已經致電ft

android.support.v4.app.FragmentTransaction transac = getSupportFragmentManager().beginTransaction(); 
+0

如果我將其更改爲改變它,這是錯誤返回: '無法從類型使靜態參考非靜態方法getSupportFragmentManager()FragmentActivity' – 2014-10-17 00:13:07

+0

這是我的ft: 'static android.support.v4.app.FragmentTransaction ft; ft = getSupportFragmentManager()。beginTransaction();' – 2014-10-17 00:17:52