0

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
4

2 回答 2

2
SharedPreferences value = getApplicationContext().getSharedPreferences("Values", MODE_PRIVATE);
SharedPreferences.Editor editor = value.edit();

将这两行移至函数 Store。在您的情况下,当创建类时,它将尝试在getApplicationContext不会创建上下文之前使用函数访问应用程序的上下文,因此请等待onCreate发生,然后保存这两个值。希望这会有所帮助。

从崩溃日志中可以清楚地看出,没有创建上下文。看到这条线

Caused by: java.lang.NullPointerException
08-29 01:42:59.390: E/AndroidRuntime(29985):    at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)

做一些这样的事情。

SharedPreferences value;
SharedPreferences.Editor editor;

@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);
    value = getApplicationContext().getSharedPreferences("Values",  MODE_PRIVATE);
    editor = value.edit();
    Store();
}
于 2013-08-28T17:50:00.360 回答
0

将您的代码更改为

SharedPreferences.Editor editor;

@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);
    SharedPreferences value =getSharedPreferences("Values",  MODE_PRIVATE);
    editor = value.edit();
    Store();
}
于 2013-08-28T18:06:26.557 回答