应用程序退出后为什么在服务中出现NullPointerException?

时间:2013-03-28 11:43:26

标签: android

我正在使用Phonegap创建基于webview的Android应用程序。为了帮助应用程序,我创建了一个服务,它基本上不时地获取用户的位置并处理它并保存它。

这就是:

  1. 我运行了该应用程序 - 我在MainActivity的startService()中进行了onCreate()调用。应用程序中没有其他活动(直到现在)。
  2. 服务运行,应用程序运行。我可以在LogCat中看到这一切。
  3. 现在,当我在应用程序的第一个屏幕上按回键时,应用程序退出,结果几秒钟后我看到LogCat中的堆栈跟踪以及应用程序已停止的消息。错误为NullPointerException
  4. 我在下面的方法中在指定的行中得到例外:

    public void GetAvailableLocation(){
        vstore = new VariableStorage(); //Even when I assigned new object to vstore
        if(vstore.load("mobileNumber").equals("0")) // Exception occures here
            return;
    
        // Get all available providers
        List<String> providers = locationManager.getAllProviders();
    
        for(String provider: providers) {
            Location newLocation = locationManager.getLastKnownLocation(provider);
        if(isBetter(newLocation, locationListener.location) 
                 && newLocation != null) {
                 locationListener.location = newLocation;
             }
        }
    }
    

    上述方法是在onCreate()服务中调用的第一种方法。

    请帮我解决这个问题。

    编辑:这是vstore中的加载方法 -

    public String load(String key){
    
        Log.d(TAG, "Load key: "+key);
        try{
    
            if(!loaded){
                this.loadFromFile();
            }
    
            String result = null;
    
            if(key.equals("loggedIn"))
                result = Boolean.toString(loggedIn);
            else if(key.equals("mobileNumber"))
                result = Long.toString(mobileNumber);
            else if(key.equals("password"))
                result = password;
            else if(key.equals("gettingService"))
                result = Boolean.toString(gettingService);
            else if(key.equals("providingService"))
                result = Boolean.toString(providingService);
            else if(key.equals("gettingServiceID"))
                result = Integer.toString(gettingServiceID);
            else if(key.equals("providingServiceTo"))
                result = Long.toString(providingServiceTo);
            else if(key.equals("usersName"))
                result = usersName;
            else if(key.equals("currLatitude"))
                result = Double.toString(currLatitude);
            else if(key.equals("currLongitude"))
                result = Double.toString(currLongitude);
            else if(key.equals("prevLatitude"))
                result = Double.toString(prevLatitude);
            else if(key.equals("prevLongitude"))
                result = Double.toString(prevLongitude);
            else if(key.equals("lastLocationUpdateTime"))
                result = Integer.toString(lastLocationUpdateTime);
            else if(key.equals("publicKey"))
                result = publicKey;
            else if(key.equals("notification"))
                result = Integer.toString(notification);
            else if(key.equals("verifyMobileNumber"))
                result = Long.toString(verifyMobileNumber);
    
            return result;
        }
        catch(Exception e){
            Log.d(TAG, "VSLoad Error: " + e.getMessage());
            return null;
        }
    }
    

2 个答案:

答案 0 :(得分:1)

确保vstore.load(“mobileNumber”)返回

或写下类似的内容:

if(vstore.load("mobileNumber") == null || vstore.load("mobileNumber").equals("0")) 
    return;

答案 1 :(得分:1)

这是编写该条件的更好方法:

if("0".equals(vstore.load("mobileNumber")))

始终给出“0”。因此,如果load返回null,您将调用return; 这称为null saved:)

相关问题