Java Android:从另一个类中的home小部件访问SharedReference

Java Android:从另一个类中的home小部件访问SharedReference,java,android,widget,sharedpreferences,Java,Android,Widget,Sharedpreferences,我有一个活动,它在另一个访问SharedReference的类中运行一个方法。我想制作一个小部件,它可以通过点击按钮来运行相同的方法。只要应用程序在后台打开,它就会工作,但如果它关闭,当我按下小部件上的按钮时,应用程序就会崩溃 我相当确定这是因为我需要访问SharedReferences的上下文,我尝试分别从活动和小部件传递上下文,但它不起作用 基本上,当方法位于另一个类中时,如何从小部件访问SharedReference 活动代码: public class MainActivity exte

我有一个活动,它在另一个访问SharedReference的类中运行一个方法。我想制作一个小部件,它可以通过点击按钮来运行相同的方法。只要应用程序在后台打开,它就会工作,但如果它关闭,当我按下小部件上的按钮时,应用程序就会崩溃

我相当确定这是因为我需要访问SharedReferences的上下文,我尝试分别从活动和小部件传递上下文,但它不起作用

基本上,当方法位于另一个类中时,如何从小部件访问SharedReference

活动代码:

public class MainActivity extends AppCompatActivity {

    protected static Context context;
    SharedPreferences prefs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        MainActivity.context = getApplicationContext();
    }

    public void panicFromView(View view){
        prefs = PreferenceManager.getDefaultSharedPreferences(context);
        Methods.panic(prefs);
    }
}
小部件代码:

public class PanicWidget extends AppWidgetProvider {

    public static final String CLICK_PANIC = "PANIC";
    SharedPreferences prefs;

    @Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
        RemoteViews remoteViews = new RemoteViews(context.getPackageName(), R.layout.panic_widget);

        Intent intent = new Intent(context, PanicWidget.class);
        intent.setAction(CLICK_PANIC);

        PendingIntent actionPendingIntent = PendingIntent.getBroadcast(context, 0, intent, 0);

        remoteViews.setOnClickPendingIntent(R.id.panicButton, actionPendingIntent);

        appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);

        prefs = PreferenceManager.getDefaultSharedPreferences(context);
    }

    @Override
    public void onReceive(Context context, Intent intent){

        super.onReceive(context, intent);

        if (CLICK_PANIC.equals(intent.getAction())){
            panicFromWidget(prefs);
        }
    }

    public void panicFromWidget(SharedPreferences prefsIn){
        Methods.panic(prefsIn);
    }
}
方法:

public class Methods {

    public static Context getAppContext() {
        return MainActivity.context;
    }

    public static void panic(SharedPreferences prefsIn){

        //loops through contacts
        for (int i = 1; i <= 5; i++) {
            String contactKey = "contact-sms" + i;
            String contactNo = getContact(contactKey, prefsIn);

            //sends sms only if contact number exists
            if (!contactNo.equals("")) {
                sendSMS(contactNo);
            }
        }
    }

    //returns contact number from contact name
    public static String getContact(String key, SharedPreferences prefsIn){
        return prefsIn.getString(key, null);
    }

    //sends SMS to contact number
    public static void sendSMS(String phoneNo){
        try{
            String msg = "test";

            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(phoneNo, null, msg, null, null);

            Toast.makeText(getAppContext(), "SMS Sent", Toast.LENGTH_SHORT).show();
        } catch (Exception ex) {}
    }
}
公共类方法{
公共静态上下文getAppContext(){
返回MainActivity.context;
}
公共静态无效恐慌(SharedReferences优先){
//通过触点循环

对于(int i=1;i无所谓,它确实有效。不知何故,我的SharedReferences在我不知道的情况下被清除了,因此它抛出了一个nullpointerexception。

发布你的代码,说明你是如何从小部件和活动传递上下文的。显示你必须访问SharedReference的代码。我尝试过传递SharedReferences,仍然不起作用。