2013-02-26 196 views
1

我的应用程序正常工作正常,但如果它在后台运行一段时间或使用任务杀手被使用,它会在启动时崩溃,如果在某个片段上停止运行,在我的程序中称为infofragment。下面是每个碰撞的logcat的信息:Android程序崩溃

02-25 21:12:23.546: E/AndroidRuntime(20048): FATAL EXCEPTION: main 
02-25 21:12:23.546: E/AndroidRuntime(20048): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.fragments/com.example.android.fragments.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.fragments.InfoFragment: make sure class name exists, is public, and has an empty constructor that is public 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2088) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2113) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread.access$700(ActivityThread.java:139) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1224) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.os.Handler.dispatchMessage(Handler.java:99) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.os.Looper.loop(Looper.java:137) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread.main(ActivityThread.java:4918) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at java.lang.reflect.Method.invokeNative(Native Method) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at java.lang.reflect.Method.invoke(Method.java:511) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at dalvik.system.NativeStart.main(Native Method) 
02-25 21:12:23.546: E/AndroidRuntime(20048): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.android.fragments.InfoFragment: make sure class name exists, is public, and has an empty constructor that is public 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.support.v4.app.Fragment.instantiate(Fragment.java:395) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.support.v4.app.FragmentState.instantiate(Fragment.java:96) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1726) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:198) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at com.example.android.fragments.MainActivity.onCreate(MainActivity.java:49) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.Activity.performCreate(Activity.java:5048) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2052) 
02-25 21:12:23.546: E/AndroidRuntime(20048): ... 11 more 
02-25 21:12:23.546: E/AndroidRuntime(20048): Caused by: java.lang.InstantiationException: can't instantiate class com.example.android.fragments.InfoFragment; no empty constructor 
02-25 21:12:23.546: E/AndroidRuntime(20048): at java.lang.Class.newInstanceImpl(Native Method) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at java.lang.Class.newInstance(Class.java:1319) 
02-25 21:12:23.546: E/AndroidRuntime(20048): at android.support.v4.app.Fragment.instantiate(Fragment.java:384) 
02-25 21:12:23.546: E/AndroidRuntime(20048): ... 18 more 

我看到它指向的唯一的事情就是super.onCreate()的主要活动的onCreate()函数。下面是主要活动代码:

public class MainActivity extends FragmentActivity 
     implements MainListFragment.OnListSelectedListener { 

    MainListFragment tempmainfrag; 
    InfoFragment infofrag; 
    int mainPosition = -1; 
    MenuItem menuItemAdd; //plus button in ActionBar/options menu 
    boolean menucreated = false; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.mainlayout); 

     // Check whether the activity is using the layout version with 
     // the fragment_container FrameLayout. If so, we must add the first fragment 
     if (findViewById(R.id.fragment_container) != null) { //meaning, if using phone version 


      // Create an instance of MainListFragment 
      tempmainfrag = new MainListFragment(); //made a context parameter to pass the context 

      // In case this activity was started with special instructions from an Intent, 
      // pass the Intent's extras to the fragment as arguments 
      tempmainfrag.setArguments(getIntent().getExtras()); 



      // Add the fragment to the 'fragment_container' FrameLayout 
      getSupportFragmentManager().beginTransaction() 
        .replace(R.id.fragment_container, tempmainfrag).commit(); 

      Log.i("mydebug","TEMPMAINFRAG: " + tempmainfrag); 
     } 
    } 

    @Override 
    public void onResume() 
    {  
     super.onResume(); 
    } 

    @Override 
    public void onStart() 
    { 

     Log.i("mydebug","1"); 
     if(menuItemAdd != null)  
      menuItemAdd.setVisible(true); //turns on menu item 'add' 

     super.onStart(); 
    } 


    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.menu, menu); 
     menuItemAdd = menu.findItem(R.id.menu_add); 
     menucreated = true; 

     return true; 
    } 


    @Override 
    public boolean onOptionsItemSelected(MenuItem item) { 
     switch (item.getItemId()) { 
      case R.id.menu_add:  //NEED TO IMPLEMENT: MAKES ICON PRESS FOR BACK GO BACK 
       // Create fragment 
       infofrag = new InfoFragment(menuItemAdd); 
       Bundle args = new Bundle(); 
       args.putBoolean(infofrag.ARG_NEW, true); 

       infofrag.setArguments(args); 

       FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

       // Replace whatever is in the fragment_container view with this fragment, 
       // and add the transaction to the back stack so the user can navigate back 
       transaction.replace(R.id.fragment_container, infofrag); 
       transaction.addToBackStack(null); 

       // Commit the transaction 
       transaction.commit(); 
       return true; 
      default: 
       return super.onOptionsItemSelected(item); 
     } 
    } 

    public void onItemSelected(int position, String schedulename, String[] ampm, boolean[] days, int[] times, boolean vibrate) { 
     // The user selected a list item 

     //////////////////////////////TWO PANE LAYOUT STUFF/////////////////////////////////// 
     // Capture the article fragment from the activity layout 
//  InfoFragment articleFrag = (InfoFragment) 
//    getSupportFragmentManager().findFragmentById(R.id.article_fragment); //article_fragment exists in layout-large 
// 
//  if (articleFrag != null) { 
//   // If article frag is available, we're in two-pane layout... 
// 
//   // Call a method in the ArticleFragment to update its content 
//   articleFrag.updateArticleView(position); 
// 
//  } else { 
      // phone layout - swap frags 

      mainPosition = position; 

      // Create fragment and give it an argument for the selected article 
      infofrag = new InfoFragment(menuItemAdd); 
      Bundle args = new Bundle(); 
      args.putInt(infofrag.ARG_POSITION, position); 

      //new stuff to add info 
      args.putString(infofrag.ARG_NAME, schedulename);  
      args.putBooleanArray(infofrag.ARG_DAYS, days); 
      args.putIntArray(infofrag.ARG_TIMES, times); 
      args.putBoolean(infofrag.ARG_VIBRATE, vibrate); 
      args.putStringArray(infofrag.ARG_AMPM, ampm); 
      args.putBoolean(infofrag.ARG_NEW, false); 

      infofrag.setArguments(args); 
      FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 

      // Replace whatever is in the fragment_container view with this fragment, 
      // and add the transaction to the back stack so the user can navigate back 
      transaction.replace(R.id.fragment_container, infofrag); 
      transaction.addToBackStack(null); 

      // Commit the transaction 
      transaction.commit(); 

    } 

    public void saveButtonClicked(View view) { //pass the click to the mainlistfragment 
//  MainListFragment tempmainfrag2 = (MainListFragment)getSupportFragmentManager(). 
//    findFragmentById(R.id.fragment_container); 

     Log.i("mydebug","TEMPMAINFRAG: " + tempmainfrag); 
     if(!infofrag.newsched) //if existing schedule, so save 
     { 
      Log.i("mydebug","Saving schedule..."); 
      boolean redo = false;  //is set true every time info isnt correct when trying to save schedule 
      //create toast 
      Toast toast; 

      //get title 
      EditText titletext = (EditText)this.findViewById(R.id.titletext); 

      //get checkboxes 
      CheckBox check1 = (CheckBox)this.findViewById(R.id.monbox); //recreate checkboxes from view in activity (doesnt extend Activity 
      CheckBox check2 = (CheckBox)this.findViewById(R.id.tuebox); //so use getActivity()) 
      CheckBox check3 = (CheckBox)this.findViewById(R.id.wedbox); 
      CheckBox check4 = (CheckBox)this.findViewById(R.id.thubox); 
      CheckBox check5 = (CheckBox)this.findViewById(R.id.fribox); 
      CheckBox check6 = (CheckBox)this.findViewById(R.id.satbox); 
      CheckBox check7 = (CheckBox)this.findViewById(R.id.sunbox); 
      CheckBox vibratebox = (CheckBox)this.findViewById(R.id.vibratecheckbox); 

      //get times 
      TimePicker startpicker = (TimePicker)this.findViewById(R.id.starttimepicker); 
      TimePicker stoppicker = (TimePicker)this.findViewById(R.id.stoptimepicker); 

      //check for input errors 
      if(titletext.getText().toString().length() == 0) //if title is empty 
      { 
       redo = true; 
       toast = Toast.makeText(view.getContext(), "Enter an event name", 4); 
       toast.show(); 

       //some sick-ass shake animations!!! 
       Animation shake = AnimationUtils.loadAnimation(titletext.getContext(), R.anim.shake_big); 
       this.findViewById(R.id.titletext).startAnimation(shake); 
      } 
      else if((!check1.isChecked()) && (!check2.isChecked()) && (!check3.isChecked()) && 
        (!check4.isChecked()) && (!check5.isChecked()) && (!check6.isChecked()) && 
        (!check7.isChecked())) //if all checkboxes arent checked 
      { 
       redo = true; 
       toast = Toast.makeText(view.getContext(), "At least one day of week must be checked", 4); 
       toast.show(); 

       //more sick-ass shake animations!!! 
       Animation shake = AnimationUtils.loadAnimation(titletext.getContext(), R.anim.shake_small); 
       this.findViewById(R.id.checkboxes).startAnimation(shake); 
       this.findViewById(R.id.daysofweek).startAnimation(shake); 
       this.findViewById(R.id.frequencytext).startAnimation(shake); 
      } 

      if(!redo) //if all info is fine 
      { 
       //check to see if time goes into next day 
       if((startpicker.getCurrentHour() > stoppicker.getCurrentHour())|| 
         ((startpicker.getCurrentHour() == stoppicker.getCurrentHour()) 
           && (startpicker.getCurrentMinute() >= stoppicker.getCurrentMinute()))) 
       { 
        toast = Toast.makeText(view.getContext(), "Note: Stop time is earlier than start time, so this schedule stops at next day", Toast.LENGTH_LONG); 
        toast.show(); 
       } 

       toast = Toast.makeText(view.getContext(), "Schedule saved", Toast.LENGTH_LONG); 
       toast.show(); 

       //changing old schedule to new one 
       boolean[] tempdays = {check1.isChecked(), check2.isChecked(), check3.isChecked(), check4.isChecked(), 
         check5.isChecked(), check6.isChecked(), check7.isChecked()}; 

       Log.i("mydebug","Time info read from counters: Start hour: " + startpicker.getCurrentHour() + "\nStop hour: " + stoppicker.getCurrentHour()); 

       tempmainfrag.mainObjectList.changeSchedule(mainPosition, titletext.getText().toString(), tempdays, vibratebox.isChecked(), 
         startpicker.getCurrentHour(), startpicker.getCurrentMinute(), stoppicker.getCurrentHour(), stoppicker.getCurrentMinute()); 

       //used to hide keyboard in case its still open when displaying list 
       InputMethodManager imm = (InputMethodManager)this.getSystemService(
          Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(titletext.getWindowToken(), 0); 

       this.onBackPressed(); //replicates backpress to go back to list 
      } 
     } 
     else  //if new schedule needs to be created 
     { 
      Log.i("mydebug","Creating new schedule..."); 
      boolean redo = false; 
      //create toast 
      Toast toast; 

      //get title 
      EditText titletext = (EditText)this.findViewById(R.id.titletext); 

      //get checkboxes 
      CheckBox check1 = (CheckBox)this.findViewById(R.id.monbox); //recreate checkboxes from view in activity (doesnt extend Activity 
      CheckBox check2 = (CheckBox)this.findViewById(R.id.tuebox); //so use getActivity()) 
      CheckBox check3 = (CheckBox)this.findViewById(R.id.wedbox); 
      CheckBox check4 = (CheckBox)this.findViewById(R.id.thubox); 
      CheckBox check5 = (CheckBox)this.findViewById(R.id.fribox); 
      CheckBox check6 = (CheckBox)this.findViewById(R.id.satbox); 
      CheckBox check7 = (CheckBox)this.findViewById(R.id.sunbox); 
      CheckBox vibratebox = (CheckBox)this.findViewById(R.id.vibratecheckbox); 

      //get times 
      TimePicker startpicker = (TimePicker)this.findViewById(R.id.starttimepicker); 
      TimePicker stoppicker = (TimePicker)this.findViewById(R.id.stoptimepicker); 

      EditText temppp = titletext; 
      //check for input errors 
      if(titletext.getText().toString().length() == 0) //if title is empty 
      { 
       redo = true; 
       toast = Toast.makeText(view.getContext(), "Enter an event name", 4); 
       toast.show(); 

       //some sick-ass shake animations!!! 
       Animation shake = AnimationUtils.loadAnimation(titletext.getContext(), R.anim.shake_big); 
       this.findViewById(R.id.titletext).startAnimation(shake); 
      } 
      else if((!check1.isChecked()) && (!check2.isChecked()) && (!check3.isChecked()) && 
        (!check4.isChecked()) && (!check5.isChecked()) && (!check6.isChecked()) && 
        (!check7.isChecked())) //if all checkboxes arent checked 
      { 
       redo = true; 
       toast = Toast.makeText(view.getContext(), "At least one day of week must be checked", 4); 
       toast.show(); 

       //more sick-ass shake animations!!! 
       Animation shake = AnimationUtils.loadAnimation(titletext.getContext(), R.anim.shake_small); 
       this.findViewById(R.id.checkboxes).startAnimation(shake); 
       this.findViewById(R.id.daysofweek).startAnimation(shake); 
       this.findViewById(R.id.frequencytext).startAnimation(shake); 
      } 

      if(!redo) //if all info is fine 
      { 
       //check to see if time goes into next day 
       if((startpicker.getCurrentHour() > stoppicker.getCurrentHour())|| 
         ((startpicker.getCurrentHour() == stoppicker.getCurrentHour()) 
           && (startpicker.getCurrentMinute() >= stoppicker.getCurrentMinute()))) 
       { 
        toast = Toast.makeText(view.getContext(), "Note: Stop time is earlier than start time, so this schedule stops at next day", Toast.LENGTH_LONG); 
        toast.show(); 
       } 

       toast = Toast.makeText(view.getContext(), "Schedule created", Toast.LENGTH_LONG); 
       toast.show(); 

       //changing old schedule to new one 
       boolean[] tempdays = {check1.isChecked(), check2.isChecked(), check3.isChecked(), check4.isChecked(), 
         check5.isChecked(), check6.isChecked(), check7.isChecked()}; 

       Log.i("mydebug","Time info read from counters: Start hour: " + startpicker.getCurrentHour() + "\nStop hour: " + stoppicker.getCurrentHour()); 

       tempmainfrag.mainObjectList.addSchedule(titletext.getText().toString(), tempdays, vibratebox.isChecked(), 
         startpicker.getCurrentHour(), startpicker.getCurrentMinute(), stoppicker.getCurrentHour(), stoppicker.getCurrentMinute()); 

       //used to hide keyboard in case its still open when displaying list 
       InputMethodManager imm = (InputMethodManager)this.getSystemService(
          Context.INPUT_METHOD_SERVICE); 
        imm.hideSoftInputFromWindow(titletext.getWindowToken(), 0); 

       this.onBackPressed(); //replicates backpress to go back to list 
      } 
     } 
    } 

    public void deleteButtonClicked(View view) 
    { 
     //tempmainfrag = (MainListFragment)getSupportFragmentManager(). 
       //findFragmentById(R.id.fragment_container); 

     if(!infofrag.newsched) //if existing schedule, so ask to delete 
     { 
      //make a notification 

      AlertDialog.Builder builder = new AlertDialog.Builder(this); 
      builder.setTitle("Delete?"); 
      builder.setIcon(R.drawable.trash_icon); 
      builder.setMessage("Are you sure you wish to delete this schedule?") 
       .setCancelable(false) 
       .setPositiveButton("Delete", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         tempmainfrag.mainObjectList.removeSchedule(mainPosition); 

         Toast toast; 
         toast = Toast.makeText(tempmainfrag.getActivity(), "Schedule deleted", 4); 
         toast.show(); 

         tempmainfrag.exit(); 
        } 
       }) 
       .setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog, int id) { 
         dialog.dismiss(); 
        } 
       }); 
      AlertDialog alert = builder.create(); 
      alert.show(); 
     } 
     else //if new schedule, so just cancel 
      tempmainfrag.exit(); 
    } 

    public void hideKeyboard() //hides keyboard, called whenever reverting back to list 
    { 
     InputMethodManager imm = (InputMethodManager)getSystemService(
        Context.INPUT_METHOD_SERVICE); 
     imm.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), 0); 
    } 
} 

