IDE 似乎没有检测到任何错误。但是当系统在物理设备上运行时,系统到了这个类就崩溃了。似乎是什么问题?可能是声明的共享偏好吗?我怎样才能解决这个问题?
package com.example.mobile_app;
import android.os.Bundle;
import android.app.Activity;
import android.content.SharedPreferences;
import android.view.Menu;
import android.view.Window;
import android.widget.TextView;
public class Post_4 extends Activity {
static int time = Post_1.getTime(), post_error = Post_3.get_pos_err();
int percent;
float quotient_Float = (float)post_error/(float)time;
float computation=0;
SharedPreferences value = getApplicationContext().getSharedPreferences("Values", MODE_PRIVATE);
SharedPreferences.Editor editor = value.edit();
TextView percentage_display;
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_4);
percentage_display = (TextView)findViewById(R.id.textView1);
Store();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.post_4, menu);
return true;
}
private void Store(){
computation=value.getFloat("Value_Quotient", quotient_Float)-quotient_Float/value.getFloat("Value_Quotient", quotient_Float);
if(value.getFloat("Value_Quotient", quotient_Float)==0.0f || quotient_Float < value.getFloat("Value_Quotient", quotient_Float))
{
editor.putFloat("Value_Quotient", quotient_Float);
editor.commit();
if(value.getFloat("Value_Quotient", quotient_Float)==0.0f)
percentage_display.setText("System cannot make comparison on first use.");
else
percentage_display.setText("POSITIVE by "+Math.round(computation*100)+"%.");
}
else
percentage_display.setText("NEGATIVE by "+Math.round(computation*100)+"%.");
}
}
错误日志:
08-29 01:42:59.390: E/AndroidRuntime(29985): FATAL EXCEPTION: main
08-29 01:42:59.390: E/AndroidRuntime(29985): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.mobile_app/com.example.mobile_app.Post_4}: java.lang.NullPointerException
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1991)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2092)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.access$600(ActivityThread.java:133)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1203)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.os.Handler.dispatchMessage(Handler.java:99)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.os.Looper.loop(Looper.java:137)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.main(ActivityThread.java:4797)
08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.reflect.Method.invokeNative(Native Method)
08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.reflect.Method.invoke(Method.java:511)
08-29 01:42:59.390: E/AndroidRuntime(29985): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-29 01:42:59.390: E/AndroidRuntime(29985): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-29 01:42:59.390: E/AndroidRuntime(29985): at dalvik.system.NativeStart.main(Native Method)
08-29 01:42:59.390: E/AndroidRuntime(29985): Caused by: java.lang.NullPointerException
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
08-29 01:42:59.390: E/AndroidRuntime(29985): at com.example.mobile_app.Post_4.<init>(Post_4.java:16)
08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.Class.newInstanceImpl(Native Method)
08-29 01:42:59.390: E/AndroidRuntime(29985): at java.lang.Class.newInstance(Class.java:1319)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.Instrumentation.newActivity(Instrumentation.java:1053)
08-29 01:42:59.390: E/AndroidRuntime(29985): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1982)
08-29 01:42:59.390: E/AndroidRuntime(29985): ... 11 more