简体   繁体   English

如何为片段设置listadapter

[英]how to set listadapter for fragment

I have two framelayout in my main.xml file. 我的main.xml文件中有两个framelayout。 I add framelayouts to the class that extends Fragment. 我将framelayouts添加到扩展Fragment的类中。
my main class extends FragmentActivity and this is Oncreate method of it: 我的主类扩展了FragmentActivity,这是它的Oncreate方法:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    FragmentManager fm =getFragmentManager();
    FragmentTransaction ft=fm.beginTransaction();
    Fragment f=new Freg1();
    Fragment f2=new Freg1();
    ft.add(R.id.frame1, f);
    ft.add(R.id.frame2, f2);
    ft.commit();
    tf=Typeface.createFromAsset(this.getAssets(),"font/Byekan.ttf" );
    tv1=(TextView) findViewById(R.id.textView1);
    tv1.setTypeface(tf);
    Log.i(TAG,"1");
    lv1=(ListView) findViewById(R.id.listView1);
    lv2=(ListView) findViewById(R.id.listView2);
    Log.i(TAG,"2");
    List<String> stringList = new ArrayList<String>(Arrays.asList(s1));
    Log.i(TAG,"3");
    ListAdapter listAdapter = new CustomListAdapter(MainActivity.this , R.layout.custom_list ,stringList);
    Log.i(TAG,"4");
    lv1.setAdapter(listAdapter);
    Log.i(TAG,"5");
    lv2.setAdapter(listAdapter);
    Log.i(TAG,"6");


}

when i run the codes, it crashed after LOG no4. 当我运行代码时,它在LOG No4之后崩溃。 that mean setAdapter() method do not work. 这意味着setAdapter()方法不起作用。 how can i resolve this problem? 我该如何解决这个问题?

this is my logcat resource: 这是我的logcat资源:

Shutting down VM
threadid=1: thread exiting with uncaught exception (group=0x40a13300)
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.taxitabriz/com.example.taxitabriz.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
        at android.app.ActivityThread.access$600(ActivityThread.java:130)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)
    Caused by: java.lang.NullPointerException
        at com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)
        at android.app.Activity.performCreate(Activity.java:5008)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
        ... 11 more

thank for you that help me to resolve problem. 感谢您帮助我解决问题。

Your ListView lv1 is null as you can see here: 您的ListView lv1为null,如下所示:

java.lang.RuntimeException: Unable to start activity ComponentInfo{...}: java.lang.NullPointerException
....
com.example.taxitabriz.MainActivity.onCreate(MainActivity.java:55)

The line 55 of MainActivity should be this call: lv1.setAdapter(listAdapter); MainActivity的第55行应为以下调用: lv1.setAdapter(listAdapter);

Make sure that your listView1 is included within the layout and initialized correctly prior to trying to set an Adapter to it. 在尝试为其设置适配器之前,请确保listView1包含在布局中并正确初始化。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM