我正在使用Android服务进行android push notification

当我从datepicker选择日期/月份/年份时,它就像一种魅力(请参阅下面的代码)。 但是,当我尝试使用hard code值设置calender instance ,会给我一个NULL POINTER EXCEPTION

我如何尝试:

我为此尝试了两种方法,但是两者都出现了异常。

1.方法

    Calendar c = Calendar.getInstance();
    c.clear();
    int year = 2014;
    int month = 3;
    int day = 21;
    c.set(year, month, day); 

2.方法

    Calendar c = Calendar.getInstance();
    Calendar dt = Calendar.getInstance();
    dt.clear();
    dt.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE));

主要活动:

private ScheduleClient scheduleClient;

private SharedPreferences prefs; 
private String prefName = "userPrefs";
private static final String TITLE_KEY = "title"; 

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

    // Create a new service client and bind our activity to this service
    scheduleClient = new ScheduleClient(this);
    scheduleClient.doBindService();


    t1 = (TextView) findViewById(R.id.txt);
    nofitication();
}

private void nofitication(){

    Calendar c = Calendar.getInstance();
    c.clear();
    int year = 2014;
    int month = 3;
    int day = 21;
    c.set(year, month, day); 


    prefs = getSharedPreferences(prefName, MODE_PRIVATE); 
    SharedPreferences.Editor editor = prefs.edit(); 

    //---save the values in the EditText view to preferences--- 
     editor.putString(TITLE_KEY,"Muneeb's Notification"); 
     //---saves the values--- 
     editor.commit(); 

        scheduleClient.setAlarmForNotification(c);

//     Toast.makeText(this, "Notification set for: "+ day +"/"+ (month+1) +"/"+ year, Toast.LENGTH_SHORT).show();

}

在此行出现错误:

scheduleClient.setAlarmForNotification(c);

当我运行带有硬编码值的代码时, c始终为空。 (请参见下面的图像)。

在此处输入图片说明

logcat的

    04-21 10:07:39.430: E/AndroidRuntime(4721): FATAL EXCEPTION: main
04-21 10:07:39.430: E/AndroidRuntime(4721): Process: com.example.customnotification, PID: 4721
04-21 10:07:39.430: E/AndroidRuntime(4721): java.lang.RuntimeException: Unable to start activity ComponentInfo    {com.example.customnotification/com.example.customnotification.TestingActivity}: java.lang.NullPointerException
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread.access$800(ActivityThread.java:156)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.os.Looper.loop(Looper.java:157)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread.main(ActivityThread.java:5872)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at java.lang.reflect.Method.invokeNative(Native Method)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at java.lang.reflect.Method.invoke(Method.java:515)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1069)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:885)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at dalvik.system.NativeStart.main(Native Method)
04-21 10:07:39.430: E/AndroidRuntime(4721): Caused by: java.lang.NullPointerException
04-21 10:07:39.430: E/AndroidRuntime(4721):     at services.ScheduleClient.setAlarmForNotification(ScheduleClient.java:62)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at com.example.customnotification.TestingActivity.nofitication(TestingActivity.java:56)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at com.example.customnotification.TestingActivity.onCreate(TestingActivity.java:31)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.Activity.performCreate(Activity.java:5312)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
04-21 10:07:39.430: E/AndroidRuntime(4721):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
04-21 10:07:39.430: E/AndroidRuntime(4721):     ... 11 more

===============>>#1 票数:1 已采纳

服务尚未绑定时,您无法设置日历。 您应该听onServiceConnected

在您的具体示例中,您应该在ScheduleClient.java:47中设置日历。这样一来,连接服务并可以设置日历。

与(ScheduleClient.java)类似:

public class ScheduleClient {

// The hook into our service
private ScheduleService mBoundService;
// The context to start the service in
private Context mContext;
// A flag if we are connected to the service or not
private boolean mIsBound;

public ScheduleClient(Context context) {
    mContext = context;
}

/**
 * Call this to connect your activity to your service
 */
public void doBindService() {
    // Establish a connection with our service
    mContext.bindService(new Intent(mContext, ScheduleService.class), mConnection, Context.BIND_AUTO_CREATE);
    mIsBound = true;
}

/**
 * When you attempt to connect to the service, this connection will be called with the result.
 * If we have successfully connected we instantiate our service object so that we can call methods on it.
 */
private ServiceConnection mConnection = new ServiceConnection() {
    public void onServiceConnected(ComponentName className, IBinder service) {
        // This is called when the connection with our service has been established, 
        // giving us the service object we can use to interact with our service.
        mBoundService = ((ScheduleService.ServiceBinder) service).getService();
        Calendar c = Calendar.getInstance();
        c.clear();
        int year = 2014;
        int month = 3;
        int day = 21;
        c.set(year, month, day);
        ScheduleClient.this.setAlarmForNotification(c);
    }

    public void onServiceDisconnected(ComponentName className) {
        mBoundService = null;
    }
};

/**
 * Tell our service to set an alarm for the given date
 * @param c a date to set the notification for
 */
public void setAlarmForNotification(Calendar c){
    mBoundService.setAlarm(c);
}

/**
 * When you have finished with the service call this method to stop it 
 * releasing your connection and resources
 */
public void doUnbindService() {
    if (mIsBound) {
        // Detach our existing connection.
        mContext.unbindService(mConnection);
        mIsBound = false;
    }
}

}

  ask by Muneeb A. translate from so

未解决问题?本站智能推荐: