android sdk nullpointerexception background-service elapsedtime

NullPointerException en AlarmManager.setRepeating()-ElapsedRealTime-Android



sdk background-service (0)

Estoy desarrollando una aplicación para Android, que enciende / apaga el wifi después de cada 5 segundos (5 segundos son solo para probar) Si esta información ayuda: Estoy usando un NavigationDrawer para esta aplicación. Estoy usando la alarma elapsedRealTime para esto. El usuario selecciona la hora de un menú desplegable y después del tiempo seleccionado, la aplicación activa / desactiva wifi. Por ejemplo: cuando él (el usuario) selecciona 30 minutos desde el menú desplegable (spinner), el wifi se debe encender / apagar después de 30 minutos. Creo que tengo todo implementado (como BroadcastReceiver , IntentService ).

Mi código si ayuda. Así es como llamo a mi clase 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()); }

En SpinnerTimeOnItemSelectedListener estoy creando la alarma y ejecutando BroadCastReceiver después de la alarma

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(); } } }

Mi clase 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)); }

}

Mi clase 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); } } }

Mi archivo Manifest

<?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" />

Me estoy convirtiendo en una NullPointerException en esta línea: am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 10000, mAlarmSender); ¿Cuál puede ser el problema y cómo puedo solucionarlo? Aquí está el logcat si ayuda:

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

Estoy teniendo este problema durante 3 días y espero que alguien me pueda ayudar