我正在尝试使用 afreechart 绘制折线图,我已经使用SharedPreferences保存了x和y点。但是,当我尝试从共享偏好中获取一些信息时,我得到了一个错误,我怀疑它与静态和非静态方法无法协同工作,但我无法弄明白。
所以这里是我添加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;
}
}
LogCat
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)
答案 0 :(得分:0)
为什么不在你认为不起作用的代码之前打破,并且通过查看你的成员变量等单步执行。看看你是否能发现错误。你有一个调试器,所以我建议你应该使用它。 :)
答案 1 :(得分:0)
我的猜测是没有名称stat的共享首选项,因此someStat始终为null
答案 2 :(得分:0)
您的Stat
课程延伸Activity
,但您将其创建为new Stat()
。你不能为Activity
。
相反,您可以使用
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
答案 3 :(得分:0)
请检查您的Manifest.xml文件中是否有所有活动的条目。 如果你在帖子中显示它会很有帮助。
答案 4 :(得分:0)
您正在尝试从共享首选项中获取值,但您没有在共享首选项中添加任何值。这就是 NullPointerException 的原因。
请检查您的方法getround()