我的应用程序有一个后台 AsyncTask 每隔几秒钟检查一次是否有新提醒,如果有新提醒,它应该启动一个显示提醒消息的 dialogeFragment。问题在于我读到的 dialogeFregment 只能从活动中使用,您能否分享有关如何实现此功能的想法?
这是我们当前的代码:
public class checkReminderTask extends AsyncTask<FragmentManager, String, Void> {
Context context;
private MainActivity activity;
FragmentManager fm;
checkReminderTask(Context app_context){
context=app_context;
objPlayer=MediaPlayer.create(context, R.raw.its_time_time_tim);
reminderTable = new ParseObject("ReminderTable");
settingsTable = new ParseObject("SettingsTable");
androidId = Secure.getString(context.getContentResolver(), Secure.ANDROID_ID);
ParseQuery query = new ParseQuery("SettingsTable");
query.whereEqualTo("userID", androidId);
query.getFirstInBackground(new GetCallback() {
public void done(ParseObject object, ParseException e) {
if (object == null) {
//TODO something, throw exception
} else {
myNumber=object.getString("userNumber");
}
}
});
}
@Override
protected Void doInBackground(FragmentManager... arg) {
//infinitely loop, and move the ball accordingly
fm=arg[0];
while (1 < 2)
{
//downloading the reminder...
try
{
publishProgress(currentDateandTime,reminderExeTime,reciverNumber,message);
Thread.sleep(1000);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
@Override
protected void onProgressUpdate(String... values)
{
super.onProgressUpdate(values);
if(values[0].equals(values[1])){
MessageReceivedReminder dialog = new MessageReceivedReminder();
dialog.setMessageArguments(context,values[2],values[3]);
dialog.show(fm, "MyDialog");
objPlayer.start();
}
else
{
Log.i("thread", "compareFailed");
}
}
活动中的代码(尝试直接发送 FragmentManager):
FragmentManager fm = getFragmentManager();
ringThread.execute(fm);
现在我们收到一个运行时错误,因为空指针有没有人更好地了解我们如何做这样的事情?
谢谢!!