无法添加窗口 - 令牌null不适用于ListFragment中的应用程序

时间:2015-01-20 20:42:05

标签: android

我有来自Web服务器的json数据填充的ListActivity,但我突然想尝试在我的应用程序上使用ViewPager。

似乎ListActivity和ListFragment具有几乎相同的功能。所以我所做的是将代码复制粘贴到ListFragment,我已经改变了一些代码来消除错误。

我所做的是将onCreate(ListActivity)中的所有代码放到onCreateView(ListFragment)中,如下所示:

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

    View rootView = inflater.inflate(R.layout.fragment_movies, container, false);
    examinationList = new ArrayList<HashMap<String, String>>();
    ListView lv = getListView();
    new GetContacts().execute();

    return rootView;
}

创建了一个尚未创建的Content视图异常。所以我做的是创建一个onViewCreated方法并从onCreateView中删除所有不必要的代码并将其传输到onViewCreated

@Override 
public void onViewCreated (View view, Bundle savedInstanceState) {

    examinationList = new ArrayList<HashMap<String, String>>();
    ListView lv = getListView();
    new GetContacts().execute();

}

删除了尚未创建异常的内容视图,但现在又出现了另一个异常,我无法解决。

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application

EDITED

fragment_movies.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">
    <!-- Main ListView 
         Always give id value as list(@android:id/list)
    -->
    <ListView
        android:id="@android:id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"/>

</LinearLayout>

1 个答案:

答案 0 :(得分:0)

解决它!问题出在onPreExecute上显示ProgressDialog,似乎获取正常活动的上下文与获取片段上下文不同。