无法在Android片段中加载简单ListView

时间:2014-03-01 11:54:02

标签: android android-listview android-fragments actionbarsherlock android-listfragment

我有一个片段包含列表视图,按钮和图像。按钮,图像和其他窗口小部件正确加载但不显示列表视图它只显示加载图标

公共类ListDetails扩展了SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        // TODO Auto-generated method stub
         ListView listView = (ListView)container.findViewById(R.id.list_view2);

            // Defined Array values to show in ListView
            String[] values = new String[] { "Android List View", 
                                             "Adapter implementation",
                                             "Simple List View In Android",
                                             "Create List View Android", 
                                             "Android Example", 
                                             "List View Source Code", 
                                             "List View Array Adapter", 
                                             "Android Example List View" 
                                            };

            // Define a new Adapter
            // First parameter - Context
            // Second parameter - Layout for the row
            // Third parameter - ID of the TextView to which the data is written
            // Forth - the Array of data

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);


            // Assign adapter to ListView
            listView.setAdapter(adapter); 
        return inflater.inflate(
                R.layout.listdetails_fragement, container, false);
    }

我已检查其他按钮,图片已加载,但列表视图在我的ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);enter image description here上显示错误

这里我提供了我的logcat错误。

3 个答案:

答案 0 :(得分:3)

这就是我的所作所为:

我没有在onCreateView()中做任何事情,而是使用onActivityCreated()。

在onActivityCreated()中,我刚刚添加了这段代码:以下是实现:

public class Database extends Fragment {

    public ViewPager viewPager;
    private AllPagesAdapter mAdapter;
    private ActionBar actionBar;
    private String[] tabs = { "Tab1", "Tab2" };
    String[] values = new String[] { "Android List View", 
            "Adapter implementation",
            "Simple List View In Android",
            "Create List View Android", 
            "Android Example", 
            "List View Source Code", 
            "List View Array Adapter", 
            "Android Example List View" 
           };
    ArrayAdapter<String> mArrayAdapter; 

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

        View databaseview = inflater.inflate(R.layout.database, container,
                false);



        return databaseview;

    }

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

        ListView mListView = (ListView)getActivity().findViewById(R.id.databaselist);

        mArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, android.R.id.text1, values);

        mListView.setAdapter(mArrayAdapter);

    }

}

我使用的布局是:

<?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="vertical" >

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
    </LinearLayout>

    <ListView
        android:id="@+id/databaselist"
        android:layout_width="match_parent"
        android:layout_height="254dp" >
    </ListView>

    <TextView
        android:id="@+id/newmainlist"
        android:layout_width="match_parent"
        android:layout_height="250dp"
        android:gravity="center"
        android:text="database" />

</LinearLayout>

此外,这是它的图像:

enter image description here

希望这会有所帮助.. :)

答案 1 :(得分:1)

因为这条线错误..

ListView listView = (ListView)container.findViewById(R.id.list_view2);

此处listview为空 像这样改变你的代码..

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View view = inflater.inflate(R.layout.listdetails_fragement, container,
            false);

    ListView listView = (ListView) view.findViewById(R.id.list_view2);

    // Defined Array values to show in ListView
    String[] values = new String[] { "Android List View",
            "Adapter implementation", "Simple List View In Android",
            "Create List View Android", "Android Example",
            "List View Source Code", "List View Array Adapter",
            "Android Example List View" };

    // Define a new Adapter
    // First parameter - Context
    // Second parameter - Layout for the row
    // Third parameter - ID of the TextView to which the data is written
    // Forth - the Array of data

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity()
            .getApplicationContext(), android.R.layout.simple_list_item_1,
            android.R.id.text1, values);

    // Assign adapter to ListView
    listView.setAdapter(adapter);
    return view;
}

答案 2 :(得分:0)

使用此

更改onCreateView(....)
 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
     View v=inflater.inflate(
            R.layout.listdetails_fragement, container, false);

     ListView listView = (ListView)v.findViewById(R.id.list_view2);

        // Defined Array values to show in ListView
        String[] values = new String[] { "Android List View", 
                                         "Adapter implementation",
                                         "Simple List View In Android",
                                         "Create List View Android", 
                                         "Android Example", 
                                         "List View Source Code", 
                                         "List View Array Adapter", 
                                         "Android Example List View" 
                                        };


        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getApplicationContext(),android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter); 
    return v;
}