如何实现Android Fragment Transaction .add语法错误

时间:2013-05-24 15:05:41

标签: android fragment android-fragmentactivity

我想让我的Android应用程序使用Fragment Transactions,这样我就可以 在各种片段之间切换显示其关联列表。我的应用程序 在尝试转换为Fragment Transactions之前工作正常。在我最初的activity_main.xml中, 我删除了android:name =“com.birdsall.peter.chap.ChapterFragment”,这是我的理解你不能使用xml定义的片段 片段交易。

1)我似乎无法获得.add() getSupportFragmentManager使用它的参数更正,即使是来自工作示例的代码 我也试图使用较新版本的FragmentTransactions无济于事 我甚至使用getSupportFragmentManager / FragmentTransactions获取了一个代码的工作示例, 修改为使用我的名字,它的工作原理。然后我将该代码导入我的应用程序 它在.add()语法上失败了。我对Android开发有些新意,不能 找出我出错的地方。

这是我对FrameLayout的主要xml布局

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

 <FrameLayout
    android:id="@+id/fragment_container"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="1" />

 <fragment
      android:id="@+id/chapterfragments"

      android:layout_width="match_parent"
      android:layout_height="match_parent" />

 <!-- Removed this from above <fragment> block to enable Fragment Transaction

 android:name="com.birdsall.peter.chap.ChapterFragment"  -->              
 </LinearLayout>

这是ChapterFragment ListView的

chapterfragment.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ListView android:id="@+id/chapterlist" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_alignParentLeft="true" />

</RelativeLayout>

以下是列表详细信息的布局 chapter_info.xml

<?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="wrap_content"
android:orientation="vertical" android:padding="6dip">


<TextView android:id="@+id/chapter1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView android:id="@+id/textViewLiteral" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/chapter1" android:text=" - "
android:textAppearance="?android:attr/textAppearanceSmall" />

<TextView android:id="@+id/chaptertitle1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/textViewLiteral" 
android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

这是我修改过的 MainActivity.java 。我离开了'setContentView(R.layout.activity_main);'因为我认为我需要创建一个视图(即使它是空的)来将片段视图添加到。

package com.birdsall.peter.chap;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;

public class MainActivity extends FragmentActivity implements       ChapterFragment.ChapterSelectedListener {



private static final String TAG = "Main_Activity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(TAG, "Starting ...");

setContentView(R.layout.activity_main);


if (findViewById(R.id.fragment_container) != null) {
  if (savedInstanceState != null) {
      return;
  }

  // Create an instance of ExampleFragment
  ChapterFragment firstFragment = new ChapterFragment();


  // Add the fragment to the 'fragment_container' FrameLayout
  getSupportFragmentManager().beginTransaction()
          **.add**(R.id.fragment_container, firstFragment).commit(); 


         Log.i(TAG, "Ending ..."); 
  }

}   ...

这是我的ChapterFragment.java

package com.birdsall.peter.chap;

import android.app.Activity;
import android.app.Fragment;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

public class ChapterFragment extends Fragment implements     LoaderManager.LoaderCallbacks<Cursor> {
ChapterSelectedListener mCallback;
 // Container Activity must implement this interface
public interface ChapterSelectedListener {
    public void onChapterSelected(String position, int rowId);
}

 public SimpleCursorAdapter dataAdapter;
 private static final String TAG = "ChapterFragment";


 @Override
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState ) {
     View listview = inflater.inflate(R.layout.activity_main,null);
     ListView mList =(ListView)listview.findViewById(R.id.chapterlist);
      // The desired columns to be bound
      String[] columns = new String[] {
        TDAdb.COL_CHAPTER,
        TDAdb.COL_CHAPTERTITLE};


      // the XML defined views which the data will be bound to
      int[] to = new int[] { 
        R.id.chapter1,
        R.id.chaptertitle1,

      };

      // create an adapter from the SimpleCursorAdapter
      dataAdapter = new SimpleCursorAdapter(
        getActivity(), 
        R.layout.chapter_info, 
        null, 
        columns, 
        to,
        0);
      mList.setAdapter(dataAdapter);
      //Ensures a loader is initialized and active.
      getLoaderManager().initLoader(0, null, this);
     return listview;

 }
 @Override
 public void onStart() {
  super.onStart();
  Log.i(TAG, " onStart");
  displayListView(); 
  Log.i(TAG, " end of onStart");
 } 
    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);

        // This makes sure that the container activity has implemented
        // the callback interface. If not, it throws an exception
        try {
            mCallback = (ChapterSelectedListener) activity;
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString()
                    + " must implement ChapterSelectedListener");
        }
    }

 @Override
