我正在学习Android中的Fragments,我遇到了Logcat的问题。
我有2个活动和2个碎片及其布局:
PlayerActivity - PlayerFragment / activity_player - fragment_player
LibraryActivity - LibraryFragment / activity_library - fragment_library
我有一个PlayerFragment到LibraryActivity的按钮,在那里我给LibraryFragment充气。问题是我在LibraryFragment时Logcat没有显示任何内容。
这是我用来调用LibraryActivity的Intent(此按钮位于onActivityCreated方法的PlayerFragment中)
final Button to_library = (Button) getView().findViewById(R.id.library);
to_library.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent newIntent = new Intent(getActivity(), LibraryActivity.class);
startActivity(newIntent);
}
});
LibraryActivity:
public class LibraryActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.library, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_library,
container, false);
return rootView;
}
}
}
LibraryFragment
public class LibraryFragment extends Fragment {
private final String LOG_TAG = "test";
public LibraryFragment() {
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
Log.v(LOG_TAG, "onAttach");
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.v(LOG_TAG, "onCreate");
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_library, container,
false);
Log.v(LOG_TAG, "onCreateViewLibraryFragment");
/*
* Aquí podemos seleccionar las Views contenidas en el Layout para
* trabajar con ellas, por ejemplo con: TipoView miView = (TipoView)
* rootView.findViewById(R.id.miViewXML);
*/
return rootView;
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.v(LOG_TAG, "onActivityCreatedLibraryFragment");
}
@Override
public void onStart() {
super.onStart();
Log.v(LOG_TAG, "onStart");
}
@Override
public void onResume() {
super.onResume();
Log.v(LOG_TAG, "onResume");
}
@Override
public void onPause() {
super.onPause();
Log.v(LOG_TAG, "onPause");
}
@Override
public void onStop() {
super.onStop();
Log.v(LOG_TAG, "onStop");
}
@Override
public void onDestroyView() {
super.onDestroyView();
Log.v(LOG_TAG, "onDestroyView");
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(LOG_TAG, "onDestroy");
}
@Override
public void onDetach() {
super.onDetach();
Log.v(LOG_TAG, "onDetach");
}
}
这是我遇到问题的地方。
Logcat不会显示消息“onActivityCreatedLibraryFragment”。我有方法onStart(),onResume(),onStop()等,他们的“Log.v”和我有同样的问题。
提前感谢您的帮助。
activity_library.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.jgutierrezgil.bmusic.LibraryActivity"
tools:ignore="MergeRootFrame" />
fragment_library.xml
<RelativeLayout 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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.jgutierrezgil.bmusic.LibraryActivity$PlaceholderFragment" >
</RelativeLayout>
编辑1 :我已经完成了LibraryFragment的代码,并添加了activity_library.xml和fragment_library.xml
答案 0 :(得分:0)
看起来您实际上从未在代码中添加LibraryFragment
。您只是添加了PlaceholderFragment
,这是您所做的教程中的剩余部分。
在onCreate()
的{{1}}中,进行以下更改:
LibraryActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
//.add(R.id.container, new PlaceholderFragment()).commit();
.add(R.id.container, new LibraryFragment()).commit();
}
}
定义在PlaceholderFragment
的底部,意为虚拟。显示正确布局的原因是因为您将输入的布局文件更改为LibraryActivity
R.layout.fragment_library
,表明它夸大了正确的PlaceholderFragment
。