I just wrote this simple piece of code, but it doesn't work, but the debugger stopped my program. Any ideas? This is the line that probably causes the error:
SharedPreferences.Editor editor = sharedPreferences.edit();
package com.***.juraquiz;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class FragmentStatistics extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
int answers_total;
int correct_answers_total;
int wrong_answers_total;
SharedPreferences sharedPreferences = getSharedPreferences("statistics", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("ANSWERS_TOTAL", 90);
editor.putInt("CORRECT_ANSWERS_TOTAL", 82);
editor.commit();
View view = inflater.inflate(R.layout.fragment_statistics, container, false);
SupportFunctions.setViewBackgroundWithDrawable(view.findViewById(R.id.tv_heading_statistics), getResources().getDrawable(R.drawable.quiz_heading_background));
TextView tv_answers_round = (TextView)view.findViewById(R.id.tv_answers_round_value);
tv_answers_round.setText(String.valueOf(Globals.getANSWERS_ROUND()));
TextView tv_correct_answers_round = (TextView)view.findViewById(R.id.tv_correct_answers_round_value);
tv_correct_answers_round.setText(String.valueOf(Globals.getCORRECT_ANSWERS_ROUND()));
TextView tv_wrong_answers_round = (TextView)view.findViewById(R.id.tv_wrong_answers_round_value);
tv_wrong_answers_round.setText(String.valueOf(Globals.getANSWERS_ROUND()-Globals.getCORRECT_ANSWERS_ROUND()));
SharedPreferences getSharedPreferences = getSharedPreferences("statistics", Context.MODE_PRIVATE);
answers_total = getSharedPreferences.getInt("ANSWERS_TOTAL", 0);
correct_answers_total = getSharedPreferences.getInt("CORRECT_ANSWERS_TOTAL", 0);
wrong_answers_total = answers_total - correct_answers_total;
TextView tv_answers_total = (TextView)view.findViewById(R.id.tv_answers_total_value);
tv_answers_total.setText(String.valueOf(answers_total));
TextView tv_correct_answers_total = (TextView)view.findViewById(R.id.tv_correct_answers_total_value);
tv_correct_answers_total.setText(String.valueOf(correct_answers_total));
TextView tv_wrong_answers_total = (TextView)view.findViewById(R.id.tv_wrong_answers_total_value);
tv_wrong_answers_total.setText(String.valueOf(wrong_answers_total));
return view;
}
private SharedPreferences getSharedPreferences(String string,
int modePrivate) {
// TODO Auto-generated method stub
return null;
}
}
This is what I get:
Pick [Android Application]
Pick [Android Application]
Pick [Android Application]
Pick [Android Application]
DalvikVM [localhost:8600]
Pick [Android Application]
DalvikVM [localhost:8600]
Thread [<1> main] (Suspended (exception NullPointerException))
FragmentStatistics.onCreateView(LayoutInflater, ViewGroup, Bundle) line: 22
FragmentStatistics(Fragment).performCreateView(LayoutInflater, ViewGroup, Bundle) line: 1500
FragmentManagerImpl.moveToState(Fragment, int, int, int, boolean) line: 927
FragmentManagerImpl.moveToState(int, int, int, boolean) line: 1104
BackStackRecord.run() line: 682
FragmentManagerImpl.execPendingActions() line: 1467
FragmentManagerImpl$1.run() line: 440
Handler.handleCallback(Message) line: 730
FragmentActivity$1(Handler).dispatchMessage(Message) line: 92
Looper.loop() line: 137
ActivityThread.main(String[]) line: 5419
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) line: not available [native method]
Method.invoke(Object, Object...) line: 525
ZygoteInit$MethodAndArgsCaller.run() line: 1187
ZygoteInit.main(String[]) line: 1003
NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder_2] (Running)
Thread [<9> Binder_1] (Running)
Thread [<11> Binder_3] (Running)