我是Android的新手,当我使用片段跟随谷歌上的一些教程时,我遇到了一些问题。 第一个问题:我尝试简单的发送数据有类型是字符串形式片段到其他片段,但它不起作用。 这是我的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();
}
这是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();
}
}
单击SearchFragment上的“搜索”按钮后,将显示ResultFragment,但Textview的值不会更改。我的代码出了什么问题?。
第二个问题:在ResultFragment中,如果我没有将此代码放在try catch块中,则Logcat将显示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());
这是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)
如果我阻止try catch块上的代码,它可以工作,但可以从SearchFragment获取数据。我尝试在Google和Starkoverflow上搜索并遵循一些解决方案,但可以解决。请帮我。 p / s:感谢您阅读我的问题,很抱歉,如果我的问题很愚蠢。
答案 0 :(得分:0)
我的问题已经解决了。我覆盖错误的方法。我应该覆盖方法onActivityCreated而不是onCreate方法。