android嵌套片段无法正常工作

时间:2016-01-26 16:50:16

标签: android nested fragment child-fragment

我试图在按钮点击时在我的片段中显示一个嵌套片段,所以在我的主要片段的按钮.setOnClickListener(可以正常使用Log.d)中放置:

    Fragment noteFragment = new FrontPageNote();
    FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
    transaction.add(R.id.frontNote, noteFragment).commit();

在开发人员指南中找到,其中 FrontPageNote 是我的嵌套片段(显然扩展了Fragment),而R.id。 frontNote 是xml布局的id:< / p>

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingBottom="48dp"
    android:paddingLeft="10dp"
    android:paddingRight="10dp"
    android:paddingTop="10dp"
    android:layout_weight="1"
    android:id="@+id/frontNote"
    android:background="@color/Background">

    <LinearLayout
        android:id="@+id/linearLayoutFront"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="5dp"
        android:paddingTop="15dp">

        <EditText
            android:id="@+id/titleEditFront"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/titleEdit"
            android:textColor="@color/TextColor.White"/>

        <EditText
            android:id="@+id/noteEditFront"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:hint="@string/noteEdit"
            android:paddingTop="20dp"
            android:textColor="@color/TextColor.White"
            android:inputType="textMultiLine" />
    </LinearLayout>

</RelativeLayout>

嵌套片段FrontPageNote也使用onCreate()方法下的getActivity().setContentView(R.layout.front_note);链接到此xml。

问。我做错了什么?感谢

编辑:添加了FrontPageNote.class:

public class FrontPageNote extends Fragment {

// Declare Variables
public static long rowID;
private EditText title_edit;
private EditText note_edit;
private static final String TITLE = "title";
private static final String NOTE = "note";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
    getActivity().setContentView(R.layout.front_note);

    // Locate the EditText in add_note.xml
    title_edit = (EditText) getActivity().findViewById(R.id.titleEditFront);
    note_edit = (EditText) getActivity().findViewById(R.id.noteEditFront);

    // Retrieve the Row ID from ViewNote.java
    Bundle extras = getActivity().getIntent().getExtras();
    if (extras != null) {
        rowID = extras.getLong("row_id");
        title_edit.setText(extras.getString(TITLE));
        note_edit.setText(extras.getString(NOTE));
    }
}

// Create an ActionBar menu
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    inflater.inflate(R.menu.menu_all_geofences, menu);
    menu.add("Save changes")
            .setOnMenuItemClickListener(this.SaveButtonClickListener)
            .setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    super.onCreateOptionsMenu(menu, inflater);
}

// Capture save menu item click
OnMenuItemClickListener SaveButtonClickListener = new OnMenuItemClickListener() {
    public boolean onMenuItemClick(MenuItem item) {

        // Passes the data into saveNote() function
        if (title_edit.getText().length() != 0) {
            AsyncTask<Object, Object, Object> saveNoteAsyncTask = new AsyncTask<Object, Object, Object>() {
                @Override
                protected Object doInBackground(Object... params) {
                    saveNote();
                    return null;
                }

                @Override
                protected void onPostExecute(Object result) {
                    // Close this activity
                    //getActivity().finish();
                }
            };
            // Execute the saveNoteAsyncTask AsyncTask above
            saveNoteAsyncTask.execute((Object[]) null);
        }

        else {
            // Display a simple alert dialog that forces user to put in a title
            AlertDialog.Builder alert = new AlertDialog.Builder(
                    getActivity());
            alert.setTitle("Title is required");
            alert.setMessage("Put in a title for this note");
            alert.setPositiveButton("Okay", null);
            alert.show();
        }

        return false;

    }
};
// saveNote() function
private void saveNote() {
    DatabaseConnector dbConnector = new DatabaseConnector(getActivity());

    if (getActivity().getIntent().getExtras() == null) {
        // Passes the data to InsertNote in DatabaseConnector.java
        dbConnector.InsertNote(title_edit.getText().toString(), note_edit
                .getText().toString());
    } else {
        // Passes the Row ID and data to UpdateNote in DatabaseConnector.java
        dbConnector.UpdateNote(rowID, title_edit.getText().toString(),
                note_edit.getText().toString());
    }
}

}

0 个答案:

没有答案