ListView无法显示

时间:2015-08-02 17:48:14

标签: android android-layout android-fragments

我试图在我的应用程序启动时显示项目的列表视图。但是在应用程序启动时,没有显示列表视图,但只显示一个空白屏幕。下面是代码:

MainActivity.java

public class MainActivity extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    }


public static class PlaceholderFragment extends Fragment
{
    ArrayAdapter<String> mForecastAdapter;
    public PlaceholderFragment()
    {}

    @Override
    public View onCreateView(LayoutInflater inflator, ViewGroup v, Bundle savedInstanceState)
    {
        View rootView = inflator.inflate(R.layout.fragment_main, v);

        String[] forecastArray= {
                "Today - Sunny - 88/63",
                "Monday - foggy - 67/20",
                "Tuesday - Sunny - 88/63"
                };

        List<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));

        mForecastAdapter= new ArrayAdapter<String>(
                getActivity(),
                R.layout.list_item_forecast,
                R.id.list_item_forecast_textview,
                weekforecast);

        ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
        listview.setAdapter(mForecastAdapter);


        return rootView;
    }


}

}

activity_main.xml中

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.sunshineapp.MainActivity" 
tools:ignore="MergeRootFrame" />

fragment_main.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:paddingLeft="64dp" 
android:paddingRight="64dp"
android:paddingTop="16dp"
android:paddingBottom="16dp"
tools:context="com.example.sunshineapp.MainActivity$PlaceholderFragment" >

<ListView
    android:id="@+id/listview_forecast"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     />

list_item_forecast.xml

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical" 
android:id="@+id/list_item_forecast_textview"/>

有人可以指导我解决这个问题吗?提前谢谢!

修改 正如@Sanjeet所建议的那样,我忘记将片段附加到活动中。所以我在onCreate活动方法中将其附加如下:

if(savedInstanceState == null)
    {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction  =fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.container, new PlaceholderFragment());
    fragmentTransaction.commit();   

    }  

但是现在我在logcat中得到以下内容:

08-03 11:55:37.365: E/AndroidRuntime(20088): FATAL EXCEPTION: main
08-03 11:55:37.365: E/AndroidRuntime(20088): java.lang.RuntimeException:   Unable to start       activity ComponentInfo{com.example.sunshineapp/com.example.sunshineapp.MainActivity}:     java.lang.IllegalStateException: The specified child already has a parent. You must call     removeView() on the child's parent first.
08-03 11:55:37.365: E/AndroidRuntime(20088):    at     android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2343)
08-03 11:55:37.365: E/AndroidRuntime(20088):    at     android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2395)
08-03 11:55:37.365: E/AndroidRuntime(20088):    at android.app.ActivityThread.access$600    (ActivityThread.java:162)

申请停止......请帮忙!

5 个答案:

答案 0 :(得分:1)

对我来说,仅仅帮助更新了适配器:

ListView listview =(ListView)rootView.findViewById(R.id.listview_forecast);
        listview.setAdapter(mForecastAdapter);
        mForecastAdapter.notifyDataSetChanged();

        return rootView;

答案 1 :(得分:1)

在我看来你的mForecastAdapter数组是空的。尝试在内部添加一些值进行测试。

答案 2 :(得分:1)

尝试更改

List<String> weekforecast= new ArrayList<String (Arrays.asList(forecastArray));

ArrayList<String> weekforecast= new ArrayList<String>(Arrays.asList(forecastArray));

答案 3 :(得分:1)

尝试更改

View rootView = inflator.inflate(R.layout.fragment_main, v);

View rootView = inflator.inflate(R.layout.fragment_main, v,false);

有关上一个参数中false的详细信息。你应该查看answer

希望这有帮助。

答案 4 :(得分:1)

你应该使用

View rootView = inflator.inflate(R.layout.fragment_main, v,false);

而不是

View rootView = inflator.inflate(R.layout.fragment_main, v);