public void onResume() {
  super.onResume();
  //Starts a new or restarts an existing Loader in this manager
  Log.i(TAG, " onResume");
  getLoaderManager().restartLoader(0, null, this);
 }

 private void displayListView() {

     Log.i(TAG, " Starting displayListView");
  // The desired columns to be bound
  String[] columns = new String[] {
    TDAdb.COL_CHAPTER,
    TDAdb.COL_CHAPTERTITLE};


  // the XML defined views which the data will be bound to
  int[] to = new int[] { 
    R.id.chapter1,
    R.id.chaptertitle1,

  };

  // create an adapter from the SimpleCursorAdapter
  dataAdapter = new SimpleCursorAdapter(
    getActivity(), 
    R.layout.chapter_info, 
    null, 
    columns, 
    to,
    0);

  // get reference to the ListView
  ListView listView = (ListView) getView().findViewById(R.id.chapterlist);
  // Assign adapter to ListView
  listView.setAdapter(dataAdapter);
  //Ensures a loader is initialized and active.
  getLoaderManager().initLoader(0, null, this);


  listView.setOnItemClickListener(new OnItemClickListener() {
   @Override
   public void onItemClick(AdapterView<?> listView, View view, 
     int position, long id) {


    // Get the cursor, positioned to the corresponding row in the result set
    Cursor cursor = (Cursor) listView.getItemAtPosition(position);

    String chaptervalueselected = 
    cursor.getString(cursor.getColumnIndexOrThrow(TDAdb.COL_CHAPTER));

       mCallback.onChapterSelected(chaptervalueselected, position);
        Toast.makeText(getActivity(), "Chapter " + chaptervalueselected,     Toast.LENGTH_SHORT).show();

    // starts a new Intent to update/delete a Chapter
    // pass in row Id to create the Content URI for a single row
    //Intent chapterEdit = new Intent(getBaseContext(), ChapterEdit.class);
    //Bundle bundle = new Bundle();
    //bundle.putString("mode", "update");
    //bundle.putString("rowId", rowId);
    //chapterEdit.putExtras(bundle);
    //startActivity(chapterEdit);

   }
  });

 }

 // This is called when a new Loader needs to be created.
 @Override
 public Loader<Cursor> onCreateLoader(int id, Bundle args) {
     Log.i(TAG, " onCreateLoader");
  String[] projection = { 
    TDAdb.KEY_ROWID,    
    TDAdb.COL_CHAPTER, 
    TDAdb.COL_CHAPTERTITLE}; 

  CursorLoader cursorLoader = new CursorLoader(getActivity(),
  TDAProvider.CONTENT_URI, projection, null, null, null);
  return cursorLoader;
 }

 @Override
 public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
     Log.i(TAG, " ononLoadFinished");
  // Swap the new cursor in.  (The framework will take care of closing the
        // old cursor once we return.)
        dataAdapter.swapCursor(data);
 }

 @Override
 public void onLoaderReset(Loader<Cursor> loader) {
  // This is called when the last Cursor provided to onLoadFinished()
  // above is about to be closed.  We need to make sure we are no
  // longer using it.
     Log.i(TAG, " onLoaderReset");
  dataAdapter.swapCursor(null);
 } 


}

2 个答案:

答案 0 :(得分:4)

如果您已使用add FragmentTransaction方法,则不得在布局中添加<fragment标记。如果您刚刚离开主活动XML,那该怎么办:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/fragment_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

答案 1 :(得分:1)

在chapterFragment.java中更改导入

import android.app.Fragment

import android.support.v4.app.Fragment