我一直遇到问题让我的应用程序按照我的意图运行。首先我尝试将所有代码塞进一个活动中,但随着我的应用程序变得越来越复杂,我决定需要减轻特定活动的负担。所以我创建了一个带有活页夹的服务,并且从绑定器中打破了getfromSP方法(每当我从我的主要活动中调用它时获取Nullpointer),但无论我刚刚刷掉它。
现在我尝试创建一个服务,在启动时由广播接收器启动后重新安排一些警报,但是每次调用mShared.alerm时,我的主要活动上都会出现Null指针错误,工作正常。所以我试过使它成为一个简单的活动,并试图隐藏UI,但问题仍然存在,而主要活动工作peachy(除了getSP方法)。
有些日子我无法找到错误发生的原因,所以任何人都可以帮助确定错误发生在getfromSP和rpr的类mShared.getfromSP上的原因吗?
另外一个附带问题:与最基本的服务类型类相比,没有用户界面的Activity会影响性能多少?
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import com.App.pc.app.Sharedpees.MyLocalBinder;
public class rpr extends AppCompatActivity{
Sharedpees mShared;
boolean isBound=false;
private ServiceConnection sharedpref =new ServiceConnection() {
@Override
public void onServiceConnected(ComponentName name, IBinder service) {
Sharedpees.MyLocalBinder binder =(Sharedpees.MyLocalBinder) service;
mShared=binder.getService();
isBound=true;
}
@Override
public void onServiceDisconnected(ComponentName name) {
isBound=false;
}
};
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent f=new Intent(this, Sharedpees.class);
bindService(f,sharedpref, Context.BIND_AUTO_CREATE);
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PF", android.content.Context.MODE_PRIVATE);
if (preferences.getBoolean("conrtacts", false)){
String checker = "231";
int rc = 1;
mShared.alerm(this, checker, rc);
}
if (preferences.getBoolean("message", false)){
String checker = "232";
int rc = 2;
mShared.alerm(this, checker, rc);
}
if (preferences.getBoolean("clients", false)){
String checker = "233";
int rc = 3;
mShared.alerm(this, checker, rc);
}
}
}
这是我的Binder
import android.app.AlarmManager;
import android.app.Application;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.os.Binder;
public class Sharedpees extends Service {
private final IBinder myBinder = new MyLocalBinder();
@Override
public IBinder onBind(Intent arg0) {
// TODO Auto-generated method stub
return myBinder;
}
public class MyLocalBinder extends Binder {
Sharedpees getService() {
return Sharedpees.this;
}
}
public void saveInSp(String key, boolean value) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PF", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean(key, value);
editor.commit();
}
public boolean getFromSP(String key) {
SharedPreferences preferences = getApplicationContext().getSharedPreferences("PF", android.content.Context.MODE_PRIVATE);
return preferences.getBoolean(key, false);
}
public void alerm(Context c,String valuew,int lalu){
AlarmManager alarm;
PendingIntent pintent;
Intent intent = new Intent(c, Lookout.class);
intent.putExtra("key",valuew);
alarm = (AlarmManager) c.getSystemService(Context.ALARM_SERVICE);
pintent = PendingIntent.getService(c, lalu, intent, 0);
alarm.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), 10 * 1000, pintent);
}
public void alermoff(Context b,String valuew,int lalu){
AlarmManager alarm;
PendingIntent pintent;
Intent intent = new Intent(b, Lookout.class);
intent.putExtra("key",valuew);
alarm = (AlarmManager) this.getSystemService(Context.ALARM_SERVICE);
pintent = PendingIntent.getService(b, lalu, intent, 0);
alarm.cancel(pintent);
}
}
来自mShared的崩溃日志
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.App.pc.app/com.App.pc.app.rpr}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.App.pc.app.rpr.onCreate(rpr.java:51)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
调用mShared.getFromSP时,主活动上的getFromSP崩溃日志(" conrtacts"));
FATAL EXCEPTION: main
Process: com.App.pc.app, PID: 8181
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.App.pc.app/com.App.pc.app.About}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.App.pc.app.About.onCreate(About.java:75)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5146)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
at dalvik.system.NativeStart.main(Native Method)