AlarmManager.setRepeating()上的NullPointerException - ElapsedRealTime - Android

时间:2014-07-27 11:31:35

标签: android sdk nullpointerexception background-service elapsedtime

我正在开发一个Android应用程序,每隔5秒就开启/关闭wifi(5秒仅用于测试) 如果此信息有帮助:我正在为此应用程序使用NavigationDrawer。我正在使用elapsedRealTime警报。用户从下拉列表中选择时间,在所选时间之后,应用程序打开/关闭wifi。例如:当他(用户)从下拉列表中选择30分钟(微调器)时,应该在30分钟后打开/关闭wifi。我认为我已经实施了所有内容(例如BroadcastReceiverIntentService)。

我的代码如果有帮助的话。 以下是我调用SpinnerTimeOnItemSelectedListener课程的方法。

public class TweaksFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    view = inflater.inflate(R.layout.fragment_tweaks, container, false);
    addListenerOnSpinnerItemSelection();
    return view;
}

public void addListenerOnSpinnerItemSelection(){
    spinnerSelectTime = (Spinner) view.findViewById(R.id.spinner_select_time);
    spinnerSelectTime.setOnItemSelectedListener(new SpinnerTimeOnItemSelectedListener());
}

SpinnerTimeOnItemSelectedListener我正在创建闹钟并在闹钟后执行BroadCastReceiver

   public class SpinnerTimeOnItemSelectedListener extends Activity implements AdapterView.OnItemSelectedListener {

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {

        if(parent.getItemAtPosition(position).toString().equals("Zeit auswählen") || parent.getItemAtPosition(position).toString().equals("Select Time")){
            //onNothingSelected(parent);
Toast.LENGTH_SHORT).show();
        } else if (parent.getItemAtPosition(position).toString().equals("30min")){
            alarm(parent);
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    public void alarm(AdapterView<?> parent) {
        try {
            Toast.makeText(parent.getContext(), "alarm() is called", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, BroadCastReceiver.class);
            PendingIntent mAlarmSender = PendingIntent.getBroadcast(this, 0, intent, 0);
            long firstTime = SystemClock.elapsedRealtime(); 
            AlarmManager am = (AlarmManager) getSystemService(ALARM_SERVICE);
            am.setRepeating(AlarmManager.ELAPSED_REALTIME,
                    firstTime, 10, mAlarmSender);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我的BroadCastReceiver班级

public class BroadCastReceiver extends WakefulBroadcastReceiver {


@Override
public void onReceive(Context context, Intent intent) {
    Toast.makeText(context, "BroadCastReceiver is running", Toast.LENGTH_SHORT).show();
    startWakefulService(context,new Intent(context, BackgroundService.class));
}

}

我的BackgroundService班级

public class BackgroundService extends IntentService {

public BackgroundService() {
    super("BackgroundService");
}
private WifiManager wifiManager;
@Override
protected void onHandleIntent(Intent intent) {
    // Hier kommt der Code zum ein und ausschalten (von z.B. wifi)
    // Ein Intentservice gibt nichts auf der UI aus
    wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);
    if (wifiManager.isWifiEnabled()) {
        wifiManager.setWifiEnabled(false);
    } else {
        wifiManager.setWifiEnabled(true);
    }
}
}

我的清单文件

    <?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.selfmade.ali.wifionoffer" >
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>

    <activity android:name=".SpinnerTimeOnItemSelectedListener" ></activity>
    <activity android:name=".ElapsedRealtimeAlarm"></activity>

    <receiver android:name=".BroadCastReceiver" ></receiver>
    <service android:name=".BackgroundService" android:exported="false" android:enabled="true" ></service>
</application>
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />

我在这一行上变成了NullPointerException:am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 10000, mAlarmSender);可能是什么问题,我该如何解决? 如果它有帮助,这是logcat:

07-27 15:24:29.490  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ java.lang.NullPointerException
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at com.selfmade.ali.wifionoffer.SpinnerTimeOnItemSelectedListener.alarm(SpinnerTimeOnItemSelectedListener.java:93)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at com.selfmade.ali.wifionoffer.SpinnerTimeOnItemSelectedListener.onItemSelected(SpinnerTimeOnItemSelectedListener.java:38)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.widget.AdapterView.fireOnSelected(AdapterView.java:956)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.widget.AdapterView.access$200(AdapterView.java:49)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:920)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:733)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:95)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.os.Looper.loop(Looper.java:157)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5356)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:515)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
07-27 15:24:29.500  20446-20446/com.selfmade.ali.wifionoffer W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-27 15:24:51.692  20446-20446/com.selfmade.ali.wifionoffer W/IInputConnectionWrapper﹕ getExtractedText on inactive InputConnection
07-27 15:24:51.692  20446-20446/com.selfmade.ali.wifionoffer W/IInputConnectionWrapper﹕ getTextBeforeCursor on inactive InputConnection
07-27 15:24:51.702  20446-20446/com.selfmade.ali.wifionoffer W/IInputConnectionWrapper﹕ getSelectedText on inactive InputConnection
07-27 15:24:51.702  20446-20446/com.selfmade.ali.wifionoffer W/IInputConnectionWrapper﹕ getTextAfterCursor on inactive InputConnection

我有这个问题3天了,我希望有人可以帮助我

0 个答案:

没有答案