我在服务中创建了名为S.java的proximityAlarm
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
locPrefs = getSharedPreferences("locations", 0);
String stringArray = locPrefs.getString("ssidArray", "");
int locationCountP = locPrefs.getInt("locationCount", 0);
final ArrayList<String> wifis =
new ArrayList<String>(Arrays.asList(stringArray.split(",")));
if(locationCountP!=0){
if (wifis.contains(ssid)) {
if (prefs.getBoolean(PreferencesActivity.TOAST, false))
{
Toast.makeText(getBaseContext(), getString(R.string.wifiAlreadyAdded), Toast.LENGTH_SHORT).show();
}
} else {
addProxAlert(lat, lng, locationCountP);
wifis.add(ssid);
if (prefs.getBoolean(PreferencesActivity.TOAST, false))
{
Toast.makeText(getBaseContext(), getString(R.string.newWifiAdded), Toast.LENGTH_SHORT).show();
}
}
}
else {
addProxAlert(lat, lng, locationCountP);
Toast.makeText(getBaseContext(), getString(R.string.firstWifiAdded), Toast.LENGTH_SHORT).show();
}
}
public void addProxAlert(double lat, double lng, int id) {
Intent pIntent = new Intent(PROX_ALERT_INTENT);
Log.v("loc", Integer.toString(locationCountP-1));
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), id, pIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
lm.addProximityAlert(lat, lng, 20, -1, proximityIntent);
IntentFilter filter = new IntentFilter(PROX_ALERT_INTENT);
registerReceiver(new ProximityIntentReceiver(), filter);
}
但是我需要从名为A.java的活动中删除相同的proximityAlarm。
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.SharedPreferences;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
public class WifiList extends Activity {
private LocationManager lm;
private static final String PROX_ALERT_INTENT =
"dado.pack";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wifilist);
final Button delete = (Button) findViewById(R.id.deletewifi);
locPrefs = getSharedPreferences("locations", 0);
int locationCount = locPrefs.getInt("locationCount", 0);
final RadioGroup rg = (RadioGroup) findViewById(R.id.radioGroup1);
rg.setOrientation(RadioGroup.VERTICAL);
if(locationCount!=0){
String ssid = "";
Toast.makeText(getBaseContext(), Integer.toString(locationCount), Toast.LENGTH_SHORT).show();
for(int i=0;i<locationCount;i++){
ssid = locPrefs.getString("ssid"+i,"0");
Log.v("i", Integer.toString(i));
final RadioButton rb = new RadioButton(this);
rb.setLayoutParams (new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
rb.setText(ssid);
rb.setId(i);
rg.addView(rb, i);
}
}
delete.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int idbt = rg.getCheckedRadioButtonId();
Log.v("id", Integer.toString(idbt));
Intent pIntent = new Intent(PROX_ALERT_INTENT);
PendingIntent proximityIntent = PendingIntent.getBroadcast(getApplicationContext(), idbt, pIntent, Intent.FLAG_ACTIVITY_NEW_TASK);
lm.removeProximityAlert(proximityIntent);
}
});
}
lm.removeProximityAlert(proximityIntent)上的NullPointerException。
05-02 12:43:54.536: E/AndroidRuntime(32564): FATAL EXCEPTION: main
05-02 12:43:54.536: E/AndroidRuntime(32564): Process: dado.pack, PID: 32564
05-02 12:43:54.536: E/AndroidRuntime(32564): java.lang.NullPointerException
05-02 12:43:54.536: E/AndroidRuntime(32564): at dado.pack.WifiList$1.onClick(WifiList.java:64)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.view.View.performClick(View.java:4633)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.view.View$PerformClick.run(View.java:19330)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.os.Handler.handleCallback(Handler.java:733)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.os.Handler.dispatchMessage(Handler.java:95)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.os.Looper.loop(Looper.java:157)
05-02 12:43:54.536: E/AndroidRuntime(32564): at android.app.ActivityThread.main(ActivityThread.java:5356)
05-02 12:43:54.536: E/AndroidRuntime(32564): at java.lang.reflect.Method.invokeNative(Native Method)
05-02 12:43:54.536: E/AndroidRuntime(32564): at java.lang.reflect.Method.invoke(Method.java:515)
05-02 12:43:54.536: E/AndroidRuntime(32564): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
05-02 12:43:54.536: E/AndroidRuntime(32564): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
05-02 12:43:54.536: E/AndroidRuntime(32564): at dalvik.system.NativeStart.main(Native Method)
WHY ??
TNX !!!
答案 0 :(得分:0)
您写道:
lm.removeProximityAlert(proximityIntent)上的NullPointerException
因为lm
是null
。你永远不会初始化这个成员变量。