Hy家伙,我对Android开发者有疑问......
我有一个名为proKlasa()的课程
public class proKlasa()
{
private static final int NOTIFICATION_ID = 1;
public void sendSMS(Context ctx, String number, String message, String notification_text)
{
NotificationManager mNotificationManager = (NotificationManager) ctx.getSystemService(Context.NOTIFICATION_SERVICE);
mNotificationManager.cancelAll();
Notification notification = new Notification(R.drawable.icon, notification_text, System.currentTimeMillis());
PendingIntent contentIntent = PendingIntent.getActivity(ctx, 0, null, 0);
notification.setLatestEventInfo(ctx, null, null, contentIntent);
mNotificationManager.notify(NOTIFICATION_ID, notification);
SmsManager sms = SmsManager.getDefault();
sms.sendTextMessage(number, null, message, null, null);
}
}
上面这个课我用来发送短信,并创建通知,我也有一个活动
public class Status extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
proKlasa pk = new proKlasa();
pk.sendSMS(getApplicationContext(), "1234", "pozz", "notification text");
}
}
当我启动我的应用程序时,它会自动发送一条消息,这项工作很棒。但是当我想在我的小部件中执行此操作时,我遇到了问题(NullPointerException)。我认为这是因为getApplicationContext();但我现在不知道如何解决它。我的小部件就像这样下来
public class Widget extends AppWidgetProvider
{
public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds)
{
remoteViews = new RemoteViews(context.getPackageName(), R.layout.widget);
proKlasa pk = new proKlasa();
pk.sendSMS(context, "1234", "pozz", "notification text");
appWidgetManager.updateAppWidget(appWidgetIds, remoteViews);
}
}
不,不是因为remoteViews或appWidgetManager,这只是一个例子。我有一个完全工作的小部件,但当我把这个代码(pk.sendSMS ...)停止时,任何人都知道问题可能在哪里?
编辑:问题解决了,我不得不把这个
proKlasa pk = new proKlasa();
pk.sendSMS(context, "1234", "pozz", "notification text");
在onReceive中,并在onUpdate中放了一些会触发te onReceive方法的setOnClickPendingIntent,不知道为什么会这样,并且不在onUpdate中,但是对我来说没问题:))