使用this.getSharedPreferences不断抛出NPE

时间:2014-10-14 01:42:50

标签: java android

我尝试使用pageViewer创建一个包含几页的简单应用程序,在主页面上它有两个按钮和两个计数器。一个计数器被动地增加,另一个计数器显示多少。一个按钮使第一个计数器每次点击增加一个,另一个按钮增加被动。到那儿我一切都很好,花花公子。但现在我尝试使用SharedPreferences保存值。但是,当我运行代码时,它会在行

上抛出一个NPE

SharedPreferences prefs = this.getSharedPreferences("savedInfo", Context.MODE_PRIVATE);

这里是完整的主要活动:

public class MainActivity extends FragmentActivity {
SharedPreferences prefs = this.getSharedPreferences("savedInfo", Context.MODE_PRIVATE);
StandFragment standFragment;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    standFragment.setTotalMoney(prefs.getInt("totalMoney", 0));
}

@Override
public void onPause(){
    super.onPause();
    prefs.edit().putInt("totalMoney",standFragment.getTotalMoney()).apply();
}
private class MyPagerAdapter extends FragmentPagerAdapter {

    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int pos){
        switch(pos){
        case 0: 
            standFragment = StandFragment.newInstance("STAND");
            return standFragment;
        case 1: return CartFragment.newInstance("CART");
        case 2: return ThirdFragment.newInstance("THIRD");
        case 3: return ThirdFragment.newInstance("FOURTH");
        case 4: return ThirdFragment.newInstance("FIFTH");
        default: return ThirdFragment.newInstance("Default");
        }
    }

    @Override
    public int getCount(){
        return 5;
    }
}
}

StandFragment.java:

public class StandFragment extends Fragment{

private int totalMoney;
private int moneyPerSec;
private int moneyPerClick;
Handler handleCounter = new Handler();
TextView counter;
TextView perSecCounter;
Button buttonMoney;
Button buttonMoneyPerSec;



public void setTotalMoney(int money){
    this.totalMoney = money;
}
public int getTotalMoney(){
    return totalMoney;
}
public void setMoneyPerSec(int money){
    this.moneyPerSec = money;
}
public int getMoneyPerSec(){
    return moneyPerSec;
}
public void setMoneyPerClick(int money){
    this.moneyPerClick = money;
}
public int getMoneyPerClick(){
    return moneyPerClick;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    View v = inflater.inflate(R.layout.stand_frag, container, false);
    super.onCreate(savedInstanceState);

    buttonMoney = (Button)v.findViewById(R.id.buttonMoney);
    buttonMoneyPerSec = (Button)v.findViewById(R.id.buttonMoneyPerSec);
    counter = (TextView)v.findViewById(R.id.tvMoney);
    perSecCounter = (TextView)v.findViewById(R.id.tvMonPerSec);

    TextView tv = (TextView) v.findViewById(R.id.tvStandFrag);
    tv.setText(getArguments().getString("msg"));

    handleCounter.post(updateCounter);
    if (savedInstanceState==null){
        moneyPerSec = 1;
        moneyPerClick = 1;
        totalMoney=0;
    }
    else{
        moneyPerSec = savedInstanceState.getInt("moneyPerSec");
        moneyPerClick = savedInstanceState.getInt("moneyPerClick");
        totalMoney = savedInstanceState.getInt("totalMoney");
    }

    buttonMoney.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            totalMoney += moneyPerClick;
            counter.setText(""+totalMoney);
            perSecCounter.setText(""+moneyPerSec);
        }
    });
    buttonMoneyPerSec.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v){
            if (totalMoney>=10){
                totalMoney-=10;
                counter.setText(""+totalMoney);
                moneyPerSec++;
                perSecCounter.setText(""+moneyPerSec);
            }
        }
    });
    return v;
}

Runnable updateCounter = new Runnable(){
    public void run(){
        totalMoney += moneyPerSec;
        counter.setText(""+totalMoney);
        handleCounter.postDelayed(this, 1000);
    }
};
public void onPause(Bundle savedInstanceState){
    savedInstanceState.putInt("totalMoney",totalMoney);
    savedInstanceState.putInt("moneyPerSec",moneyPerSec);
    savedInstanceState.putInt("moneyPerClick",moneyPerClick);
    super.onSaveInstanceState(savedInstanceState);

}
public static StandFragment newInstance(String text){

    StandFragment f = new StandFragment();
    Bundle b = new Bundle();
    b.putString("msg", text);

    f.setArguments(b);

    return f;
}
}

已编辑onCreate()onPause()

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
    pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));

    SharedPreferences prefs = this.getSharedPreferences("savedInfo", Context.MODE_PRIVATE);
    standFragment.setTotalMoney(prefs.getInt("totalMoney", 0));
}

@Override
public void onPause(){
    super.onPause();

    SharedPreferences prefs = this.getSharedPreferences("savedInfo", Context.MODE_PRIVATE);
    prefs.edit().putInt("totalMoney",standFragment.getTotalMoney()).apply();
}

0 个答案:

没有答案