我想使用调用方法points()
的构造函数。此方法计算3点的经度和纬度。我需要在另一个类中使用这些值。因此,我试图在构造函数中调用该方法。
请帮我解释一下这个语法。
这是我的FirstActivity类
package com.example.gpsdistance;
import android.os.Bundle;
import android.os.Handler;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
public class FirstActivity extends Activity {
Button btnShowLocation, btnMinDis;
TextView display= null;
public float check= 5;
//private final Context mctx;
double latuser;
double longuser;
double dx1, dy1, delta_long1, delta_lat1, final_long1, final_lat1;
double dx2, dy2, delta_long2, delta_lat2, final_long2, final_lat2;
double dx3, dy3, delta_long3, delta_lat3, final_long3, final_lat3;
public float latA, longA, latB, longB, latC, longC, lati1;
// GPSTracker class
GPSTracker gps;
void points()
{
latuser = gps.getLatitude();
longuser = gps.getLongitude();
dx1 = 120*Math.cos(35);
dy1 = 120*Math.sin(35);
delta_long1 = dx1/(111320*Math.cos(latuser));
delta_lat1 = dy1/110540;
final_long1 = longuser + delta_long1;
final_lat1 = latuser + delta_lat1;
dx2 = 150*Math.cos(38);
dy2 = 150*Math.sin(38);
delta_long2 = dx2/(111320*Math.cos(latuser));
delta_lat2 = dy2/110540;
final_long2 = longuser + delta_long2;
final_lat2 = latuser + delta_lat2;
dx3 = 180*Math.cos(38);
dy3 = 180*Math.sin(38);
delta_long3 = dx3/(111320*Math.cos(latuser));
delta_lat3 = dy3/110540;
final_long3 = longuser + delta_long3;
final_lat3 = latuser + delta_lat3;
latA= (float)final_lat1;
longA= (float)final_long1;
latB= (float)final_lat2;
longB= (float)final_long2;
latC= (float)final_lat3;
longC= (float)final_long3;
}
private float computeDistance(double lat1, double lon1,
double lat2, double lon2) {
// Based on http://www.ngs.noaa.gov/PUBS_LIB/inverse.pdf
// using the "Inverse Formula" (section 4)
int MAXITERS = 20;
// Convert lat/long to radians
lat1 *= Math.PI / 180.0;
lat2 *= Math.PI / 180.0;
lon1 *= Math.PI / 180.0;
lon2 *= Math.PI / 180.0;
double a = 6378137.0; // WGS84 major axis
double b = 6356752.3142; // WGS84 semi-major axis
double f = (a - b) / a;
double aSqMinusBSqOverBSq = (a * a - b * b) / (b * b);
double L = lon2 - lon1;
double A = 0.0;
double U1 = Math.atan((1.0 - f) * Math.tan(lat1));
double U2 = Math.atan((1.0 - f) * Math.tan(lat2));
double cosU1 = Math.cos(U1);
double cosU2 = Math.cos(U2);
double sinU1 = Math.sin(U1);
double sinU2 = Math.sin(U2);
double cosU1cosU2 = cosU1 * cosU2;
double sinU1sinU2 = sinU1 * sinU2;
double sigma = 0.0;
double deltaSigma = 0.0;
double cosSqAlpha = 0.0;
double cos2SM = 0.0;
double cosSigma = 0.0;
double sinSigma = 0.0;
double cosLambda = 0.0;
double sinLambda = 0.0;
double lambda = L; // initial guess
for (int iter = 0; iter < MAXITERS; iter++) {
double lambdaOrig = lambda;
cosLambda = Math.cos(lambda);
sinLambda = Math.sin(lambda);
double t1 = cosU2 * sinLambda;
double t2 = cosU1 * sinU2 - sinU1 * cosU2 * cosLambda;
double sinSqSigma = t1 * t1 + t2 * t2; // (14)
sinSigma = Math.sqrt(sinSqSigma);
cosSigma = sinU1sinU2 + cosU1cosU2 * cosLambda; // (15)
sigma = Math.atan2(sinSigma, cosSigma); // (16)
double sinAlpha = (sinSigma == 0) ? 0.0 :
cosU1cosU2 * sinLambda / sinSigma; // (17)
cosSqAlpha = 1.0 - sinAlpha * sinAlpha;
cos2SM = (cosSqAlpha == 0) ? 0.0 :
cosSigma - 2.0 * sinU1sinU2 / cosSqAlpha; // (18)
double uSquared = cosSqAlpha * aSqMinusBSqOverBSq; // defn
A = 1 + (uSquared / 16384.0) * // (3)
(4096.0 + uSquared *
(-768 + uSquared * (320.0 - 175.0 * uSquared)));
double B = (uSquared / 1024.0) * // (4)
(256.0 + uSquared *
(-128.0 + uSquared * (74.0 - 47.0 * uSquared)));
double C = (f / 16.0) *
cosSqAlpha *
(4.0 + f * (4.0 - 3.0 * cosSqAlpha)); // (10)
double cos2SMSq = cos2SM * cos2SM;
deltaSigma = B * sinSigma * // (6)
(cos2SM + (B / 4.0) *
(cosSigma * (-1.0 + 2.0 * cos2SMSq) -
(B / 6.0) * cos2SM *
(-3.0 + 4.0 * sinSigma * sinSigma) *
(-3.0 + 4.0 * cos2SMSq)));
lambda = L +
(1.0 - C) * f * sinAlpha *
(sigma + C * sinSigma *
(cos2SM + C * cosSigma *
(-1.0 + 2.0 * cos2SM * cos2SM))); // (11)
double delta = (lambda - lambdaOrig) / lambda;
if (Math.abs(delta) < 1.0e-12) {
break;
}
}
float distance = (float) (b * A * (sigma - deltaSigma));
return distance;
}
FirstActivity()
{
points();
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.firstactivity);
final LinearLayout lView = new LinearLayout(this);
display= new TextView(this);
btnMinDis=new Button(this);
btnMinDis.setText("Nearest Point");
final Handler mHandler = new Handler();
btnShowLocation = (Button) findViewById(R.id.buttonLocation);
btnShowLocation.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0)
{
// TODO Auto-generated method stub
// create class object
gps = new GPSTracker(FirstActivity.this);
// check if GPS enabled
if(gps.canGetLocation())
{
points();
float initialdisA = computeDistance( latuser, longuser, latA, longA);
float initialdisB = computeDistance( latuser, longuser, latB, longB);
float initialdisC = computeDistance( latuser, longuser, latC, longC);
Toast.makeText(getApplicationContext(),"\nLatitude: " + latA , Toast.LENGTH_LONG).show();
lView.addView(display);
setContentView(lView);
display.setText("Initial Location is - \nLatitude: " + latuser + " N" + "\nLongitude: " + longuser + " E"
+ "\n\n\nPoint A:\n" + "Latitude: " + latA + "N" + "\nLongitude: " + longA + "E"
+ "\nPoint B:\n" + "Latitude: " + latB + "N" + "\nLongitude: " + longB + "E"
+ "\nPoint C:\n" + "Latitude: " + latC + "N" + "\nLongitude: " + longC + "E"
+ "\n\n\nInitial Distance A: " + initialdisA + "m" + "\nInitial Distance B: " + initialdisB + "m" + "\nInitial Distance C: " + initialdisC + "m");
mHandler.postDelayed(mLaunchTask,10000);
}
else
{
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
private Runnable mLaunchTask = new Runnable()
{
public void run()
{
Intent i = new Intent(getApplicationContext(),NearestPoint.class);
startActivity(i);
}
};
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.firstactivity, menu);
return true;
}
}
我尝试过使用这些:
1)
FirstActivity()
{
points();
}
2)
FirstActivity(Context context)
{
this.mContext = context;
points();
}
我遇到两个错误:“应用程序停止工作......”
这是我的logcat:
01-19 10:12:40.488: D/dalvikvm(608): newInstance failed: Lcom/example/gpsdistance/FirstActivity;.<init>() not accessible to Landroid/app/Instrumentation;
01-19 10:12:40.538: D/AndroidRuntime(608): Shutting down VM
01-19 10:12:40.578: W/dalvikvm(608): threadid=1: thread exiting with uncaught exception (group=0x40015560)
01-19 10:12:40.688: E/AndroidRuntime(608): FATAL EXCEPTION: main
01-19 10:12:40.688: E/AndroidRuntime(608): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.gpsdistance/com.example.gpsdistance.FirstActivity}: java.lang.IllegalAccessException: access to constructor not allowed
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.os.Handler.dispatchMessage(Handler.java:99)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.os.Looper.loop(Looper.java:123)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread.main(ActivityThread.java:3683)
01-19 10:12:40.688: E/AndroidRuntime(608): at java.lang.reflect.Method.invokeNative(Native Method)
01-19 10:12:40.688: E/AndroidRuntime(608): at java.lang.reflect.Method.invoke(Method.java:507)
01-19 10:12:40.688: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-19 10:12:40.688: E/AndroidRuntime(608): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-19 10:12:40.688: E/AndroidRuntime(608): at dalvik.system.NativeStart.main(Native Method)
01-19 10:12:40.688: E/AndroidRuntime(608): Caused by: java.lang.IllegalAccessException: access to constructor not allowed
01-19 10:12:40.688: E/AndroidRuntime(608): at java.lang.Class.newInstanceImpl(Native Method)
01-19 10:12:40.688: E/AndroidRuntime(608): at java.lang.Class.newInstance(Class.java:1409)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
01-19 10:12:40.688: E/AndroidRuntime(608): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
01-19 10:12:40.688: E/AndroidRuntime(608): ... 11 more
01-19 10:12:43.669: I/Process(608): Sending signal. PID: 608 SIG: 9
答案 0 :(得分:3)
您的积分功能包含此次呼叫
latuser = gps.getLatitude();
但是,gps
变量尚未初始化。只有在用户按下您添加到gps
事件中的活动的按钮时,才会创建onCreate
对象。
之前创建gps ...例如在points()
或创建一个将在整个课程中使用的函数,而不是gps
的简单调用。
function getGPS() {
if (gps == null)
gps = new GPSTracker(FirstActivity.this);
return gps;
}
答案 1 :(得分:0)
是的,这是可能的
class AdapterVersion {
AdapterVersion() {
show();
}
public void show(){
System.out.println("Hello");
}
}
class AdapterVersionNew {
AdapterVersionNew()
{
AdapterVersion a= new AdapterVersion();
}
public void show2(){
System.out.println("Hello2");
}
}
class test2 {
public static void main(String[] args) {
AdapterVersionNew q = new AdapterVersionNew();
}
}