【问题标题】:Project runs on first time but when I run it again It shows project has Stopped项目第一次运行,但当我再次运行它显示项目已停止
【发布时间】:2017-09-10 17:42:53
【问题描述】:

从这个问题主题可以看出,当在模拟器上运行我的程序时,它第一次运行完美,但是当我再次运行它而不做任何更改时,它显示“(项目名称)已停止工作。

我真的不知道如何解决这个问题,我尝试清理项目,再次构建 APK,但仍然没有解决方案。

请提供任何帮助,我们将不胜感激。

这是我的代码:

public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;

TextView latitude;
TextView longitude;
TextView accuracy;
TextView altitude;
TextView address;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    latitude = (TextView) findViewById(R.id.latitude);
    longitude = (TextView) findViewById(R.id.longitude);
    accuracy = (TextView) findViewById(R.id.accuracy);
    altitude = (TextView) findViewById(R.id.altitude);


    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    // Check if we already have a location access permission
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // this mean we don't have permissions, so we need to ask for it.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); // 1 here is some sort of id to identify this request, its called request code


    } else {
        // we do have permission

        // 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    }


    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {


            latitude.setText("Latitude: " + location.getLatitude());
            longitude.setText("Longitude: " + location.getLongitude());
            accuracy.setText("Accuracy: " + location.getAccuracy());
            altitude.setText("Alitiude: " + location.getAltitude());



        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };
}

@Override // this controls the dialog that asks user for location access permission
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    // check if user clicked on the dialog and choose allow.
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // if we have permission, then update user location. we need this check to make the code run!
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            // 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
        }
      }

   }
}

即使我复制此代码并将其放入新项目中,它也可以工作,但同样的问题,当我再次运行它时,它也不起作用。

我收到了这个错误

09-10 21:35:37.936 2588-2588/com.example.qubayel.test2 E/AndroidRuntime: 致命异常: main 进程:com.example.qubayel.test2,PID:2588 java.lang.RuntimeException:无法启动活动 ComponentInfo{com.example.qubayel.test2/com.example.qubayel.test2.MainActivity}:java.lang.IllegalArgumentException:无效的侦听器:null 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 在 android.app.ActivityThread.-wrap12(ActivityThread.java) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 在 android.os.Handler.dispatchMessage(Handler.java:102) 在 android.os.Looper.loop(Looper.java:154) 在 android.app.ActivityThread.main(ActivityThread.java:6119) 在 java.lang.reflect.Method.invoke(本机方法) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

【问题讨论】:

  • 可能你没有停止locationManageronDestroy 中添加locationManager.removeUpdates(locationListener);(覆盖它)
  • 第二次错误指向哪一行?
  • 请给我们错误信息。 developer.android.com/studio/profile/android-monitor.html。红色的是错误。
  • 我用错误信息更新了我的问题,谢谢。

标签: android


【解决方案1】:

我找到了解决方案,问题是我在定义它之前使用了 locationListener。在这行代码上:

 // 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

这个定义应该在我的 locationListener 调用之上:

 locationListener = new LocationListener() {
    @Override
    public void onLocationChanged(Location location) {


        latitude.setText("Latitude: " + location.getLatitude());
        longitude.setText("Longitude: " + location.getLongitude());
        accuracy.setText("Accuracy: " + location.getAccuracy());
        altitude.setText("Alitiude: " + location.getAltitude());



    }

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

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
};

最终代码应该是这样的:

public class MainActivity extends AppCompatActivity {
LocationManager locationManager;
LocationListener locationListener;

TextView latitude;
TextView longitude;
TextView accuracy;
TextView altitude;
TextView address;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    latitude = (TextView) findViewById(R.id.latitude);
    longitude = (TextView) findViewById(R.id.longitude);
    accuracy = (TextView) findViewById(R.id.accuracy);
    altitude = (TextView) findViewById(R.id.altitude);


    locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);
    locationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {


            latitude.setText("Latitude: " + location.getLatitude());
            longitude.setText("Longitude: " + location.getLongitude());
            accuracy.setText("Accuracy: " + location.getAccuracy());
            altitude.setText("Alitiude: " + location.getAltitude());



        }

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

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

        }
    };

    // Check if we already have a location access permission
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {

        // this mean we don't have permissions, so we need to ask for it.
        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 1); // 1 here is some sort of id to identify this request, its called request code


    } else {
        // we do have permission

        // 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
        locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);

    }
}


@Override // this controls the dialog that asks user for location access permission
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    super.onRequestPermissionsResult(requestCode, permissions, grantResults);

    // check if user clicked on the dialog and choose allow.
    if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {

        // if we have permission, then update user location. we need this check to make the code run!
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {

            // 0 , 0 mean always keep track of user location changes, we can change it to be every 10 seconds or something like that.
            locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
            }
        }

    }
}

【讨论】:

    猜你喜欢
    • 2021-01-15
    • 1970-01-01
    • 2011-09-29
    • 2021-06-30
    • 1970-01-01
    • 1970-01-01
    • 2022-12-17
    • 2018-05-12
    • 1970-01-01
    相关资源
    最近更新 更多