Android构造函数未定义

Android构造函数未定义,android,android-listview,android-fragments,android-arrayadapter,Android,Android Listview,Android Fragments,Android Arrayadapter,我尝试在ViewPager中使用ListView,但无法创建适配器。它基本上崩溃了。我有以下代码(由eclipse的模板组成): 使用getActivity() 编辑 你有这个 <ListView android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:color/white" android:id="@+id

我尝试在ViewPager中使用ListView,但无法创建适配器。它基本上崩溃了。我有以下代码(由eclipse的模板组成):

使用
getActivity()

编辑

你有这个

<ListView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/weekly_list" // id is weekly_list
    android:padding="20dp" />
Id不匹配

应该是

ListView lv = (ListView) rootView.findViewById(R.id.weekly_list);
还要确保使用arrayadapter的活动上下文。

使用
getActivity()

编辑

你有这个

<ListView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/weekly_list" // id is weekly_list
    android:padding="20dp" />
Id不匹配

应该是

ListView lv = (ListView) rootView.findViewById(R.id.weekly_list);

另外,请确保将活动上下文用于arrayadapter。

您传递的上下文不正确。在片段中,您需要传递视图的上下文。因此,您需要编写“rootView.getContext()”而不是“this

您传递了一个不正确的上下文。在片段中,您需要传递视图的上下文。因此,您需要编写“rootView.getContext()”而不是“this

当我尝试运行应用程序时,应用程序会崩溃发布堆栈跟踪,但它不会崩溃,并发布完整的片段代码。@user2733845发布堆栈跟踪pls@user2733845我来看看。为什么会有额外的细节。只发布代码的相关部分。什么是第137行
WeeklyActivity.java
@user2733845也发布
fragment\u weekly\u dummy
您的应用程序崩溃,因为第137行的内容为空。当我试图运行它时,应用程序会崩溃发布堆栈跟踪,但它不会崩溃,并发布完整的片段代码。@user2733845发布堆栈跟踪pls@user2733845我来看看。为什么会有额外的细节。只发布代码的相关部分。什么是第137行
WeeklyActivity.java
@user2733845也发布
fragment\u weekly\u dummy
您的应用程序崩溃,因为第137行的内容为空。当我运行它时,应用程序崩溃您的视图传递不正确。你需要像这样传递它“View rootView=(RelativeLayout)inflater.inflate(R.layout.fragment\u weekly\u dummy,container,false);”如果你的布局在LinearLayout中,你需要将RelativeLayout更改为LinearLayout。当我运行它时,应用程序会崩溃。你的视图传递不正确。您需要像这样传递它:“视图根视图=(RelativeLayout)充气器。充气(R.layout.fragment\u weekly\u dummy,container,false);”如果您的布局位于LinearLayout内部,则需要将RelativeLayout更改为LinearLayout。
Return the Activity this fragment is currently associated with.
<ListView 
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@android:color/white"
    android:id="@+id/weekly_list" // id is weekly_list
    android:padding="20dp" />
ListView lv = (ListView) rootView.findViewById(R.id.time_tv); // id is not weekly_list
ListView lv = (ListView) rootView.findViewById(R.id.weekly_list);