Android locationManager.RequestLocationUpdate挂起

Android locationManager.RequestLocationUpdate挂起,android,gps,Android,Gps,给定以下简单类: public class MainActivity extends ActionBarActivity { TextView tv; protected void makeUseOfNewLocation(Location location) { tv.setText("test"); } @Override protected void onCreate(Bundle savedInstanceState) {

给定以下简单类:

 public class MainActivity extends ActionBarActivity {

   TextView tv;

   protected void makeUseOfNewLocation(Location location)
   {
      tv.setText("test");
   }

   @Override
   protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);

     // Acquire a reference to the system Location Manager
     LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);

     // Define a listener that responds to location updates
     LocationListener locationListener = new LocationListener() {
        public void onLocationChanged(Location location) {
            // Called when a new location is found by the network location provider.
            makeUseOfNewLocation(location);
        }

        public void onStatusChanged(String provider, int status, Bundle extras) {}

        public void onProviderEnabled(String provider) {}

        public void onProviderDisabled(String provider) {}
    };

    // Register the listener with the Location Manager to receive location updates
    locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);


    tv=new TextView(this);
    LinearLayout lyo=new LinearLayout(this);
    Button btn=new Button(this);
    btn.setText("aaa");
    lyo.addView(btn);
    lyo.addView(tv);
    setContentView(lyo);

}
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener)在调试时挂起,在运行应用程序时挂起:

Process: com.example.social.gps, PID: 964
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.social.gps/com.example.social.gps.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        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:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.example.social.gps.MainActivity.onCreate(MainActivity.java:28)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
        ....
知道为什么吗

我的朋友们:

  <uses-permission
    android:name="android.permission.ACCESS_FINE_LOCATION" />


哪一行是您的第28行?stacktrace告诉您错误在哪里MainActivity.java类第28行。第28行是注释
//定义一个响应位置更新的侦听器
首先,您的
LocationListener
的超时设置为零-这意味着您将获得有关位置的恒定信息。其次,由于您正在主UI线程上运行
LocationListener
,因此它将挂起整个应用程序。因此,您会得到
NullPointerException
,因为UI没有足够的时间来创建视图、布局或按钮。但是挂起位置在
locationManager.RequestLocationUpdate(locationManager.NETWORK\u提供程序,0,0,locationListener)之前