在同一个类中使用多个适配器

时间:2014-09-10 06:53:18

标签: android listview baseadapter

我在片段类中使用多个Base适配器,并在按钮点击时切换它们, 问题 - > 在onCreate设置适配器1作为默认值,当将其切换为秒时,则首先调用第一个适配器然后调用第二个。

  

09-10 13:07:46.396:E / AndroidRuntime(15742):致命异常:主要   09-10 13:07:46.396:E / AndroidRuntime(15742):   java.lang.NullPointerException 09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.widget.ListView.measureScrapChild(ListView.java:1163)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.widget.ListView.measureHeightOfChildren(ListView.java:1246)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.ListView.onMeasure(ListView.java:1155)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.widget.RelativeLayout.measureChild(RelativeLayout.java:584)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.RelativeLayout.onMeasure(RelativeLayout.java:397)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:298)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.widget.RelativeLayout.measureChildHorizo​​ntal(RelativeLayout.java:599)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.RelativeLayout.onMeasure(RelativeLayout.java:381)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:298)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.widget.LinearLayout.measureVertical(LinearLayout.java:829)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.LinearLayout.onMeasure(LinearLayout.java:558)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4806)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.widget.FrameLayout.onMeasure(FrameLayout.java:298)09-10   13:07:46.396:E / AndroidRuntime(15742):at   com.android.internal.policy.impl.PhoneWindow $ DecorView.onMeasure(PhoneWindow.java:2111)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.view.View.measure(View.java:12853)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1065)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2455)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.os.Handler.dispatchMessage(Handler.java:99)09-10   13:07:46.396:E / AndroidRuntime(15742):at   android.os.Looper.loop(Looper.java:137)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   android.app.ActivityThread.main(ActivityThread.java:4424)09-10   13:07:46.396:E / AndroidRuntime(15742):at   java.lang.reflect.Method.invokeNative(Native Method)09-10   13:07:46.396:E / AndroidRuntime(15742):at   java.lang.reflect.Method.invoke(Method.java:511)09-10 13:07:46.396:   E / AndroidRuntime(15742):at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:817)   09-10 13:07:46.396:E / AndroidRuntime(15742):at   com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)09-10   13:07:46.396:E / AndroidRuntime(15742):at   dalvik.system.NativeStart.main(原生方法)

1 个答案:

答案 0 :(得分:1)

如果您想切换,则需要

 .setAdapter(null);

然后

.setAdapter(secondAdapter);

还适用于您的适配器:

public View getView(final int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        LayoutInflater mInflater = (LayoutInflater) parent.getContext().getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = mInflater.inflate(R.layout.trip_map_adapter, null);