我正在编写一个简单的Activity
内容,它应该检索并显示Fragment
包含 a 的一些数据ListView
。问题是我无法访问,ListView
因为on*
似乎没有调用任何方法。
该片段有一个LinearLayout
由onCreateView
. Activity
创建Fragment
in并使用返回的 by将onCreate
其添加到其布局中(附带说明:我使用的是 support-v4 库)。FragmentManager
getSupportFragmentManager
在//方法中添加一些Log.d
调用清楚地表明它们根本没有被调用。onAttach
onCreate
onCreateView
我究竟做错了什么?
活动:
public class CharactersActivity extends FragmentActivity {
private MainCharactersFragment mainFragment = null;
private String[] charnames = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// R.layout.characters contains only a LinearLayout with id characters_fragment_container
setContentView(R.layout.characters);
if (mainFragment == null) {
mainFragment = new MainCharactersFragment();
//Add the fragment to the activity's layout.
//Taken from Google's tutorials.
getSupportFragmentManager().beginTransaction()
.add(R.id.characters_fragment_container, mainFragment).commit();
//I'd expect onCreateView to be called during this commit, but it is not called.
}
//Loads some data to display
Intent intent = getIntent();
String[] filenames = intent.getStringArrayExtra(PortableDnDActivity.CHARACTERS_FILENAMES);
this.charnames = new String[filenames.length];
for (int i=0; i < filenames.length; i++) {
this.charnames[i] = getCharacterName(filenames[i]);
}
}
@Override
public void onStart() {
// Set the ListAdapter to show the data.
Log.d("MyLog", " HERE "); // Shown by logs
ListView view = (ListView) findViewById(R.id.main_characters_view); // This is null
Log.d("MyLog", "The view is null:" + (view == null));
mainFragment.setCharsAdapter(this, this.charnames);
Log.d("MyLog", " HERE 2222"); //NOT shown by the logs
}
活动布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/characters_fragment_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</LinearLayout>
片段:
public class MainCharactersFragment extends Fragment {
public ListView charsList;
public Button newCharButton;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// The following is never shown in the logs
Log.d("MyLog", " HERE ");
View view = inflater.inflate(R.layout.main_characters_fragment, container, false);
return view;
}
public void setCharsAdapter(Context ctx, String[] data) {
// Raise NullPointerException because at this moment
// findViewById(R.id.main_characters_view) returns null
charsList.setAdapter(new ArrayAdapter<String>(ctx, R.id.main_characters_view, data));
}
@Override
public void onAttach(Activity activity) {
Log.d("MyLog", " onAttach called "); //Never shown
}
}
片段布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView android:id="@+id/main_characters_view"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
/>
<Button android:id="@+id/new_character_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/new_character"
/>
</LinearLayout>