简体   繁体   English

在类和方法之间传递字符串

[英]Passing strings between classes and methods

I've been trying to pass a string from a method in one class to a method in another class, but can't seem to do it. 我一直在尝试将字符串从一个类中的方法传递给另一个类中的方法,但似乎无法做到这一点。

I've been trying a couple of things like 我一直在尝试一些类似的事情

String location = Latitude + "," + Longitude;
AccelOrientExample GPSstring = new AccelOrientExample();
GPSstring.onSensorChanged(location);

where location is the string I want to pass to the class AccelOrientExample, where I have a method with a arguement for location. 其中location是要传递给类AccelOrientExample的字符串,其中我有一个关于位置的争论的方法。

Please help, thanks 请帮忙,谢谢

Cannot instantiate the type AccelOrientExample is the first error I get which is on the new side. 无法实例化类型AccelOrientExample是我遇到的第一个错误,它是新问题。 And The method onSensorChanged(SensorEvent) in the type SensorEventListener is not applicable for the arguments (String) on the second line. 而且SensorEventListener类型的onSensorChanged(SensorEvent)方法不适用于第二行的参数(字符串)。

public void onSensorChanged(SensorEvent sensorEvent, String location)

Is the method in the AccelOrientExample 是AccelOrientExample中的方法吗

Cannot instantiate the type AccelOrientExample is the first error I get which is on the new side. 无法实例化类型AccelOrientExample是我遇到的第一个错误,它是新问题。

I think your AccelOrientExample is an Abstract class. 我认为您的AccelOrientExample是一个Abstract类。 you cant instantiate your Abstract class with new operator. 您不能使用new运算符实例化Abstract类。 you will have to let your subclass(concrete class) to initialize it. 您将必须让您的子类(具体类)对其进行初始化。

like lets assume AccelOrientedExampleImpl is the subclass of AccelOrientedExample class. 假设让我们假设AccelOrientedExampleImpl是AccelOrientedExample类的子类。

then , 然后 ,

           AccelOrientExample GPSstring  = new AccelOrientedExampleImpl(); 

The method onSensorChanged(SensorEvent) in the type SensorEventListener is not applicable for the arguments (String) on the second line. SensorEventListener类型的onSensorChanged(SensorEvent)方法不适用于第二行的参数(String)。

your method signature takes two arguments, SensorEvent instance and a String. 您的方法签名带有两个参数,SensorEvent实例和一个String。

public void onSensorChanged(SensorEvent sensorEvent, String location)

you are trying to pass only String instance.you have to pass both as your method expects two arguments. 您尝试仅传递String实例。您必须同时传递两个,因为您的方法需要两个参数。 do something like below, i am assuming the SensorEvent is a concrete class. 做下面的事情,我假设SensorEvent是一个具体的类。

String location = Latitude + "," + Longitude;
AccelOrientExample GPSstring = new AccelOrientExample();
GPSstring.onSensorChanged(new SensorEvent(), location);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM