您好,我在活动中有以下代码:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    LocationManager lm = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    NamazTimesManager nt = NamazTimesManager.getInstance(lm);
            ...
   }
}


getInstance()是普通Java类(与活动相同的程序包)中的静态方法:

public static NamazTimesManager getInstance(LocationManager lm) {
    if (instance == null) {
        instance = new NamazTimesManager(lm);
    }
    return instance;
}


我已经调试了所有内容,但他从未尝试过使用静态方法。相反,我得到一些找不到的源,类加载器等。异常。 LocationManager也已正确初始化。...我不知道该怎么办。请帮忙!

我得到以下例外:

02-09 11:51:34.463: W/dalvikvm(12270): threadid=1: thread exiting with uncaught exception (group=0x41f9a600)
02-09 11:51:34.533: E/AndroidRuntime(12270): FATAL EXCEPTION: main
02-09 11:51:34.533: E/AndroidRuntime(12270): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.khuddam.namazapp/de.khuddam.namazapp.MainActivity}: java.lang.NullPointerException
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread.access$600(ActivityThread.java:151)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1330)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.os.Handler.dispatchMessage(Handler.java:99)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.os.Looper.loop(Looper.java:155)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread.main(ActivityThread.java:5536)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at java.lang.reflect.Method.invokeNative(Native Method)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at java.lang.reflect.Method.invoke(Method.java:511)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1074)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:841)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at dalvik.system.NativeStart.main(Native Method)
02-09 11:51:34.533: E/AndroidRuntime(12270): Caused by: java.lang.NullPointerException
02-09 11:51:34.533: E/AndroidRuntime(12270):    at de.khuddam.namazapp.NamazTimesManager.<init>(NamazTimesManager.java:41)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at de.khuddam.namazapp.NamazTimesManager.getInstance(NamazTimesManager.java:29)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at de.khuddam.namazapp.MainActivity.onCreate(MainActivity.java:44)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.Activity.performCreate(Activity.java:5066)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1102)
02-09 11:51:34.533: E/AndroidRuntime(12270):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2288)
02-09 11:51:34.533: E/AndroidRuntime(12270):    ... 11 more


构造函数:

protected NamazTimesManager(LocationManager lm) {
    Location location = lm.getLastKnownLocation(LocationManager.GPS_PROVIDER);
    double longitude = location.getLongitude();
    double latitude = location.getLatitude();

    LatitudeLongitude ll = new LatitudeLongitude(latitude, longitude);
    TimeZone gmt = TimeZone.getTimeZone("Europe/London");
    Calendar cal = Calendar.getInstance();
    boolean dst = false;

    sunriseAPI = Sun.sunriseTime(cal, ll, gmt, dst);
    sunsetAPI = Sun.sunsetTime(cal, ll, gmt, dst);

    String sunriseString = sunriseAPI.getHours() + ":" + sunriseAPI.getMinutes();
    String sunsetString = sunsetAPI.getHours() + ":" + sunsetAPI.getMinutes();
    SimpleDateFormat format = new SimpleDateFormat("HH:mm");

    try {
        sunriseDate = format.parse(sunriseString);
        sunsetDate = format.parse(sunsetString);
    } catch (ParseException e) {
        e.printStackTrace();
    }
}

最佳答案

检查namazTimeManager的第41行,也许您在构造函数中的lm变量为null。您可能应该在使用输入之前对其进行测试。

附言:我尚未在发布前阅读评论,nickT可能有答案。

10-06 05:54