gpt4 book ai didi

android - 将 HARD CODE 值设置为日历实例后出现空指针异常

转载 作者:行者123 更新时间:2023-11-30 02:56:23 24 4
gpt4 key购买 nike

我正在使用 Android 服务开发 android 推送通知

当我从 datepicker 中选择 date/month/year 时,它就像一个魅力一样工作(参见下面的代码)。但是,当我尝试使用 硬代码 值设置 calender 实例 时,它会给我一个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));

主要 Activity :

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 始终变为 null。(参见下图)。

enter image description here

日志

    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

最佳答案

未绑定(bind)服务时无法设置日历。你宁愿听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;
}
}

}

关于android - 将 HARD CODE 值设置为日历实例后出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23191088/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com