这里是使应用程序崩溃,如果最后的悬空和任务的杀手用于片段代码:

public class InfoFragment extends Fragment { 
    //strings used to get info from MainListFragment 
    public String ARG_POSITION = "position"; 
    public String ARG_NAME = "name"; 
    public String ARG_DAYS = "days"; 
    public String ARG_TIMES = "times"; 
    public String ARG_VIBRATE = "vibrate"; 
    public String ARG_AMPM = "ampm"; 
    public String ARG_NEW = "new"; 

    boolean newsched = false; 
    int mCurrentPosition = -1; 
    MenuItem menuItemAddInfo; 

    public InfoFragment(MenuItem add) 
    { 
     menuItemAddInfo = add; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

     // If activity recreated (such as from screen rotate), restore 
     // the previous article selection set by onSaveInstanceState(). 
     // This is primarily necessary when in the two-pane layout. 
     if (savedInstanceState != null) { 
      mCurrentPosition = savedInstanceState.getInt(ARG_POSITION); 
     } 

     // Inflate the layout for this fragment 
     return inflater.inflate(R.layout.infolayout, container, false); 
    } 

    @Override 
    public void onStart() { 

     super.onStart(); 
     menuItemAddInfo.setVisible(false); 

     // During startup, check if there are arguments passed to the fragment. 
     // onStart is a good place to do this because the layout has already been 
     // applied to the fragment at this point so we can safely call the method 
     // below that sets the article text. 
     Bundle args = getArguments(); 
     if (!args.getBoolean(ARG_NEW)) //if creating fragment with existing info i.e. not new schedule 
     { 
      newsched = false; 
      Log.i("mydebug", "TYPE: Existing schedule"); 
      // if a list item is clicked and arguments are fed in 
      updateInfoView(args.getInt(ARG_POSITION), args.getString(ARG_NAME), args.getStringArray(ARG_AMPM), args.getBooleanArray(ARG_DAYS), 
        args.getIntArray(ARG_TIMES), args.getBoolean(ARG_VIBRATE)); 
     } 
     else 
     { 
      Log.i("mydebug", "TYPE: New schedule"); 
      Button changesave = (Button)getActivity().findViewById(R.id.savebutton); 
      changesave.setText("Create"); 
      Button changedelete = (Button)getActivity().findViewById(R.id.cancelbutton); 
      changedelete.setText("Cancel"); 
      newsched = true; 

      //set defaults 
      TimePicker startpicker = (TimePicker)getActivity().findViewById(R.id.starttimepicker); 
      TimePicker stoppicker = (TimePicker)getActivity().findViewById(R.id.stoptimepicker); 
      startpicker.setCurrentHour(8); 
      startpicker.setCurrentMinute(0); 
      stoppicker.setCurrentHour(15); 
      stoppicker.setCurrentMinute(0); 
      CheckBox vibratebox = (CheckBox)getActivity().findViewById(R.id.vibratecheckbox); 
      vibratebox.setChecked(true); 
     } 
     //set up custom font 
     Typeface customfont = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Light.ttf"); 

     TextView frequency = (TextView)getActivity().findViewById(R.id.frequencytext); 
     TextView start = (TextView)getActivity().findViewById(R.id.starttext); 
     TextView stop = (TextView)getActivity().findViewById(R.id.stoptext); 
     CheckBox vibratebox = (CheckBox)getActivity().findViewById(R.id.vibratecheckbox); 
     TextView mon = (TextView)getActivity().findViewById(R.id.montext); 
     TextView tue = (TextView)getActivity().findViewById(R.id.tuetext); 
     TextView wed = (TextView)getActivity().findViewById(R.id.wedtext); 
     TextView thu = (TextView)getActivity().findViewById(R.id.thutext); 
     TextView fri = (TextView)getActivity().findViewById(R.id.fritext); 
     TextView sat = (TextView)getActivity().findViewById(R.id.sattext); 
     TextView sun = (TextView)getActivity().findViewById(R.id.suntext); 
     EditText title = (EditText)getActivity().findViewById(R.id.titletext); 

     frequency.setTypeface(customfont); 
     start.setTypeface(customfont); 
     stop.setTypeface(customfont); 
     vibratebox.setTypeface(customfont); 
     mon.setTypeface(customfont); 
     tue.setTypeface(customfont); 
     wed.setTypeface(customfont); 
     thu.setTypeface(customfont); 
     fri.setTypeface(customfont); 
     sat.setTypeface(customfont); 
     sun.setTypeface(customfont); 
     title.setTypeface(customfont); 
    } 

