I'm a newbie in android and I have some problem when I use fragment follow some tutorial on Google. First prolem: I try simple send data have type is String form fragment to other fragment but it's not working. Here is my SearchFargment:
public class SearchFragment extends Fragment {
private EditText et_departure;
private EditText et_arrival;
private EditText et_date;
// private Button btn_searching;
private DatePickerDialog fromDatePickerDialog;
private SimpleDateFormat dateFormatter;
private SearchTicketPresenter searchTicketPresenter;
public SearchFragment(){
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
et_departure = (EditText)getView().findViewById(R.id.et_departure);
et_arrival = (EditText)getView().findViewById(R.id.et_arrival);
et_date = (EditText)getView().findViewById(R.id.et_date);
dateFormatter = new SimpleDateFormat("yyyy-MM-dd", Locale.US);
searchTicketPresenter = new SearchTicketPresenter(this);
//setDateTimeField();
Button btn_searching = (Button)getView().findViewById(R.id.btn_searching);
btn_searching.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DataBundle("TEST Bundel");
//JSonAsyncTask jSonAsyncTask = new JSonAsyncTask();
// jSonAsyncTask.execute();
}
});
}
public void DataBundle(String test){
Bundle packageDataStation = new Bundle();
// packageDataStation.putSerializable("arrTicket", result);
packageDataStation.putString("s",test);
Fragment rFrag = new ResultFragment();
rFrag.setArguments(packageDataStation);
//noinspection ResourceType
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, rFrag);
fragmentTransaction.commit();
// set the toolbar title
Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);//get Toolbar from SearchingTicketActivity
((SearchingTicketActivity)getActivity()).setSupportActionBar(toolbar);
((SearchingTicketActivity)getActivity()).getSupportActionBar().setTitle(R.string.title_result);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_search, container, false);
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
Here is ResultFargment:
public class ResultFragment extends Fragment {
private ArrayList<TicketInforModel> arrayListResultTickets;
private ArrayAdapter<TicketInforModel>arrayAdapterTicket;
private ListView lv_resultTicket;
public ResultFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try{
lv_resultTicket = (ListView)getView().findViewById(R.id.lv_ticket);
TextView tv = (TextView)getView().findViewById(R.id.tv_test);
tv.setText(getArguments().getString("s").toString());
//arrayListResultTickets = (ArrayList<TicketInforModel>) b.getSerializable("arrTicket");
//arrayAdapterTicket = new viewListTicket();
//lv_resultTicket.setAdapter(arrayAdapterTicket);
}catch (Exception ex){
ex.getMessage();
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_result, container, false);
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
@Override
public void onDetach() {
super.onDetach();
}
}
After I click Search button on SearchFragment, the ResultFragment is showed but the value of Textview not change. What's wrong in my code ?.
Second problem: In ResultFragment, If i don't put this code below in try catch block, the Logcat will show Nullpointer Exception.
lv_resultTicket = (ListView)getView().findViewById(R.id.lv_ticket);
TextView tv = (TextView)getView().findViewById(R.id.tv_test);
tv.setText(getArguments().getString("s").toString());
This is Logcat:
11-16 00:15:32.982 7314-7314/com.example.vuong.final_project_pott E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException: println needs a message
at android.util.Log.println_native(Native Method)
at android.util.Log.d(Log.java:138)
at com.example.vuong.final_project_pott.view.ResultFragment.onCreate(ResultFragment.java:45)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1766)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:917)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
at android.os.Handler.handleCallback(Handler.java:730)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:150)
at android.app.ActivityThread.main(ActivityThread.java:5390)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
If i block the code on try catch block, it works but can get data from SearchFragment. I tried searching on Google, and Starkoverflow and follow some solution, but can resolve. Please help me. p/s: thanks you for reading my question and so sorry if my question is stupid.
My problem is resolved. I Override wrong method. I should override method onActivityCreated instead of method onCreate.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.