if和return语句

时间:2018-06-07 13:30:01

标签: android nullpointerexception

我是Android编程的新手,现在我正在开发一个应用程序来提醒我讲课的时间表。例如,讲座从12:30开始,到下午1:30结束。该应用程序将在该日期通知我,并将我的手机转为静音模式。我的应用程序现在运行良好,但问题是在演讲结束后应用程序应将手机恢复到正常模式。 我的main_activity代码:

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ring(12,21,0);
    ring(12,30,1);

}
public void ring (int h,int m,int mode){
/** This intent invokes the activity DemoActivity, which in turn opens the AlertDialog window */
Intent i = new Intent("in.wptrafficanalyzer.servicealarmdemo.demoactivity");
    i.putExtra("mode",mode);

/** Creating a Pending Intent */
PendingIntent operation = PendingIntent.getActivity(getBaseContext(), 0, i, Intent.FLAG_ACTIVITY_NEW_TASK);

/** Getting a reference to the System Service ALARM_SERVICE */
AlarmManager alarmManager = (AlarmManager) getBaseContext().getSystemService(ALARM_SERVICE);



int year = Calendar.getInstance().get(Calendar.YEAR);
int month = Calendar.getInstance().get(Calendar.MONTH);
int day = Calendar.getInstance().get(Calendar.DAY_OF_MONTH);
int hour = h;
int minute = m;

/** Creating a calendar object corresponding to the date and time set by the user */
GregorianCalendar calendar = new GregorianCalendar(year,month,day, hour, minute);

/** Converting the date and time in to milliseconds elapsed since epoch */
long alarm_time = calendar.getTimeInMillis();

/** Setting an alarm, which invokes the operation at alart_time */
alarmManager.set(AlarmManager.RTC_WAKEUP  , alarm_time , operation);

/** Alert is set successfully */
Toast.makeText(getBaseContext(), "Alarm is set successfully",Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}

我的DemoActivity代码:

public class DemoActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {        
    super.onCreate(savedInstanceState); 

    /** Creating an Alert Dialog Window */
    AlertDemo alert = new AlertDemo();

    /** Opening the Alert Dialog Window */
    alert.show(getSupportFragmentManager(), "AlertDemo");
}
}

我的问题出在AlertDemo代码中:

public Dialog onCreateDialog(Bundle savedInstanceState) {
    am = (AudioManager) getActivity().getBaseContext().getSystemService(Context.AUDIO_SERVICE);

    Intent i = this.getActivity().getIntent();
    int info = this.getActivity().getIntent().getIntExtra("mode", 0);
    if (info == 0) {
        /** Turn Screen On and Unlock the keypad when this alert dialog is displayed */
        getActivity().getWindow().addFlags(LayoutParams.FLAG_TURN_SCREEN_ON | LayoutParams.FLAG_DISMISS_KEYGUARD);
        /** Creating a alert dialog builder */
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

        /** Setting title for the alert dialog */
        builder.setTitle("Alarm");

        /** Setting the content for the alert dialog */
        builder.setMessage("An Alarm by AlarmManager");
        Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_ALARM);
        final Ringtone ringtone = RingtoneManager.getRingtone(getContext(), uri);
        ringtone.play();
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                am.setRingerMode(AudioManager.RINGER_MODE_SILENT);
            }
        }, 300000);

        /** Defining an OK button event listener */
        builder.setPositiveButton("OK", new OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                /** Exit application on click OK */
                getActivity().finish();
                ringtone.stop();
            }
        });

        /** Creating the alert dialog window */
        return builder.create();
    }
    else
        am.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}

当我添加语句"返回null"时,问题缺少 return语句 出现此错误(应用程序已停止):

java.lang.RuntimeException: Unable to start activity ComponentInfo{in.wptrafficanalyzer.servicealarmdemo/in.wptrafficanalyzer.servicealarmdemo.DemoActivity}: java.lang.NullPointerException
                                                                                     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
                                                                                     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
                                                                                     at android.app.ActivityThread.access$600(ActivityThread.java:128)
                                                                                     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                                     at android.os.Looper.loop(Looper.java:137)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:4517)
                                                                                     at java.lang.reflect.Method.invokeNative(Native Method)
                                                                                     at java.lang.reflect.Method.invoke(Method.java:511)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:993)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:760)
                                                                                     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

如果返回null,则警报对象为null。然后你继续请求对这个对象执行“show”方法,因此空指针异常。如果您在“onCreate”方法中执行此操作,如果DemoActivity,您的活动无法启动。尝试先决定是否先显示对话框,之前要创建警报本身。然后只在模式合适时创建(并显示)警报(顺便说一句,Java有布尔值,你为什么使用整数?)。

答案 1 :(得分:0)

不要用 $spreadsheet = \PhpOffice\PhpSpreadsheet\IOFactory::load($upload_path); $worksheet = $spreadsheet->getActiveSheet(); foreach ($worksheet->getRowIterator() as $row) { $cellIterator = $row->getCellIterator(); print_r($cellIterator); $cellIterator->setIterateOnlyExistingCells(FALSE); foreach ($cellIterator as $cell) { $emailid = $cell->getValue(); } 方法做决策。从onCreateDialog

中移除if-else阻止
onCreateDialog

}