我已经设置了3个滑动视图选项卡/屏幕,其中一个我想显示自定义ListView。出于某种原因,当我到达该特定标签时,应用程序崩溃了。我的代码如下:
swipeview的片段
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代码()---我的类来设置自定义列表视图
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$3.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
答案 0 :(得分:3)
1。在onCreateView
的{{1}}中,替换
Onstage
与
lv=(ListView) getView().findViewById(R.id.onstagelist);
原因是在lv=(ListView) rootView.findViewById(R.id.onstagelist);
返回之前无法调用getView()
,因为onCreateView()
返回的View
首先由getView()
生成。
2。您发布的第二个错误正在发生,因为您的onCreateView()
引用为空。你的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
来获取它。)
3。您可以getActivity()
成为Onstage
,它会有效。
答案 1 :(得分:1)
ListFragment
扩展Fragment
,是的,您可以在任何可以使用Fragment
的地方使用它。你的崩溃在这里:
lv=(ListView) getView().findViewById(R.id.onstagelist);
您正在getView()
内拨打onCreateView()
- 但您还没有返回View
,因此getView()
没有onViewCreated()
。将该代码移至lv = (ListView) rootView.findViewById(R.id.onstagelist);
或将其更改为在您的虚增视图中查看,即:
{{1}}