执行代码时出现游标错误:

public void getCartData()
{
        MainActivity activity = (MainActivity) getActivity();
        Prodotti tempCartItem = new Prodotti();
        cart_list.clear();

        Cursor c=sqLite.getAllProdottiCarrello();   //here is the error
        count=0;


我敢肯定问题不在于调用的方法。 (sqLite是一类的可变性
扩展SQLiteOpenHelper

这是logcat:

04-16 20:55:23.900  25062-25062/com.example.utente.mysupermarket W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41e0dda0)
04-16 20:55:23.910  25062-25062/com.example.utente.mysupermarket E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.example.utente.mysupermarket, PID: 25062
    java.lang.NullPointerException
            at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
            at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
            at com.example.utente.mysupermarket.db.DatabaseHelper.getAllProdottiCarrello(DatabaseHelper.java:126)
            at com.example.utente.mysupermarket.CarrelloFragment.getCartData(CarrelloFragment.java:451)
            at com.example.utente.mysupermarket.CarrelloFragment.onCreateView(CarrelloFragment.java:70)
            at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:953)
            at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1136)
            at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
            at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1499)
            at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:488)
            at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
            at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
            at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
            at android.support.v7.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:453)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1410)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5363)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2548)
            at android.view.View.measure(View.java:17478)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2266)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1395)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1594)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1253)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6596)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
            at android.view.Choreographer.doCallbacks(Choreographer.java:603)
            at android.view.Choreographer.doFrame(Choreographer.java:573)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5476)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
            at dalvik.system.NativeStart.main(Native Method)

最佳答案

public class CarrelloFragment extends Fragment {
        DatabaseHelper sqLite=new DatabaseHelper(this.getActivity());


getActivity()执行完onAttach后,返回承载片段的Activity。之前返回null。在您的情况下,getActivity返回null。为了解决延迟成员初始化问题。例如,您可以在首次调用getCartData时对其进行初始化:

public void getCartData() {
        MainActivity activity = (MainActivity) getActivity();
        if (sqLite == null) {
              sqLite=new DatabaseHelper(activity);
        }

10-04 13:45