    @Override 
    public void onStop() 
    { 
     if(menuItemAddInfo != null)  
      menuItemAddInfo.setVisible(true); //turns on menu item 'add' 
     super.onStop(); 
    } 
    public void updateInfoView(int position, String schedulename, String[] ampm, boolean[] days, int[] times, boolean vibrate) { //previously used to set which article 

     mCurrentPosition = position; 
     //debugging info 
     String debugdays = String.valueOf(days[0]) + ", " + String.valueOf(days[1]) + ", " + String.valueOf(days[2]) + ", " + 
       String.valueOf(days[3]) + ", " + String.valueOf(days[4]) + ", " + String.valueOf(days[5]) + ", " + String.valueOf(days[6]); 
     String debug = "LISTITEMCLICKED:\nPosition:" + position + "\nName:" + schedulename + "\nStart AM/PM:" + ampm[0] + "\nStop AM/PM:" + ampm[1] 
       + "\nDays:" + debugdays + "\nStart time: " + times[0] + ":" + times[1] + "\nStop time: " + times[2] + ":" + times[3] + "\nVibrate: " + vibrate; 
     Log.i("mydebug",debug); 


     //set title 
     EditText titletext = (EditText)getActivity().findViewById(R.id.titletext); 
     titletext.setText(schedulename); 
     titletext.setSelection(titletext.getText().length()); //just sets cursor at end to fulfill my OCD 

     //set days 
     CheckBox check1 = (CheckBox)getActivity().findViewById(R.id.monbox); //recreate checkboxes from view in activity (doesnt extend Activity 
     CheckBox check2 = (CheckBox)getActivity().findViewById(R.id.tuebox); //so use getActivity()) 
     CheckBox check3 = (CheckBox)getActivity().findViewById(R.id.wedbox); 
     CheckBox check4 = (CheckBox)getActivity().findViewById(R.id.thubox); 
     CheckBox check5 = (CheckBox)getActivity().findViewById(R.id.fribox); 
     CheckBox check6 = (CheckBox)getActivity().findViewById(R.id.satbox); 
     CheckBox check7 = (CheckBox)getActivity().findViewById(R.id.sunbox); 
     CheckBox vibratebox = (CheckBox)getActivity().findViewById(R.id.vibratecheckbox); 

     if (days[0]) 
      check1.setChecked(true);   //enable checkboxes to correct bool value 
     if (days[1]) 
      check2.setChecked(true); 
     if (days[2]) 
      check3.setChecked(true); 
     if (days[3]) 
      check4.setChecked(true); 
     if (days[4]) 
      check5.setChecked(true); 
     if (days[5]) 
      check6.setChecked(true); 
     if (days[6]) 
      check7.setChecked(true); 
     if (vibrate) 
      vibratebox.setChecked(true); 

     //set times 
     TimePicker startpicker = (TimePicker)getActivity().findViewById(R.id.starttimepicker); 
     TimePicker stoppicker = (TimePicker)getActivity().findViewById(R.id.stoptimepicker); 

     startpicker.setCurrentHour(times[0]); 
     startpicker.setCurrentMinute(times[1]); 
     stoppicker.setCurrentHour(times[2]); 
     stoppicker.setCurrentMinute(times[3]); 

    } 



    @Override 
    public void onSaveInstanceState(Bundle outState) { 
     super.onSaveInstanceState(outState); 

     // Save the current article selection in case we need to recreate the fragment 
     outState.putInt(ARG_POSITION, mCurrentPosition); 
    } 
} 

回答

4

确保类InfoFragment具有公共默认构造函数

public class InfoFragment extends Fragment { 

    public InfoFragment() 
    { 
     // Do some stuff 
    } 

    public InfoFragment(MenuItem add) 
    { 
     menuItemAddInfo = add; 
    } 
} 
+0

只是说明到OP - 这可能会导致数据丢失/因为当系统重新制作的片段NPE上,'add'不会再行通过。 – 2013-02-26 03:25:35

+0

@ A - C我同意你的意见。当系统调用'onSaveInstanceState()'时,'setRetainInstance()'可能会帮助和/或保存数据。 – Geros 2013-02-26 03:27:20

+1

'MenuItems'不能序列化。我会尝试在Fragment中创建一个接口,由主Activity执行。这样,Fragment可以在需要时从Activity中请求'MenuItem'。 – 2013-02-26 03:31:21