我想在设备启动完成时显示一个警报对话框。广播接收器没有问题,它工作正常。但是当启动完成时,没有打开的活动,因此我到NullPointerException
了这里。在这种情况下如何显示对话框?这是我用来显示对话框的代码:
public class RestartReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context, Intent intent)
{
AlertDialog alertHelp;
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
TextView m_timetext = new TextView(context );
m_timetext.setText("hello");
// m_timetext.setTextColor(getResources().getColor(R.color.dark_green));
LinearLayout linearLayout = new LinearLayout(context );
linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
linearLayout.setOrientation(1);
linearLayout.addView(m_timetext);
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}});
alertHelp = dialog.create();
alertHelp.setView(linearLayout);
alertHelp.show();
Log.d("In","Switched On");
}
}
请帮我。提前致谢。