长话短说,我有一个处理我的应用程序共享首选项的类。 我从各种其他类中调用它没有问题,但当我尝试从我的服务(从相同的APK)调用它时,我得到一个null异常。我猜它是从错误的上下文或类似的东西中调用的。这是相关的代码。
MainActivity.java
package com.deskwizard.audiomanager;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import com.deskwizard.audiomanager.DataSaveRestore;
public class MainActivity extends FragmentActivity {
public static Context contextOfApplication;
final FragmentManager fm = getFragmentManager();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
contextOfApplication = getApplicationContext();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_settings, new FadeBalanceFragment());
ft.commit();
// TODO: Load previously saved settings for all values
DataSaveRestore.restore_all();
// TODO: init I2C
}
public static Context getContextOfApplication() {
return contextOfApplication;
}
}
DataSaveRestore.java(defaultpreferences类)
package com.deskwizard.audiomanager;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.util.Log;
public class DataSaveRestore extends Application {
// Data variables
public static int Bass_level, Bass_CFreq, Bass_Qfact, Sub_level,
Sub_Lowpass, Treble_level, Treble_CFreq, Mid_level, Mid_CFreq,
Mid_Qfact, Fade, Balance, Loudness_level, Loudness_freq,
Loudness_boost;
static boolean Bass_DCMode, Loudness_state;
static Context applicationContext = MainActivity.getContextOfApplication();
public static void restore_all() {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(applicationContext);
if (prefs.getInt("data_saved", 0) == 0) {
set_defaults();
load_defaults();
} else {
load_defaults();
}
//TODO: send settings to TDA7418
DS3903.set_lowpass(DataSaveRestore.Sub_Lowpass);
};
服务代码段:
public class AudioManagerService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
// TODO do something useful
Log.d("com.deskwizard.audiomanager", "starting service...");
DataSaveRestore.restore_all(); // restore settings to TDA7418/DS3903
start();
return Service.START_STICKY;
}
Null Exception错误引用此行,只有从服务调用时,它才能从主应用程序和其他类中正常工作:
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(applicationContext);
如果有任何进一步的代码可以帮助缩小范围,请告诉我。 谢谢, DW
答案 0 :(得分:0)
因为,在您的服务中,当您致电时,DataSaveRestore.restore_all();
它会参考,(因为Servce没有可用的MainActivity上下文)
static Context applicationContext = MainActivity.getContextOfApplication();
在这一行,applicationContext
将 null ,因为它无法找到MainActivity初始化
简单地说,只需从Application类更改restore_all()
方法即可。
首先删除静态并使用Android应用程序类方法的getApplicationContext()
来获取应用程序上下文,如服务中所示,
public void restore_all() {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
if (prefs.getInt("data_saved", 0) == 0) {
set_defaults();
load_defaults();
} else {
load_defaults();
}
//TODO: send settings to TDA7418
DS3903.set_lowpass(DataSaveRestore.Sub_Lowpass);
};
现在通过初始化Application类的对象而不是静态方式来调用restore_all();
。
像,
DataSaveRestore dataSaveRestore = (DataSaveRestore) getApplicationContext();
dataSaveRestore.restore_all();