从调用共享片段preF当上下文NullPointerException异常错误 [英] NullPointerException error on context when calling sharedPref from fragment

查看:163
本文介绍了从调用共享片段preF当上下文NullPointerException异常错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不断收到一个 NullPointerException异常上下文当我尝试启动我的共享preference 的init从片段。

以下是错误:

 产生的原因:显示java.lang.NullPointerException:尝试调用虚拟方法android.content.Shared preferences android.content.Context.getShared preferences(java中。 lang.String,INT)对空对象引用
            在c.c.modular.SessionManager<&初始化GT;(SessionManager.java:68)
            在c.c.ProfileCompletedFragment.setupData(ProfileCompletedFragment.java:75)
            在c.c.ProfileCompletedFragment.passDataToFragment(ProfileCompletedFragment.java:210)
            在c.c.ProfileActivity.onCreate(ProfileActivity.java:287)

下面是初始化:

  //初始化
    公众是SessionManager(上下文的背景下){
        this._context =背景;            preF = _context.getShared preferences(preF_NAME,PRIVATE_MODE);
            编辑= pref.edit();    }

下面是片段中调用:

 私人无效setupData(){
        //会话管理器
        会话=新SessionManager(中间getActivity());        //建立数据库
        DB =新DatabaseHelper(getActivity());        计数= 0;    }

编辑:

我已经意识到的是,getActivity为空,由于该片段尚未连接到活动本身,试图找出如何安装它,所以我可以对活动通话的事实......这是我在下面的编辑新的尝试......同样的问题...

  profileJoinedFrag =新ProfileJoinedFragment();
        profileCompletedFrag =新ProfileCompletedFragment();
        android.support.v4.app.FragmentManager FM = getSupportFragmentManager();        fm.beginTransaction()
                .replace(R.id.frame_container,profileCompletedFrag).commit();        //设置片段
        的String [] = tabTitles {帖子,联接};
        适配器=新TabAdapter(FM,profileCompletedFrag,profileJoinedFrag,tabTitles);// //初始化ViewPager并设置一个适配器
        寻呼机=(ViewPager)findViewById(R.id.profilePager);
        pager.setAdapter(适配器);        //绑定的选项卡的ViewPager
        标签=(PagerSlidingTabStrip)findViewById(R.id.profileTabs);
        tabs.setViewPager(寻呼机);        //默认的位置信息发送
        //        pager.setCurrentItem(0);
        profileCompletedFrag.passDataToFragment(用户ID);


解决方案

使用<$c$c>getActivity().getApplicationContext().getShared$p$pferences(\"name\",Context.MODE_PRIVATE);而不是环境中使用应用程序上下文

I keep getting a NullPointerException on the context when I try to initiate my SharedPreference init from a fragment.

Here is the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
            at c.c.modular.SessionManager.<init>(SessionManager.java:68)
            at c.c.ProfileCompletedFragment.setupData(ProfileCompletedFragment.java:75)
            at c.c.ProfileCompletedFragment.passDataToFragment(ProfileCompletedFragment.java:210)
            at c.c.ProfileActivity.onCreate(ProfileActivity.java:287)

Here is the initializer:

// init
    public SessionManager(Context context) {
        this._context = context;

            pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
            editor = pref.edit();

    }

Here is the call in the fragment:

private void setupData() {
        //sessions manager
        session = new SessionManager(getActivity());

        //establishing db
        db = new DatabaseHelper(getActivity());

        count = 0;

    }

EDIT:

What I have realized is that getActivity is null due to the fact that the fragment is not yet attached to the activity itself, trying to figure out how to attach it so I can make a call to the activity... This is my new attempt in the edit below... same issue...

 profileJoinedFrag = new ProfileJoinedFragment();
        profileCompletedFrag = new ProfileCompletedFragment();
        android.support.v4.app.FragmentManager fm = getSupportFragmentManager();

        fm.beginTransaction()
                .replace(R.id.frame_container, profileCompletedFrag).commit();

        //Setting up fragment
        String[] tabTitles =  {"Posts", "Joins"};
        adapter = new TabAdapter(fm, profileCompletedFrag, profileJoinedFrag, tabTitles);

//        // Initialize the ViewPager and set an adapter
        pager = (ViewPager) findViewById(R.id.profilePager);
        pager.setAdapter(adapter);

        // Bind the tabs to the ViewPager
        tabs = (PagerSlidingTabStrip) findViewById(R.id.profileTabs);
        tabs.setViewPager(pager);

        //default place send
        //

        pager.setCurrentItem(0);
        profileCompletedFrag.passDataToFragment(userId);

解决方案

Use getActivity().getApplicationContext().getSharedPreferences("name",Context.MODE_PRIVATE); instead of context use application context

这篇关于从调用共享片段preF当上下文NullPointerException异常错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