所以早些时候我创建了一个导航工具,其中每个项目都会打开一个Fragment活动。其中一个被称为“计划”,这是我遇到问题的地方。 我想在计划活动中添加一个片段(也是一个片段)。
目的是添加日期选择器片段。当单击按钮时,应使用minicalendar视图打开dialogfragment以请求用户提供日期。此外,该日期随后存储在文本视图中。
不幸的是,当我点击按钮时没有任何反应。没有动作,也没有错误或崩溃,这让我很难调试。
这是我的代码:
称为计划的主机碎片(没有导入):
public class planning extends Fragment implements DatePickerDialog.OnDateSetListener{
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
public planning() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment planning.
*/
// TODO: Rename and change types and number of parameters
public static planning newInstance(String param1, String param2) {
planning fragment = new planning();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(Calendar.YEAR,year);
c.set(Calendar.MONTH,month);
c.set(Calendar.DAY_OF_MONTH,day);
String currentDateString = DateFormat.getDateInstance().format(c.getTime());
TextView textView = getView().findViewById(R.id.view_date);
textView.setText(currentDateString);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_planning,
container, false);
Button button = (Button)view.findViewById(R.id.b_datepicker);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
DialogFragment datePicker = new DatePickerFragment();
datePicker.show(getFragmentManager(), "date picker");
}
});
//
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_planning, container, false);
}
// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}
}
这是规划XML(片段的framelayout):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".Fragments.planning">
<!-- TODO: Update blank fragment layout -->
<FrameLayout
android:id="@+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</FrameLayout>
<Button
android:id="@+id/b_datepicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:text="open date picker" />
<TextView
android:id="@+id/view_date"
android:layout_width="50dp"
android:layout_height="50dp"
android:textColor="@color/blue"
/>
</LinearLayout>
这是DatePickerFragment,我想在点击按钮时显示:
package com.example.android.meat_timealpha10.Fragments;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment;
import java.util.Calendar;
public class DatePickerFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), (DatePickerDialog.OnDateSetListener) getActivity(), year, month, day );
}
}
我认为问题出现在我的规划片段的OnCreateView中。 我尝试了不同的东西,比如在onclicklistener
之外的AND中添加它FragmentTransaction ft = getChildFragmentManager().beginTransaction();
DatePickerFragment datepicker = new DatePickerFragment();
ft.replace(R.id.date_picker, datepicker);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.addToBackStack(null);
ft.commit();
但是没什么好看的。 我希望你能帮助我。 请记住,我在android编程方面相当新。提前谢谢!
答案 0 :(得分:0)
请勿在{{1}}内重新扩充您的布局。您的最后一行应为onCreateView
此外,使用大写(CamelCase)
启动您的班级名称return view;