2015-09-07 93 views
0

你好我正在開發一個應用程序,它能夠爲事件設置日期和時間。現在我成功加載TimePicker,但是當我點擊確定應用程序崩潰,我不明白爲什麼。有一個片段調用我的TimePicker片段,我想將結果檢索到調用者片段。從DialogFragment得到結果

謝謝。

來電片段:

package com.example.matant.gpsportclient.Controllers; 

import android.app.Activity; 
import android.app.DialogFragment; 

import android.os.Bundle; 
import android.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.CheckBox; 
import android.widget.EditText; 
import android.widget.Spinner; 
import android.widget.Switch; 
import android.widget.TextView; 

import com.example.matant.gpsportclient.R; 
import com.example.matant.gpsportclient.Utilities.TimePicker; 


public class CreateEventFragmentController extends Fragment implements View.OnClickListener,TimePicker.OnCompleteListener { 

    private Button btnStartdate,btnstartTime,btnendTime,btninviteUsers,btnEndDate,btnSave,btnCancel; 
    private EditText addressEditText,maxParticipantsEdittext,minAgeEditText; 
    private CheckBox privateEventCbox,reccuringEventCbox,specificAddressCbox; 
    private Spinner sportSpinner,genderSpinner,radiusSpinner; 


    public CreateEventFragmentController() { 
     // Required empty public constructor 
    } 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View v = inflater.inflate(R.layout.fragment_create_event_fragment_controller, container, false); 


     //initialize the widgets 
     btnStartdate = (Button) v.findViewById(R.id.buttonFromDate); 
     btnstartTime = (Button) v.findViewById(R.id.buttonFromTime); 
     btnEndDate = (Button) v.findViewById(R.id.buttonEndDate); 
     btnendTime = (Button) v.findViewById(R.id.buttonEndTime); 
     btninviteUsers = (Button) v.findViewById(R.id.buttonInviteUsers); 
     btnSave = (Button) v.findViewById(R.id.ButtonSave); 
     btnCancel = (Button) v.findViewById(R.id.ButtonCancel); 





     maxParticipantsEdittext = (EditText)v.findViewById(R.id.editTextMaxPaticipants); 
     minAgeEditText = (EditText)v.findViewById(R.id.editTextMinAge); 
     addressEditText = (EditText)v.findViewById(R.id.editTextLocation); 

     privateEventCbox = (CheckBox) v.findViewById(R.id.checkBoxPrivateEvent); 
     reccuringEventCbox = (CheckBox) v.findViewById(R.id.checkBoxRecurring); 
     specificAddressCbox = (CheckBox) v.findViewById(R.id.checkBoxSpecifcAddress); 

     sportSpinner = (Spinner) v.findViewById(R.id.spinnerSports); 
     genderSpinner = (Spinner) v.findViewById(R.id.spinnerGender); 
     radiusSpinner = (Spinner) v.findViewById(R.id.spinnerRadius); 



     //gender spinner 
     ArrayAdapter<CharSequence> genderAdapter = ArrayAdapter.createFromResource(v.getContext(), R.array.eventgender, android.R.layout.simple_spinner_item); 

     genderAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     genderSpinner.setAdapter(genderAdapter); 

     genderSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 


      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     });//gender spinner 



     //Sport Spinner 
     ArrayAdapter<CharSequence> sportAdapter = ArrayAdapter.createFromResource(v.getContext(), R.array.kind_of_sport, android.R.layout.simple_spinner_item); 

     sportAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     sportSpinner.setAdapter(sportAdapter); 

     sportSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 


      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 

     //Sport Spinner 

     //Radius Spinner 
     ArrayAdapter<CharSequence> radiusAdapter = ArrayAdapter.createFromResource(v.getContext(), R.array.radius_range, android.R.layout.simple_spinner_item); 

     radiusAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

     radiusSpinner.setAdapter(radiusAdapter); 

     radiusSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
      @Override 
      public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { 


      } 

      @Override 
      public void onNothingSelected(AdapterView<?> parent) { 

      } 
     }); 
     //Radius Spinner 



     addressEditText.setVisibility(v.GONE); 
     btninviteUsers.setVisibility(v.GONE); 


     //private event check box listener 
     privateEventCbox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(privateEventCbox.isChecked()) 
        btninviteUsers.setVisibility(v.VISIBLE); 
       else 
        btninviteUsers.setVisibility(v.GONE); 

      } 
     });//private event check box listener 

     //create event from specific location check box listener 
     specificAddressCbox.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       if(specificAddressCbox.isChecked()) 
        addressEditText.setVisibility(v.VISIBLE); 
       else 
        addressEditText.setVisibility(v.GONE); 
      } 
     });//create event from specific location check box listener 

     btnstartTime.setOnClickListener(this); 
     btnendTime.setOnClickListener(this); 
     btnStartdate.setOnClickListener(this); 
     btnEndDate.setOnClickListener(this); 

     return v; 
    } 



    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 

    } 


    @Override 
    public void onClick(View v) { 
     switch(v.getId()) { 
      case R.id.buttonFromTime: 
      { 
       { 
        Bundle bundle = new Bundle(); 
        bundle.putInt("Time", 1); 
        DialogFragment TP = new TimePicker(); 
        TP.setArguments(bundle); 
        TP.show(getFragmentManager(),"TimePicker"); 
       } 
      } 
       break; 
      case R.id.buttonEndTime: 
       { 
        { 
         Bundle bundle = new Bundle(); 
         bundle.putInt("Time",2); 
         DialogFragment TP = new TimePicker(); 
         TP.setArguments(bundle); 
         TP.show(getFragmentManager(),"TimePicker"); 
        } 
        break; 
       } 



     } 
    } 

    @Override 
    public void onComplete(String flag,String time) { 

     switch(flag) 
     { 
      case "start_time": 
       btnstartTime.setText(time); 
       break; 
      case "end_time": 
        btnendTime.setText(time); 
       break; 
     } 

    } 
} 

