问题
I am developing an android app, which turns the wifi on/off after every 5seconds (5seconds are just for testing)
If this information helps: I am using a NavigationDrawer for this app. I am using elapsedRealTime alarm for this. The user selects the time from a dropdown and after the selected time the app turns wifi on/off. For example: When he (the user) selects 30 minutes from the dropdown (spinner), the wifi should be turned on/off after 30minutes. I think that I have everything implemented (like BroadcastReceiver
, IntentService
).
My Code if it helps.
Here is how I call my SpinnerTimeOnItemSelectedListener
class.
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());
}
In SpinnerTimeOnItemSelectedListener
I am creating the alarm and executing the BroadCastReceiver
after the alarm
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();
}
}
}
My BroadCastReceiver
class
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));
}
}
My BackgroundService
class
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);
}
}
}
My Manifest file
<?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" />
I am becoming a NullPointerException on this line: am.setRepeating(AlarmManager.ELAPSED_REALTIME, firstTime, 10000, mAlarmSender);
What can be the issue and how can I solve it?
Here is the logcat if it helps:
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
I am having this issue for 3 days and I hope that somebody can help me
来源:https://stackoverflow.com/questions/24980487/nullpointerexception-on-alarmmanager-setrepeating-elapsedrealtime-android