2016-10-10 171 views
-3
public class Page3 extends Activity { 
double latitude; 
double longitude; 

/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page3); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
      .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    /* Use the LocationManager class to obtain GPS locations */ 
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

    LocationListener mlocListener = new MyLocationListener(); 
    try { 
     mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, mlocListener); 
    } catch (SecurityException e) { 
     e.printStackTrace(); 
    } 
} 

/* Class My Location Listener */ 
public class MyLocationListener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location loc) { 

     double a = loc.getLatitude(); 
     latitude=a; 

     double b = loc.getLongitude(); 
     longitude=b; 
     String Text = "My current location is: " + 
       "Latitud = " + loc.getLatitude() + 
       "Longitud = " + loc.getLongitude(); 


     String x = getCompleteAddressString(a, b); 
     TextView text = (TextView) findViewById(R.id.tv_address); 
     text.setText(x); 
    } 

现在,我想访问另一个类中的变量经度和纬度。这里是我需要访问这些变量的类。请注意:latitiude和经度在这个函数中正确设置,因为我得到了我的当前位置(我在这里粘贴了整个代码,因为这样做没有意义) 这里是我在类中写的代码,我想访问这些变量从另一个类访问一个类的变量

public class Page2 extends Activity { 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page2); 
    Button btn = (Button) findViewById(R.id.help); 
    Page3 a=new Page3(); 
    final double lati=a.latitude; 
    double longi=a.longitude; 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sendSMS("9740641023", "Help"+lati+""); 
     } 
    }); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 
} 

再次我没有复制粘贴整个code.This代码工作正常自己,但现在修改它发送消息“Help0.0”等根据我的纬度值应该改变到我目前的位置。请帮助我。

+1

提示:使用变量名称如a或b ...可能会节省5秒用于输入;但是为了理解它们的含义,每个读者花费几分钟和几分钟的时间。换句话说:使用**有意义的**名称。 **总是**。 – GhostCat

+0

据我可以从所示的片段中读取,你是“实例化一个新的'Page3'”,但是,它的'Object.property'a没有准备好被使用,并且你正在使用它。你的'MyLocationListener'没有时间去更新正确的变量。可以保存该值,使用Singleton保存正确的值,或者只在使用该值时使用该值。 – Bonatti

+0

@GhostCat下次会记住这一点。 –

回答

1

你的问题基本上是,一个实例被创建的方法:

LocationListener mLocListener = new MyLocationListener(); 

相反,你应该让一个字段级的

而且,如果你让一个公共静态场,那么其他类可以直接使用

LocationListener theListner = Classname.mLocListener; 

访问它,但是仅仅是一个非常“暴力方式”的做事。所以,你可以用它来看看你是否可以从那里取得进展;但事情是:直接访问来自其他类的静态字段是不好的做法;你应该避免它。

由于真正的教训是:这是非常基本的“java知识”。你现在应该从“android”退后一步;和研究这些基本的Java事物(如:“什么是访问其他对象中的信息的合理方式”)。否则,你会击中另一个墙壁!

然后,当你了解那些基础知识时;比你看看关于Android的书籍/教程,向你解释“Android世界”是如何工作的。因为Android有时使用非常特殊的方式来完成任务。

+0

如果我让LocationListener在本质上是公共静态的,那么我将不得不将所有方法都改为静态的。就是说你想说的是什么? –

+0

当然**不**。您可以从静态和“普通”方法访问**静态**字段 - 但不能以其他方式访问!并感谢我的观点:在编写程序之前,你应该真正**这样的东西。否则,你会从一个问题跑到下一个问题! – GhostCat

+0

Ofcoourse我知道这一点。我以为你想让locationListener类成为静态的,如果我这样做了,因为静态类只能访问静态方法,所以我必须使所有其他方法静态化。谢谢你 –

0

声明说variavle作为public static double lattitiue在First.java类

,现在你可以使用First.lattitude

0

良好的数据抽象得到任何类这个变量的值和封装允许只看到一类的客户班级允许客户看到什么。您的数据成员,如类别Page3中的变量经度和纬度,不应该由其他类直接访问。你应该有公共的getter(accessor)和setter(mutator)方法来限制对你的数据成员的“访问”,这些数据成员应该被声明为私有的。

