我指的是谷歌提供的这个示例:Navigation Drawer sample
每当用户选择行星土星时,我试图在列表视图中显示土星的所有卫星。
为此我在外部MainActivity
类中更新了以下方法:
private void selectItem(int position) {
Fragment fragment;
switch (position) {
case 5: {
// update the main content by replacing fragments
fragment = new MoonsListFragment();
}
break;
default: {
fragment = new PlanetFragment();
Bundle args = new Bundle();
args.putInt(PlanetFragment.ARG_PLANET_NUMBER, position);
fragment.setArguments(args);
}
}
FragmentManager fragmentManager = getFragmentManager();
if (fragment != null){
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
// update selected item and title, then close the drawer
mDrawerList.setItemChecked(position, true);
setTitle(mPlanetTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
将其添加为嵌套类:
public static class MoonsListFragment extends Fragment {
private ListView mMoonsList;
private String[] mMoonsTitles;
public MoonsListFragment(){
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMoonsTitles = getResources().getStringArray(R.array.saturn_moons_array);
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
mMoonsList = (ListView)rootView.findViewById(R.id.moons_list); // mMoonsList value is null, though moons_list is available in R.java
// set up the moons list view with items and click listener
mMoonsList.setAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.moons_list_item, mMoonsTitles));
//mMoonsList.setOnItemClickListener(new DrawerItemClickListener());
return rootView;
}
}
以下是R.java文件的内容:
package com.example.android.navigationdrawerexample;
public final class R {
public static final class array {
public static final int planets_array=0x7f040000;
public static final int saturn_moons_array=0x7f040001;
}
public static final class attr {
}
public static final class drawable {
public static final int action_search=0x7f020000;
public static final int drawer_shadow=0x7f020001;
public static final int earth=0x7f020002;
public static final int ic_drawer=0x7f020003;
public static final int ic_launcher=0x7f020004;
public static final int jupiter=0x7f020005;
public static final int mars=0x7f020006;
public static final int mercury=0x7f020007;
public static final int neptune=0x7f020008;
public static final int saturn=0x7f020009;
public static final int uranus=0x7f02000a;
public static final int venus=0x7f02000b;
}
public static final class id {
public static final int action_websearch=0x7f070005;
public static final int content_frame=0x7f070001;
public static final int drawer_layout=0x7f070000;
public static final int image=0x7f070004;
public static final int left_drawer=0x7f070002;
public static final int moons_list=0x7f070003;
}
public static final class layout {
public static final int activity_main=0x7f030000;
public static final int drawer_list_item=0x7f030001;
public static final int fragment_moons_list=0x7f030002;
public static final int fragment_planet=0x7f030003;
public static final int moons_list_item=0x7f030004;
}
public static final class menu {
public static final int main=0x7f060000;
}
public static final class string {
public static final int action_websearch=0x7f050000;
public static final int app_name=0x7f050001;
public static final int app_not_available=0x7f050002;
public static final int drawer_close=0x7f050003;
public static final int drawer_open=0x7f050004;
}
}
fragment_planet.xml:
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center"
android:padding="32dp" />
由于某些原因,mMoonsList
将变为空,我无能为力:(
有什么想法吗?
几乎没有输入 - 我的应用程序崩溃在以下行:
mMoonsList.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.moons_list_item, mMoonsTitles));
答案 0 :(得分:3)
您需要返回rootView
而不是mMoonsList
男孩。 replese
View rootView = inflater.inflate(R.layout.fragment_planet, container, false);
表示
View rootView = inflater.inflate(R.layout.fragment_moons_list, container, false);