空指针使用游标适配器时出现异常...请帮我识别它

时间:2012-10-31 06:49:15

标签: android

这里我保存记录并尝试使用自定义适配器在Listview中显示它。单击按钮点击我调用getAlldata方法。我得到空指针异常。

10-31 12:01:22.129: E/AndroidRuntime(758): FATAL EXCEPTION: main
10-31 12:01:22.129: E/AndroidRuntime(758): java.lang.NullPointerException
10-31 12:01:22.129: E/AndroidRuntime(758):  at com.salesforce.android.restsample.CustomAdapter.bindView(CustomAdapter.java:38)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.CursorAdapter.getView(CursorAdapter.java:250)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.AbsListView.obtainView(AbsListView.java:2033)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.ListView.makeAndAddView(ListView.java:1772)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.ListView.fillDown(ListView.java:672)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.ListView.fillFromTop(ListView.java:732)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.ListView.layoutChildren(ListView.java:1625)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.AbsListView.onLayout(AbsListView.java:1863)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1617)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1401)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1617)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.TableRow.onLayout(TableRow.java:121)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.TableLayout.onLayout(TableLayout.java:440)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1628)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1486)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.LinearLayout.onLayout(LinearLayout.java:1399)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.widget.FrameLayout.onLayout(FrameLayout.java:431)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.View.layout(View.java:11278)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewGroup.layout(ViewGroup.java:4224)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1489)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.os.Looper.loop(Looper.java:137)
10-31 12:01:22.129: E/AndroidRuntime(758):  at android.app.ActivityThread.main(ActivityThread.java:4424)
10-31 12:01:22.129: E/AndroidRuntime(758):  at java.lang.reflect.Method.invokeNative(Native Method)
10-31 12:01:22.129: E/AndroidRuntime(758):  at java.lang.reflect.Method.invoke(Method.java:511)
10-31 12:01:22.129: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
10-31 12:01:22.129: E/AndroidRuntime(758):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
10-31 12:01:22.129: E/AndroidRuntime(758):  at dalvik.system.NativeStart.main(Native Method)


Button **showOffline** = (Button)findViewById(R.id.show_offline_accounts);
          showOffline.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                    System.out.println("==="+databaseHelper.getAllData());
                    //Cursor c = databaseHelper.getAllData();

    customAdapter = new CustomAdapter(AccountListView.this,databaseHelper.getAllData(), CursorAdapter.NO_SELECTION);
                    //customAdapter.changeCursor(databaseHelper.getAllData());
                    lv.setAdapter(customAdapter);  
            }

          });

public class CustomAdapter extends CursorAdapter {

    public CustomAdapter(Context context,Cursor c,int flags) {
        super(context,c);
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {

        LayoutInflater inflater = LayoutInflater.from(parent.getContext());
        View retView = inflater
                .inflate(R.layout.single_row, parent, false);

        return retView;
    }


@Override
    public void bindView(View view, Context context, Cursor cursor) {

        TextView textViewPersonName = (TextView) view
                .findViewById(R.id.accountBillingCityEditText);
        textViewPersonName.setText(cursor.getString(cursor
                .getColumnIndex(cursor.getColumnName(1))));

        TextView textViewPersonPIN = (TextView) view
                .findViewById(R.id.accountBillingStateEditText);
        textViewPersonPIN.setText(cursor.getString(cursor.getColumnIndex(cursor
                .getColumnName(2))));
    }      
}




TextView textViewPersonName = (TextView) view.findViewById(R.id.accountBillingCityEditText); 
textViewPersonName.setText(cursor.getString(cursor.getColumnIndex(cursor.getColumnName(1)))); 

1 个答案:

答案 0 :(得分:-1)

您是否检查过视图是否为空?如果不是,则检查view .findViewById(R.id.accountBillingCityEditText);返回的内容。如果它仍然不为null那么(听起来可能很有趣,但我们需要涵盖所有基础)检查游标是否为空。