您只能继承Java中的一个类,但您可以根据需要实现尽可能多的接口。因此,您不需要Page3类中的内部公共类MyLocationListener。只需使用实现关键字并覆盖接口的方法。

public class Page3 extends Activity implements LocationListener { // implement the interface instead of creating an inner class 
    private double latitude; // hide your data members from the client 
    private double longitude; 

/** 
* Called when the activity is first created. 
*/ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page3); 
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder() 
     .permitAll().build(); 
    StrictMode.setThreadPolicy(policy); 
    /* Use the LocationManager class to obtain GPS locations */ 
    LocationManager mlocManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 

try { 
    mlocManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); // pass in this referring to the current class since implementing the interface LocationListener 
} catch (SecurityException e) { 
    e.printStackTrace(); 
} 

@Override 
public void onLocationChanged(Location loc) { 

    setMyLatitude(loc.getLatitude()); // use mutator method to change value of your private data member 
    setMyLongitude(loc.getLongitude()); // use mutator method to change value of your private data member 
    String Text = "My current location is: " + 
      "Latitud = " + loc.getLatitude() + 
      "Longitud = " + loc.getLongitude(); 


    String x = getCompleteAddressString(a, b); 
    TextView text = (TextView) findViewById(R.id.tv_address); 
    text.setText(x); 
} 

public void setMyLatitude(double a) { 
    this.latitude = a; 
} 

public void setMyLongitude(double b) { 
    this.longitude = b; 
} 

public double getMyLatitude() { 
    return latitude; 
} 

public double getMyLongitude() { 
    return longitude; 
} 
} 

现在使用您的公共方法访问第二个活动中的数据成员。

public class Page2 extends Activity { 
/** 
* ATTENTION: This was auto-generated to implement the App Indexing API. 
* See https://g.co/AppIndexing/AndroidStudio for more information. 
*/ 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page2); 
    Button btn = (Button) findViewById(R.id.help); 
    Page3 a=new Page3(); 
    final double lati=a.getMyLatitude(); 
    double longi=a.getMyLongitude(); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      sendSMS("9740641023", "Help"+lati+""); 
     } 
    }); 
    // ATTENTION: This was auto-generated to implement the App Indexing API. 
    // See https://g.co/AppIndexing/AndroidStudio for more information. 
    client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build(); 

}

这是良好的编程习惯。一个监听器可以在两个活动之间进行通信,这可能是您不需要获得0.0的初始化double值所需要的。监听器应该在第2页的class中实现,并且在第3页的Page2中设置为Page2中的监听器。监听器将有一些方法来传递你想要的数据,或者告诉类Page2信息已经以某种方式被修改。

public class Page2 extends Activity implements DataListener { 
....... 
@Override 
public void someMethod() { 
    // do something with the data longitude and latitude as their values have changed 
} 
private GoogleApiClient client; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.page2); 
    Button btn = (Button) findViewById(R.id.help); 
    Page3 a=new Page3(); 
    a.setDataListener(this); // pass listener to the other class 
/* code in Page2 */ 
} 

public class Page3 extends Activity implements LocationListener { 
private DataListener myListener; 
/* code in Page3 */ 

@Override 
public void onLocationChanged(Location loc) { 

    setMyLatitude(loc.getLatitude()); // use mutator method to change value of your private data member 
    setMyLongitude(loc.getLongitude()); // use mutator method to change value of your private data member 
    myListener.someMethod(); // call this method to inform the other class that information has changed 
String Text = "My current location is: " + 
     "Latitud = " + loc.getLatitude() + 
     "Longitud = " + loc.getLongitude(); 


String x = getCompleteAddressString(a, b); 
TextView text = (TextView) findViewById(R.id.tv_address); 
text.setText(x); 
} 

public void setDataListener(DataListener listener) { 
    this.myListener = listener; 
} 

您也可以通过在经度和纬度,直接进入“的DataListener”法在第3页“的someMethod”,甚至不需要getter和setter和第3页类的私有数据成员。