0

我的bindView()方法中有这个错误。我之前写过很多适配器,但现在我不明白这个问题的原因。所以,我在一个屏幕上有一个视图寻呼机和一个 ListFragment。此列表通过 ResourceCursorAdapter 从内容提供者填充。编码:

public class CityFragment extends ListFragment implements LoaderCallbacks<Cursor>{

public final static int CITIES_LIST = 0;

CityAdapter adapter;

public static CityFragment newInstance() {
    CityFragment cityFragment = new CityFragment();
    return cityFragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.city_layout, container, false);
    return view;
}

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    adapter = new CityAdapter(getActivity(), 
            R.layout.city_row, 
            null, 
            CursorAdapter.FLAG_AUTO_REQUERY);
//      adapter = new SimpleCursorAdapter(getActivity().getApplicationContext(),
//              android.R.layout.simple_list_item_2,
//              null, 
//              new String[] {Schema.Cities.name,
Schema.Cities.activeCamerasCount}, 
//              new int[] {android.R.id.text1, android.R.id.text2});
    setListAdapter(adapter);

    getLoaderManager().initLoader(CITIES_LIST, null, this);
}

@Override
public Loader<Cursor> onCreateLoader(int id, Bundle arg1) {
    return new CursorLoader(getActivity(), 
            CustomContentProvider.CITIES_URI, 
            new String[] { Schema.Cities._id, Schema.Cities.name, Schema.Cities.activeCamerasCount }, 
            null, 
            null,
            null);
}

@Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
    adapter.changeCursor(cursor);
}

@Override
public void onLoaderReset(Loader<Cursor> arg0) {
    adapter.changeCursor(null);
}
}

public class CityAdapter extends ResourceCursorAdapter {

private int cityIdx;
private int cameraIdx;

public CityAdapter(Context context, int layout, Cursor c, int flags) {
    super(context, layout, c, flags);
    initColumns(c);
}

@Override
public void changeCursor(Cursor cursor) {
    super.changeCursor(cursor);
    initColumns(cursor);
}

@Override
public void bindView(View v, Context ctx, Cursor c) {
    ViewHolder holder = (ViewHolder) v.getTag();
    holder.city.setText(c.getString(cityIdx));
    holder.camera.setText(c.getString(cameraIdx));
}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
    View view = super.newView(context, cursor, parent);
//      LayoutInflater inflater = LayoutInflater.from(context);
//      View view = inflater.inflate(R.layout.city_row, parent, false);
    view.setTag(new ViewHolder(
            (TextView) view.findViewById(R.id.city), 
            (TextView) view.findViewById(R.id.cameras_count)));
    return view;
}

private void initColumns(final Cursor c) {
    if (c != null) {
        cityIdx = c.getColumnIndex(Schema.Cities.name);
        cameraIdx = c.getColumnIndex(Schema.Cities.camerasCount);
    }
}

private class ViewHolder {

    final TextView city;
    final TextView camera;

    public ViewHolder(final TextView city, final TextView camera) {
        this.city = city;
        this.camera = camera;
    }

}
}

    08-10 21:21:19.641: E/AndroidRuntime(414): java.lang.NullPointerException
08-10 21:21:19.641: E/AndroidRuntime(414):  at ru.viewcity.ui.CityAdapter.bindView(CityAdapter.java:34)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.support.v4.widget.CursorAdapter.getView(CursorAdapter.java:256)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.AbsListView.obtainView(AbsListView.java:1430)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.ListView.makeAndAddView(ListView.java:1745)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.ListView.fillDown(ListView.java:670)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.ListView.fillFromTop(ListView.java:727)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.ListView.layoutChildren(ListView.java:1598)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.AbsListView.onLayout(AbsListView.java:1260)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1141)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.widget.FrameLayout.onLayout(FrameLayout.java:338)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.View.layout(View.java:7175)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.ViewRoot.performTraversals(ViewRoot.java:1140)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.view.ViewRoot.handleMessage(ViewRoot.java:1859)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.os.Handler.dispatchMessage(Handler.java:99)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.os.Looper.loop(Looper.java:123)
08-10 21:21:19.641: E/AndroidRuntime(414):  at android.app.ActivityThread.main(ActivityThread.java:3683)
08-10 21:21:19.641: E/AndroidRuntime(414):  at java.lang.reflect.Method.invokeNative(Native Method)
08-10 21:21:19.641: E/AndroidRuntime(414):  at java.lang.reflect.Method.invoke(Method.java:507)
08-10 21:21:19.641: E/AndroidRuntime(414):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-10 21:21:19.641: E/AndroidRuntime(414):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-10 21:21:19.641: E/AndroidRuntime(414):  at dalvik.system.NativeStart.main(Native Method)

在代码中,您可以看到我也尝试使用 SimpleCursorAdapter - 它运行良好。在调试模式下,我看到在 newView() 方法中所有 TextView 都已正确设置,但在 bindView() 中,这些 TextViews 不存在 - 它们为空。

我该如何解决这个问题?

4

1 回答 1

2

只需调试并检查运行流程可能是您在找到它之前尝试 setText() 的 TextView (finViewById) 可能这就是您获得 null text view 的原因。

于 2012-08-10T05:10:50.043 回答