afreechartを使用して折れ線グラフを描画しようとしています。すでに SharedPreferences を使用して x 点と y 点を保存しています。ただし、sharedpreferences から情報を取得しようとすると、エラーが発生しました。静的メソッドと非静的メソッドが連携していないことに関係があると思われますが、それを理解できません。
だからここに私が x と y ポイントを追加するクラスがあります:
public class DrawView extends DemoView{
static int round;
static int score;
public DrawView(Context context) {
super(context);
final AFreeChart chart = createChart();
setChart(chart);
}
private static XYSeriesCollection createDataset() {
XYSeries xyS1 = new XYSeries("Progress", true, false);
//Here I try to get the info from SharedPreferences via another class
Stat t = new Stat();
round =t.getround();
for(int i = 0; i < round; i++){
score = t.getscore(i);
xyS1.add(i, score);
}
XYSeriesCollection xySC = new XYSeriesCollection();
xySC.addSeries(xyS1);
return xySC;
}
/**
* Creates a sample chart.
* @param dataset the dataset.
* @return A sample chart.
*/
private static AFreeChart createChart() {
XYDataset dataset = createDataset();
AFreeChart chart = ChartFactory.createXYLineChart(
"Statistics",
"Rounds",
"Points",
dataset,
PlotOrientation.VERTICAL,
false,
true,
false);
XYPlot plot = (XYPlot) chart.getPlot();
XYLineAndShapeRenderer renderer = new XYLineAndShapeRenderer();
renderer.setSeriesLinesVisible(0, true);
plot.setRenderer(renderer);
NumberAxis domainAxis = (NumberAxis) plot.getDomainAxis();
domainAxis.setUpperMargin(0.2);
// add some annotations...
XYTextAnnotation annotation = null;
Font font = new Font("SansSerif", Typeface.NORMAL, 12);
annotation = new XYTextAnnotation("Progress", 96, 57);
annotation.setFont(font);
annotation.setTextAnchor(TextAnchor.HALF_ASCENT_LEFT);
plot.addAnnotation(annotation);
return chart;
}
}
そして、これが私が SharePreferences から情報を取得しようとするクラスです:
public class Stat extends Activity {
public static String filenamestat = "Stat";
SharedPreferences someStat;
/**
* Called when the activity is starting.
* @param savedInstanceState
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
DrawView mView = new DrawView(this);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(mView);
}
public int getround(){
//Here is the error
someStat = getSharedPreferences(filenamestat, 0);
String roundtaker = someStat.getString("round", "0");
int round = Integer.parseInt(roundtaker);
return round;
}
public int getscore(int a){
String scoretaker = Integer.toString(a);
String stringscore = someStat.getString(scoretaker, "0");
int score = Integer.parseInt(stringscore);
return score;
}
}
ログキャット
07-29 20:09:47.422: E/AndroidRuntime(414): FATAL EXCEPTION: main
07-29 20:09:47.422: E/AndroidRuntime(414): java.lang.RuntimeException: Unable to start activity ComponentInfo{at com.the.package/at com.the.package.Stat}: java.lang.NullPointerException
07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Handler.dispatchMessage(Handler.java:99)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.os.Looper.loop(Looper.java:123)
07-29 20:09:47.422: E/AndroidRuntime(414): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invokeNative(Native Method)
07-29 20:09:47.422: E/AndroidRuntime(414): at java.lang.reflect.Method.invoke(Method.java:521)
07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-29 20:09:47.422: E/AndroidRuntime(414): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-29 20:09:47.422: E/AndroidRuntime(414): at dalvik.system.NativeStart.main(Native Method)
07-29 20:09:47.422: E/AndroidRuntime(414): Caused by: java.lang.NullPointerException
07-29 20:09:47.422: E/AndroidRuntime(414): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:146)
07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.Stat.getround(Stat.java:33)
07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createDataset(DrawView.java:50)
07-29 20:09:47.422: E/AndroidRuntime(414): at at com.the.package.DrawView.createChart(DrawView.java:71)
07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.DrawView.<init>(DrawView.java:38)
07-29 20:09:47.422: E/AndroidRuntime(414): at com.the.package.Stat.onCreate(Stat.java:26)