android - fragment 不显示任何内容

标签 android listview android-fragments

这是包含 ListView 的 fragment 代码。但它没有显示任何内容。此 fragment 与选项卡相关。此选项卡 View 包含三个 fragment ,此 fragment 不显示任何内容。为什么? 我添加了该 fragment 的代码以及相关的 xml 和其他类代码。

public class MoviesFragment extends Fragment {

String[] numbers_text = new String[] { "one", "two", "three", "four",
        "five", "six", "seven", "eight", "nine", "ten", "eleven",
        "twelve", "thirteen", "fourteen", "fifteen" };
String[] numbers_digits = new String[] { "1", "2", "3", "4", "5", "6", "7",
        "8", "9", "10", "11", "12", "13", "14", "15" };




@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);



}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {



    View view = inflater.inflate(R.layout.fragment_movies, container, false);

    MainCustomList adapter = new MainCustomList(this.getActivity(), numbers_text, numbers_digits);
         ListView list;
         list=(ListView) view.findViewById(R.id.list);
         list.setAdapter(adapter);
         return getView();
}
}

fragment 电影:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#17df0d"
android:orientation="vertical" >





<ListView
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true" >

</ListView>

</RelativeLayout>

主要列表:

     public class MainCustomList extends ArrayAdapter<String>{

 private final Activity context;
 private final String[] str1;
 private final String[] str2;
 public MainCustomList(Activity context,String[] str1, String[] str2) {
 super(context, R.layout.maintemp, str1);
 this.context = context;
 this.str1 = str1;
 this.str2 = str2;

      }
 @Override
 public View getView(int position, View view, ViewGroup parent) {
 LayoutInflater inflater = context.getLayoutInflater();
 View rowView= inflater.inflate(R.layout.maintemp, null, true);
 TextView txtTitle = (TextView) rowView.findViewById(R.id.textView1);
 TextView txtTitle2 = (TextView) rowView.findViewById(R.id.textView2);

  txtTitle.setText(str1[position]);
  txtTitle2.setText(str2[position]);

 return rowView;
}}

维护

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" android:visibility="visible">

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="fill_parent"
    android:layout_height="60dip"
    android:layout_margin="5dip"
    android:background="@drawable/shape1"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" android:padding="5dip" android:gravity="center" android:textSize="18dip" android:textColor="#ffffff"/>

最佳答案

return getView(); 是错误的。 Fragment getView返回onCreateView的结果,在onCreateView返回之前为null。因此,您将返回 null。改变

return getView();

return view;

您膨胀的 View ,其中包含您要显示的内容

关于android - fragment 不显示任何内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33443237/

相关文章:

android - 带有 fragment 的操作栏向上导航

android - 服务连接泄漏

java - 如何将按钮引用 Activity 传递给适配器android

项目之间有空格的Android自定义 ListView

android - 快速 fragment 切换 View 绑定(bind) null

java - 如何判断传递的 "Fragment"对象的类型

android - 连接到服务协议(protocol) : No Flutter view is available 时出错

android - 如何让 Rails 告诉移动应用程序用户是否登录?

android - 如何使用选择器设置初始 ListView 项目背景颜色

android - 在 Android ListView 中滑动时显示删除按钮