如何实现Android片段事务。添加语法错误
我想让我的Android应用程序使用片段事务,这样我就可以 在各种片段之间切换显示它们的关联列表。我的申请 在尝试转换为片段事务之前工作正常。在我的初始活动_main.xml中, 我删除了android:name=“com.birdsall.peter.chap.ChapterFragment”,据我所知,您不能使用xml定义的片段 使用片段事务 1) 我似乎无法在其中获取.add() getSupportFragmentManager使用其参数进行更正,即使使用工作示例中的代码也是如此。如何实现Android片段事务。添加语法错误,android,fragment,android-fragmentactivity,Android,Fragment,Android Fragmentactivity,我想让我的Android应用程序使用片段事务,这样我就可以 在各种片段之间切换显示它们的关联列表。我的申请 在尝试转换为片段事务之前工作正常。在我的初始活动_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.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);
}
}
package com.birdsall.peter.chap;
导入android.app.Activity;
导入android.app.Fragment;
导入android.app.LoaderManager;
导入android.content.CursorLoader;
导入android.content.Loader;
导入android.database.Cursor;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.AdapterView;
导入android.widget.AdapterView.OnItemClickListener;
导入android.widget.ListView;
导入android.widget.SimpleCursorAdapter;
导入android.widget.Toast;
公共类ChapterFragment扩展片段实现LoaderManager.LoaderCallbacks{
第三章选择监听器McCallback;
//容器活动必须实现此接口
公共接口章节SelectedListener{
选中一章后的公共void(字符串位置,int rowId);
}
公共SimpleCursorAdapter数据适配器;
私有静态最终字符串标记=“ChapterFragment”;
@凌驾
CreateView上的公共视图(布局、充气机、视图组容器、捆绑包保存状态){
视图listview=充气机充气(R.layout.activity\u main,空);
ListView mList=(ListView)ListView.findViewById(R.id.chapterlist);
//要绑定的所需列
字符串[]列=新字符串[]{
TDAdb.COL_章,
TDAdb.COL_CHAPTERTITLE};
//数据将绑定到的XML定义的视图
int[]到=新的int[]{
R.id.1章,
R.id.chaptertitle1,
};
//从SimpleCorsOrAdapter创建适配器
dataAdapter=新的SimpleCorsorAdapter(
getActivity(),
R.布局。章节信息,
无效的
柱,
到
0);
mList.setAdapter(dataAdapter);
//确保加载程序已初始化并处于活动状态。
getLoaderManager().initLoader(0,null,this);
返回列表视图;
}
@凌驾
public void onStart(){
super.onStart();
Log.i(标记“onStart”);
displayListView();
Log.i(标记“启动结束”);
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
//这确保容器活动已实现
//回调接口。如果不是,则抛出异常
试一试{
mCallback=(ChapterSelectedListener)活动;
}catch(ClassCastException e){
抛出新的ClassCastException(activity.toString()
+“必须实现ChapterSelectedListener”);
}
}
@凌驾
恢复时公开作废(){
super.onResume();
//在此管理器中启动新加载程序或重新启动现有加载程序
Log.i(标签“onResume”);
getLoaderManager().restartLoader(0,null,this);
}
私有void displayListView(){
Log.i(标记“启动displayListView”);
//要绑定的所需列
字符串[]列=新字符串[]{
TDAdb.COL_章,
TDAdb.COL_CHAPTERTITLE};
//数据将绑定到的XML定义的视图
int[]到=新的int[]{
R.id.1章,
R.id.chaptertitle1,
};
//从SimpleCorsOrAdapter创建适配器
dataAdapter=新的SimpleCorsorAdapter(
getActivity(),
R.布局。章节信息,
无效的
柱,
到
0);
//获取对ListView的引用
ListView ListView=(ListView)getView().findViewById(R.id.chapterlist);
//将适配器分配给ListView
setAdapter(dataAdapter);
//确保加载程序已初始化并处于活动状态。
getLoaderManager().initLoader(0,null,this);
setOnItemClickListener(新的OnItemClickListener(){
@凌驾
public void onItemClick(AdapterView列表视图、视图、,
内部位置,长id){
//获取光标,定位到结果集中的对应行
游标游标=(游标)listView.getItemAtPosition(位置);
字符串chaptervalueselected=
cursor.getString(cursor.getColumnIndexOrThrow(TDAdb.COL_章));
mCallback.onChapterSelected(chaptervalueselected,position);
Toast.makeText(getActivity(),“Chapter”+chaptervalueselected,Toast.LENGTH_SHORT).show();
//开始更新/删除章节的新意图
//传入行Id以创建单行的内容URI
//Intent chapterEdit=新的Intent(getBaseContext(),chapterEdit.class);
//Bundle=新Bundle();
//bundle.putString(“模式”、“更新”);
//bundle.putString(“rowId”,rowId);
//第二章额外支出(捆绑);
//startActivity(第二章);
}
});
}
//当需要创建新的加载程序时,将调用此函数。
@凌驾
公共加载器onCreateLoader(int-id,Bundle-args){
Log.i(标记“onCreateLoader”);
字符串[]投影={
TDAdb.KEY_ROWID,
TDAdb.COL_章,
TDAdb.COL_CHAPTERTITLE};
CursorLoader CursorLoader=新的CursorLoader(getActivity(),
TDAProvider.CONTENT\U
<?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>
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 ...");
}
} ...
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);
}
}
<?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"/>
import android.app.Fragment
import android.support.v4.app.Fragment