- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我知道这是一个简单的问题。我也会因为这个问题而失去声誉。但我正在努力解决这个问题。我用 DrawerNavigationLayout 创建了 android 项目。但我很难将简单的 ListView 添加到布局中。
这是我的布局文件。
activity_main.xml
<!-- A DrawerLayout is intended to be used as the top-level content view using match_parent for both width and height to consume the full space available. -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout"
android:layout_width="match_parent" android:layout_height="match_parent"
tools:context=".MainActivity">
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start"
android:name="com.andromedatech.musicupv2.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
</android.support.v4.widget.DrawerLayout>
fragment_main.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"
tools:context=".MainActivity$PlaceholderFragment">
<TextView android:id="@+id/section_label" android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#04330000"
tools:context=".MainActivity" >
<ListView
android:id="@+id/song_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
这是我的MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
songView = (ListView) findViewById(R.id.song_list);
songList = new ArrayList<Song>();
getSongList();
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
SongAdapter songAdt = new SongAdapter(this, songList);
songView.setAdapter(songAdt);
}
由于 setContentView(R.layout.activity_main); 不包含 ListView,它会发出 NullPointerException。但问题是如何在不丢失NavigationDrawerLayout的情况下实现Listview。我知道这可能会与之前的一些帖子重复。但我不明白如何实现它。
我尝试将ContentView设置为fragment_main,但没有成功。
最佳答案
songView
将始终为 null,因为通过在 Activity 中执行 songView = (ListView) findViewById(R.id.song_list);
,您实际上是在查找 ListView在 activity_main.xml
的 View 层次结构中,但它不在那里。
为了解决这个问题,您需要将 ListView
的代码移动到 Fragment 的代码中。像这样的事情:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_main, container, false);
songView = (ListView) v.findViewById(R.id.song_list);
return v;
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
songList = new ArrayList<Song>();
getSongList();
Collections.sort(songList, new Comparator<Song>(){
public int compare(Song a, Song b){
return a.getTitle().compareTo(b.getTitle());
}
});
// Note that I removed 'this' (referring to the Activity) from the adapter's constructor since we are now in a Fragment.
//and replaced it with getActivity()
SongAdapter songAdt = new SongAdapter(getActivity(), songList);
songView.setAdapter(songAdt);
另一个问题是您没有将 Fragment
添加到 Activity 布局的任何位置。一种方法是在您的 Activity
的 onCreate
方法中执行类似的操作,因为您似乎已经有了一个容器:
FragmentManager fragmentManager = getSupportFragmentManager();
// This is assuming you're using classes from teh support lib, of course.
// Retrieve the appropriate FragmentManager according to the super class of your Activity
fragmentManager.beginTransaction().add(R.id.container, new MainFragment());
// MainFragment is the fragment that contains the ListView
关于java - 如何将ListView添加到fragment_main.xml,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31158023/
我是一名优秀的程序员,十分优秀!