如何在 Fragment (Android) 中设置自定义 ListView?

How to set up a custom ListView in a Fragment (Android)?

我已经设置了 3 个滑动视图 tabs/screens,我想在其中一个视图中显示自定义 ListView。出于某种原因,当我到达该特定选项卡时,应用程序崩溃了。我的代码如下:

滑动视图的片段

public Fragment getItem(int position) {


        switch (position) {
        case 0:
            return new SOA();
        case 1:
            return new tv();
        case 2:
            return new Onstage();

        }

        return null; }

Onstage() 代码 --- 我的 class 设置自定义列表视图

public class Onstage extends Fragment {

ListView lv;
Context context;
Adapter adapter;

ArrayList prgmName;
public static String [] prgmNameList={"Arkham","Vendetta","Corleone"};
public static String [] prgmNameList2={"Arkham","Vendetta","Corleone};

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

    View rootView = inflater.inflate(R.layout.scoreboard, container, false);

    lv=(ListView) getView().findViewById(R.id.onstagelist);
    adapter=new Adapter(this, prgmNameList, prgmNameList2);
    lv.setAdapter(adapter);
    return rootView;


}}

我不能将其扩展到 ListFragment,因为 swipeview 只接受片段(我认为)

scoreboard.xml

<RelativeLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#000000"
tools:context="com.example.second.Onstage" >


<ListView
    android:id="@+id/onstagelist"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
</ListView>

</RelativeLayout>

我的自定义适配器

public class Adapter extends BaseAdapter {

String [] name;
String [] name2;
Context context;
int [] imgid;
private static LayoutInflater inf=null;

public Adapter(Onstage onstage, String[] prgmNameList, String[] prgmNameList2) {
    // TODO Auto-generated constructor stub
    name=prgmNameList;
    this.context=context;
    name2=prgmNameList2;
     inf = ( LayoutInflater )context.
             getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}



@Override
public int getCount() {
    // TODO Auto-generated method stub
    return name.length;
}


@Override
public Object getItem(int position) {
    // TODO Auto-generated method stub
    return position;
}

@Override
public long getItemId(int position) {
    // TODO Auto-generated method stub
    return position;
}


public class Holder
{
    TextView tv1,tv2;

}




@Override
public View getView(int position, View convertView, ViewGroup parent) {
    // TODO Auto-generated method stub


    Holder holder=new Holder();
    View rowview;
    rowview=inf.inflate(R.layout.scoreboard_layout, null);
    holder.tv1=(TextView) rowview.findViewById(R.id.eosname);
    holder.tv2=(TextView) rowview.findViewById(R.id.eosvenue);
  ``holder.tv1.setText(name[position]);
    holder.tv2.setText(name2[position]);
    return rowview;

}}

和scoreboard_layout.xml

<LinearLayout 
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >

 <TextView
    android:id="@+id/eosname"
    android:layout_gravity="center"
    android:fontFamily="sans-serif-light"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/eosvenue"
    android:layout_gravity="center"
    android:fontFamily="sans-serif-light"
    android:textSize="20sp"
    android:textColor="#FFFFFF"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />


</LinearLayout>

Logcat如下:

02-22 10:46:04.316: D/AndroidRuntime(17969): Shutting down VM
02-22 10:46:04.326: E/AndroidRuntime(17969): FATAL EXCEPTION: main
02-22 10:46:04.326: E/AndroidRuntime(17969): Process: com.example.second, PID: 17969
02-22 10:46:04.326: E/AndroidRuntime(17969): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
02-22 10:46:04.326: E/AndroidRuntime(17969):    at com.example.second.Adapter.<init>(Adapter.java:27)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at com.example.second.Onstage.onCreateView(Onstage.java:32)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.support.v4.view.ViewPager.run(ViewPager.java:249)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.view.Choreographer.doCallbacks(Choreographer.java:580)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.view.Choreographer.doFrame(Choreographer.java:549)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.os.Handler.handleCallback(Handler.java:739)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.os.Looper.loop(Looper.java:135)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at android.app.ActivityThread.main(ActivityThread.java:5255)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at java.lang.reflect.Method.invoke(Native Method)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at java.lang.reflect.Method.invoke(Method.java:372)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
02-22 10:46:04.326: E/AndroidRuntime(17969):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
02-22 10:46:08.991: I/Process(17969): Sending signal. PID: 17969 SIG: 9

1. 在你的 OnstageonCreateView 中,替换

lv=(ListView) getView().findViewById(R.id.onstagelist);

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

原因是getView()不能在onCreateView()returns之前调用,因为getView()返回的View是由[=17先生成的=].

2. 您发布的第二个错误是因为您的 context 引用为空。您的 Adapter 的构造函数

public Adapter(Onstage onstage, String[] prgmNameList, String[] prgmNameList2) {...}

没有 Context 参数,因此行 this.context = context 无法初始化 context 字段。将 Adapter 构造函数定义为

public Adapter(Onstage onstage, String[] prgmNameList, String[] prgmNameList2, Context context) {...}

并传递一个有效的 context 实例给它(它应该是 Activity 的一个实例,所以通过调用 getActivity() 来获取它)。

3. 你可以把 Onstage 改成 ListFragment 就可以了。

ListFragment 扩展了 Fragment,所以是的,您可以在任何可以使用 Fragment 的地方使用它。你的崩溃在这里:

lv=(ListView) getView().findViewById(R.id.onstagelist);

您正在从 onCreateView() 中调用 getView() -- 您尚未返回 View,因此 getView() 没有。将该代码移动到 onViewCreated() 或将其更改为在您的膨胀视图中查看,即:

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