有人可以帮我吗?是的,我知道使用错误日志这样做是不好的做法。下面的片段不会产生异常(但是),但它根本不显示:
package com.tom.pws;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class MAF extends Fragment {
public View OnCreateView(LayoutInflater inflater,
ViewGroup container, Bundle args) {
View view = inflater.inflate(R.layout.activity_maf, container, false);
Log.e("reached AGAIN", "yay");
return view;
}
}
以下是主要活动:
package com.tom.pws;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.Menu;
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("reached", "yay");
if (savedInstanceState != null) {
return;
}
MAF maf = new MAF();
FragmentTransaction fm = getSupportFragmentManager().beginTransaction();
fm.add(R.id.lodsamone, maf);
fm.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.main, menu);
return true;
}
}
activity_maf.xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/text_body"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
activity_main.xml中:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/lodsamone"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
似乎永远无法访问OnCreateView。我在这里缺少什么?
答案 0 :(得分:0)
MAF maf = new MAF();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.lodsamone, maf).commit();
OnCreateView(...)
是否正确?
是
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)