Android Listview项未显示在片段中
我有一个带碎片的导航抽屉。我想打开导航抽屉上的碎片。代码中没有任何错误,但Listview项不显示在片段中。仅显示空白页。下面的代码。请帮帮我 fragment\u blank\u fragment\u trip2.xmlAndroid Listview项未显示在片段中,android,xml,listview,layout,fragment,Android,Xml,Listview,Layout,Fragment,我有一个带碎片的导航抽屉。我想打开导航抽屉上的碎片。代码中没有任何错误,但Listview项不显示在片段中。仅显示空白页。下面的代码。请帮帮我 fragment\u blank\u fragment\u trip2.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_
<LinearLayout 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:orientation="vertical"
tools:context="com.example.administrator.vtsapplication.BlankFragmentTrip2">
<ListView
android:id="@+id/mobile_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Activity_listview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
BlankFragmenttrip2.java
package com.example.administrator.vtsapplication;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class BlankFragmentTrip2 extends Fragment {
public BlankFragmentTrip2() {
// Required empty public constructor
}
ViewGroup vg;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
vg = (ViewGroup) inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);
ListView listView = (ListView) vg.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
return inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
}
}
package com.example.administrator.vtsapplication;
导入android.os.Bundle;
导入android.support.v4.app.Fragment;
导入android.view.LayoutInflater;
导入android.view.view;
导入android.view.ViewGroup;
导入android.widget.ArrayAdapter;
导入android.widget.ListView;
公共类BlankFragmentTrip2扩展了片段{
公共广播片段Trip2(){
//必需的空公共构造函数
}
视图组vg;
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
String[]mobileArray={“Android”、“IPhone”、“WindowsMobile”、“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”};
vg=(视图组)充气器。充气(R.layout.fragment\u blank\u fragment\u trip2,容器,false);
ArrayAdapter=新的ArrayAdapter(getActivity(),R.layout.activity_listview,mobileArray);
ListView ListView=(ListView)vg.findViewById(R.id.mobile\u列表);
setAdapter(适配器);
返回充气机。充气(R.layout.fragment\u blank\u fragment\u trip2,容器,假);
}
}
您应该这样使用片段。您没有返回您的视图
公共类BlankFragmentTrip2扩展了片段{
String[]mobileArray={“Android”、“IPhone”、“WindowsMobile”、“黑莓”、“WebOS”、“Ubuntu”、“Windows7”、“Max OS X”};
公共广播片段Trip2(){
//必需的空公共构造函数
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u blank\u fragment\u trip2,容器,false);
ArrayAdapter=新的ArrayAdapter(getActivity(),R.layout.activity_listview,mobileArray);
ListView ListView=(ListView)rootView.findViewById(R.id.mobile\u列表);
setAdapter(适配器);
返回rootView;
}
}
删除最后一行,我认为返回的是空视图。只需返回vg
,不要发送ViewGroup
,因为方法onCreateView()
的返回类型仅为View
,谢谢您的回复。它的工作和我的问题是通过使用你的代码解决的。。非常感谢。
public class BlankFragmentTrip2 extends Fragment {
String[] mobileArray = {"Android", "IPhone", "WindowsMobile", "Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X"};
public BlankFragmentTrip2() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_blank_fragment_trip2, container, false);
ArrayAdapter adapter = new ArrayAdapter<String>(getActivity(), R.layout.activity_listview, mobileArray);
ListView listView = (ListView) rootView.findViewById(R.id.mobile_list);
listView.setAdapter(adapter);
return rootView;
}
}