我有一个包含4个片段的slidinglayout,其中一个片段应该在侧面使用listview但是当我运行我的应用程序时崩溃,所以这里是我的代码。
news.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
</RelativeLayout>
这是我的news.java
public class News extends Fragment {
ListView myListView;
String[] news={"first","Secound","Third"};
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View rootView= inflater.inflate(R.layout.news, container, false);
myListView = (ListView) rootView.findViewById(R.id.listview);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_list_item_1, news);
myListView.setAdapter(adapter);
return rootView;
}
这里是slidinglayout片段管理器
class MyAdapter extends FragmentPagerAdapter{
public MyAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
@Override
public Fragment getItem(int position) {
// TODO Auto-generated method stub
Fragment fragment=null;
if(position==0){
fragment=new Generalization();
}
if(position==1){
fragment=new Report();
}
if(position==2){
fragment=new Templet();
}
if(position==3){
fragment=new News();
}
return fragment;
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return 4;
}
@Override
public CharSequence getPageTitle(int position){
String title="";
if(position==0){
title=" تعميم ";
}
if(position==1){
title=" نماذج ";
}
if(position==2){
title=" تقارير ";
}
if(position==3){
title=" أخبار ";
}
return title;
}
}
请告诉我错误在哪里,
这里是logcat ,,
02-05 12:38:24.361: E/AndroidRuntime(1407): at com.uigtc.kasd.SlidingTabLayout$TabClickListener.onClick(SlidingTabLayout.java:315)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.view.View.performClick(View.java:4438)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.view.View$PerformClick.run(View.java:18422)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.os.Handler.handleCallback(Handler.java:733)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.os.Handler.dispatchMessage(Handler.java:95)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.os.Looper.loop(Looper.java:136)
02-05 12:38:24.361: E/AndroidRuntime(1407): at android.app.ActivityThread.main(ActivityThread.java:5001)
02-05 12:38:24.361: E/AndroidRuntime(1407): at java.lang.reflect.Method.invokeNative(Native Method)
02-05 12:38:24.361: E/AndroidRuntime(1407): at java.lang.reflect.Method.invoke(Method.java:515)
02-05 12:38:24.361: E/AndroidRuntime(1407): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
02-05 12:38:24.361: E/AndroidRuntime(1407): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
02-05 12:38:24.361: E/AndroidRuntime(1407): at dalvik.system.NativeStart.main(Native Method)
02-05 12:38:26.353: D/dalvikvm(1532): GC_FOR_ALLOC freed 61K, 4% free 3772K/3928K, paused 3ms, total 3ms
02-05 12:38:26.353: I/dalvikvm-heap(1532): Grow heap (frag case) to 4.376MB for 637572-byte allocation
02-05 12:38:26.389: D/dalvikvm(1532): GC_FOR_ALLOC freed 2K, 4% free 4392K/4552K, paused 8ms, total 8ms
02-05 12:38:26.413: D/dalvikvm(1532): GC_FOR_ALLOC freed 6K, 3% free 4853K/4992K, paused 3ms, total 3ms
02-05 12:38:26.421: D/dalvikvm(1532): GC_FOR_ALLOC freed 6K, 3% free 4921K/5056K, paused 5ms, total 5ms
02-05 12:38:26.429: I/dalvikvm-heap(1532): Grow heap (frag case) to 8.890MB for 4194316-byte allocation
02-05 12:38:26.437: D/dalvikvm(1532): GC_FOR_ALLOC freed <1K, 2% free 9017K/9156K, paused 10ms, total 10ms
02-05 12:38:26.549: D/libEGL(1532): loaded /system/lib/egl/libEGL_genymotion.so
02-05 12:38:26.553: D/(1532): HostConnection::get() New Host Connection established 0xb860fbf8, tid 1532
02-05 12:38:26.573: D/libEGL(1532): loaded /system/lib/egl/libGLESv1_CM_genymotion.so
02-05 12:38:26.577: D/libEGL(1532): loaded /system/lib/egl/libGLESv2_genymotion.so
02-05 12:38:26.621: W/EGL_genymotion(1532): eglSurfaceAttrib not implemented
02-05 12:38:26.625: E/OpenGLRenderer(1532): Getting MAX_TEXTURE_SIZE from GradienCache
02-05 12:38:26.625: E/OpenGLRenderer(1532): MAX_TEXTURE_SIZE: 8192
02-05 12:38:26.637: E/OpenGLRenderer(1532): Getting MAX_TEXTURE_SIZE from Caches::initConstraints()
02-05 12:38:26.641: E/OpenGLRenderer(1532): MAX_TEXTURE_SIZE: 8192
02-05 12:38:26.641: D/OpenGLRenderer(1532): Enabling debug mode 0
答案 0 :(得分:3)
当您在Android应用程序中使用片段时,您无法在onCreateView方法中填充其中的窗口小部件数据,因为尚未将视图放置在屏幕上(需要返回)。 因此,在片段生命周期中,首先使用onCreateView放置布局,然后在onCreate方法中填充小部件
所以解决方法是让你的onCreateView看起来像这样:
public class News extends Fragment {
ListView myListView;
String[] news={"first","Secound","Third"};
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
return inflater.inflate(R.layout.news , container , false);
}
并在片段的onCreate函数中填充ListView数据。 要使用View,您只需要调用
getView().findViewById(R.id.listview);....
祝你好运
答案 1 :(得分:0)
你可能遇到了&#34; getActivity&#34;有时返回&#34; null&#34;。尝试将参考文献存储在&#34; onAttach&#34;方法,然后用它来创建你的适配器:
Activity activity
@Override
void onAttach (Activity activity) {
this.activity = activity;
}
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, news);
myListView.setAdapter(adapter);
...
}
希望这有帮助