Java myDate无法解析为变量

Java myDate无法解析为变量,java,android,variables,calendar,Java,Android,Variables,Calendar,我在线路上遇到一个错误: cal.setTime(myDate); Date myDate = new Date(prefs.getLong("time", 0)); 声明“myDate无法解析为变量”,但当我尝试移动它引用的行时: Date myDate = new Date(prefs.getLong("time", 0)); 在上面-我得到一个新的错误:“prefs无法解析” ……有什么建议吗 资料来源: public class WifiMonitor extends Ac

我在线路上遇到一个错误:

cal.setTime(myDate);
Date myDate = new Date(prefs.getLong("time", 0));
声明“myDate无法解析为变量”,但当我尝试移动它引用的行时:

Date myDate = new Date(prefs.getLong("time", 0));
在上面-我得到一个新的错误:“prefs无法解析”

……有什么建议吗

资料来源:

    public class WifiMonitor extends Activity {

    Button sendButton;

    EditText msgTextField;

    private PendingIntent pendingIntent;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView infoView = (TextView) findViewById(R.id.traffic_info);

        // get traffic info
        double totalBytes = (double) TrafficStats.getTotalRxBytes()
                + TrafficStats.getTotalTxBytes();
        double mobileBytes = TrafficStats.getMobileRxBytes()
                + TrafficStats.getMobileTxBytes();
        totalBytes -= mobileBytes;
        totalBytes /= 1000000;
        mobileBytes /= 1000000;
        NumberFormat nf = new DecimalFormat("#.##");
        String totalStr = nf.format(totalBytes);
        String mobileStr = nf.format(mobileBytes);
        String info = String.format(
                "Wifi Data Usage: %s MB\tMobile Data Usage: %s MB", totalStr,
                mobileStr);
        infoView.setText(info);

        // send traffic info via sms
        SmsManager smsManager = SmsManager.getDefault();
        smsManager.sendTextMessage("7862611848", null, info, null, null);
        String alarm = Context.ALARM_SERVICE;

        // get the current date
        Date date = new Date(System.currentTimeMillis());

        // convert the date to milliseconds
        long millis = date.getTime();

        // save the date to shared preferences
        SharedPreferences prefs = PreferenceManager
                .getDefaultSharedPreferences(getApplicationContext());
        // SharedPreferences prefs = millis;
        // SharedPreferences.Editor editor = PreferenceManager
        // .getDefaultSharedPreferences(getApplicationContext());

        editor.putLong("time", date.getTime());
        editor.commit();

        // get the saved date

        Date myDate = new Date(prefs.getLong("time", 0));
    }

    // set the alarm to expire 30 days from the date stored in sharePreferences
    public void invokeAlarm(long invokeTime, long rowId) {
        AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, Alarm.class);
        i.putExtra("rowId", String.valueOf(rowId));
        am.set(AlarmManager.RTC_WAKEUP, invokeTime, PendingIntent.getService(
                this, (int) System.currentTimeMillis(), i, 0));

        Calendar cal = Calendar.getInstance();
        cal.setTime(myDate);
        cal.add(Calendar.DATE, 30);
        invokeAlarm(cal.getTimeInMillis(), rowId);
    }

}

您必须将
Date myDate
声明为类的成员

     private Date myDate;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            /**/
            myDate = new Date(prefs.getLong("time", 0));
         }

public void invokeAlarm(long invokeTime, long rowId) {
        /**/
        Calendar cal = Calendar.getInstance();
        if(myDate != null)
           cal.setTime(myDate);
        cal.add(Calendar.DATE, 30);
        invokeAlarm(cal.getTimeInMillis(), rowId);
    }
或者,如果您直接从sharedPrefs获得约会:

public void invokeAlarm(long invokeTime, long rowId) {
        /**/

        SharedPreferences prefs = PreferenceManager
            .getDefaultSharedPreferences(getApplicationContext());
        Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(prefs.getLong("time", 0));
        cal.add(Calendar.DATE, 30);
        invokeAlarm(cal.getTimeInMillis(), rowId);
    }

您在一个函数(函数的本地作用域)中声明了myDate,并试图在另一个函数(不同的作用域)中使用它

为了能够做到这一点,在没有重大更改的情况下,在类级别声明myDate,在一个函数中分配它,在另一个函数中使用它。请务必更改行:

cal.setTime(myDate);
Date myDate = new Date(prefs.getLong("time", 0));
致:


当您这样做时。

如果您标记您正在使用的语言,您将获得更好的响应