TimePickerFragment:

package com.example.matant.gpsportclient.Utilities; 

import android.app.Dialog; 
import android.app.DialogFragment; 
import android.app.TimePickerDialog; 
import android.os.Bundle; 
import android.util.Log; 

import java.util.Calendar; 

/** 
* Created by matant on 9/7/2015. 
*/ 
public class TimePicker extends DialogFragment implements TimePickerDialog.OnTimeSetListener { 
    public interface OnCompleteListener { 
     public void onComplete(String flag,String time); 
    } 

    private int Time_Picker ; 
    static final int START_TIME = 1; 
    static final int END_TIME = 2; 
    private OnCompleteListener mListener; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     try{ 
      mListener = (OnCompleteListener) getTargetFragment(); 
     }catch (ClassCastException e) 
     { 
      Log.d("Class casting Error",e.getMessage()); 
     } 
    } 

    @Override 
    public Dialog onCreateDialog(Bundle savedInstance){ 
     final Calendar cal = Calendar.getInstance(); 
     int hour = cal.get(Calendar.HOUR_OF_DAY); 
     int minute = cal.get(Calendar.MINUTE); 
     Bundle bundle = this.getArguments(); 
     Log.d("Calendar",String.valueOf(hour)+":"+String.valueOf(minute)); 
     if(bundle!=null){ 
      Time_Picker = bundle.getInt("Time",1); 
     } 
     switch (Time_Picker) 
     { 
      case START_TIME: { 
       Time_Picker = START_TIME; 
       return new TimePickerDialog(getActivity(), this, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity())); 
      } 
      case END_TIME: 
      { 
       Time_Picker = END_TIME; 
       return new TimePickerDialog(getActivity(), this, hour, minute, android.text.format.DateFormat.is24HourFormat(getActivity())); 
      } 
     } 
     return null; 
    } 
    @Override 
    public void onTimeSet(android.widget.TimePicker view, int hourOfDay, int minute) { 
     Log.d("Calendar",String.valueOf(hourOfDay)+":"+String.valueOf(minute)); 
     String s =""; 

     if(Time_Picker == START_TIME) 
     { 
      s = String.valueOf(hourOfDay)+":"+String.valueOf(minute); 
      mListener.onComplete("start_time",s); 
     } 
     else 
     { 
      s = String.valueOf(hourOfDay)+":"+String.valueOf(minute); 
      mListener.onComplete("end_time",s); 

     } 

    } 


} 

錯誤我得到:`

java.lang.NullPointerException: Attempt to invoke interface method 'void com.example.matant.gpsportclient.Utilities.TimePicker$OnCompleteListener.onComplete(java.lang.String, java.lang.String)' on a null object reference 
      at com.example.matant.gpsportclient.Utilities.TimePicker.onTimeSet(TimePicker.java:71) 
+0

您還可以使用託管活性的片段之間的通信。 – Raghunandan

回答

1

你需要調用setTargetFragment才能使用getTargetFragment成功。在你的情況

Bundle bundle = new Bundle(); 
bundle.putInt("Time", 1); 
DialogFragment TP = new TimePicker(); 
TP.setArguments(bundle); 
TP.setTargetFragment(this, 0) 
TP.show(getFragmentManager(),"TimePicker"); 

你可以找到的文檔here

+0

非常感謝。它爲我工作。 –

+0

*它爲我工作* ..甚至在方向改變? – Selvin