Java 在类(不是活动)之间传递数据会导致nullpointerexception
我有一个活动Java 在类(不是活动)之间传递数据会导致nullpointerexception,java,android,class,Java,Android,Class,我有一个活动ShowList,其中有一些edittext字段和按钮。 我有一个按钮“获取位置”,它通过GPS获取当前位置 ShowList活动(工作正常,我可以获取位置): 我有一个自定义适配器: public class myAdapter extends BaseAdapter{ ShowList locationclass=new ShowList(); ..... TextView Location = (TextView
ShowList
,其中有一些edittext
字段和按钮。
我有一个按钮
“获取位置
”,它通过GPS
获取当前位置
ShowList活动
(工作正常,我可以获取位置
):
我有一个自定义适配器:
public class myAdapter extends BaseAdapter{
ShowList locationclass=new ShowList();
.....
TextView Location = (TextView) convertView.findViewById(R.id.text_location);
String lat=Double.toString(locationclass.gps.getLatitude());
String lo=Double.toString(locationclass.gps.getLongitude());
String coordinates="Lat: " + lat + " Long: " + lo;
Location.setText(coordinates);
我在的“String lat..”中收到空指针
那么,如何将位置
从显示列表活动
传递到适配器
?考虑到显示列表
是一项活动,您很可能实现了方法onCreate
来执行所有初始化。但是,当使用new ShowList()
创建类时,不会调用onCreate
方法,而是调用默认构造函数ShowList
。我怀疑您尚未定义它,因此您的gps
成员变量为null
-导致您的NPE。考虑到ShowList
是一项活动,您很可能实现了方法onCreate
来执行所有初始化。但是,当使用new ShowList()
创建类时,不会调用onCreate
方法,而是调用默认构造函数ShowList
。我怀疑您尚未定义它,因此您的gps
成员变量为null
-导致您的NPE。您不应将活动实例创建为
ShowList locationclass=new ShowList();
相反,使用您希望传递为的参数调用适配器
MyAdapter myAdapter = new MyAdaper(latitude, longitude);
并相应地更改MyAdapter
的构造函数
请注意:不要以小写字母开头类名。遵循正确的命名约定
编辑:(回复OP的评论)
如果要在activity ShowList.java中创建适配器类的对象,请将调用更改为:
MyAdapter theAdapter = new MyAdaper(latitude, longitude);
并将classMyAdapter.java
更改如下:
public class myAdapter extends BaseAdapter{
Strig latitude, longitude;
public MyAdapter(String lat, String long)
{
this.latitude=lat;
this.longitude = long;
}
TextView Location = (TextView) convertView.findViewById(R.id.text_location);
String lat=Double.toString(latitude);
String lo=Double.toString(longitude);
String coordinates="Lat: " + lat + " Long: " + lo;
Location.setText(coordinates);
您不应将活动的实例创建为
ShowList locationclass=new ShowList();
相反,使用您希望传递为的参数调用适配器
MyAdapter myAdapter = new MyAdaper(latitude, longitude);
并相应地更改MyAdapter
的构造函数
请注意:不要以小写字母开头类名。遵循正确的命名约定
编辑:(回复OP的评论)
如果要在activity ShowList.java中创建适配器类的对象,请将调用更改为:
MyAdapter theAdapter = new MyAdaper(latitude, longitude);
并将classMyAdapter.java
更改如下:
public class myAdapter extends BaseAdapter{
Strig latitude, longitude;
public MyAdapter(String lat, String long)
{
this.latitude=lat;
this.longitude = long;
}
TextView Location = (TextView) convertView.findViewById(R.id.text_location);
String lat=Double.toString(latitude);
String lo=Double.toString(longitude);
String coordinates="Lat: " + lat + " Long: " + lo;
Location.setText(coordinates);
您不能通过new
实例化活动。否则,您应该传递上下文;构造函数中适配器的纬度和经度:
public class myAdapter extends BaseAdapter{
private String latitude;
private String longitude;
private ArrayList<Item> items;
private Context context; // TODO: use it for layoutInflater
//the constructor
public myAdapter(Context context, ArrayList<myItems> list, String latitude, String longitude) {
this.items = list;
this.latitude = latitude;
this.longitude = longitude;
this.context = context;
}
.....
TextView Location = (TextView) convertView.findViewById(R.id.text_location);
String coordinates="Lat: " + this.latitude + " Long: " + this.longitude;
Location.setText(coordinates);
公共类myAdapter扩展BaseAdapter{
私有字符串纬度;
私有字符串经度;
私有ArrayList项;
私有上下文;//TODO:将其用于LayoutFlater
//构造器
公共myAdapter(上下文上下文、ArrayList列表、字符串纬度、字符串经度){
此项=列表;
这个。纬度=纬度;
这个经度=经度;
this.context=上下文;
}
.....
TextView位置=(TextView)convertView.findViewById(R.id.text\u位置);
字符串坐标=“Lat:”+this.lation+“Long:”+this.longitude;
位置.setText(坐标);
您不能通过new
实例化活动
。否则,您应该将上下文、纬度和经度传递给构造函数中的适配器
:
public class myAdapter extends BaseAdapter{
private String latitude;
private String longitude;
private ArrayList<Item> items;
private Context context; // TODO: use it for layoutInflater
//the constructor
public myAdapter(Context context, ArrayList<myItems> list, String latitude, String longitude) {
this.items = list;
this.latitude = latitude;
this.longitude = longitude;
this.context = context;
}
.....
TextView Location = (TextView) convertView.findViewById(R.id.text_location);
String coordinates="Lat: " + this.latitude + " Long: " + this.longitude;
Location.setText(coordinates);
公共类myAdapter扩展BaseAdapter{
私有字符串纬度;
私有字符串经度;
私有ArrayList项;
私有上下文;//TODO:将其用于LayoutFlater
//构造器
公共myAdapter(上下文上下文、ArrayList列表、字符串纬度、字符串经度){
此项=列表;
这个。纬度=纬度;
这个经度=经度;
this.context=上下文;
}
.....
TextView位置=(TextView)convertView.findViewById(R.id.text\u位置);
字符串坐标=“Lat:”+this.lation+“Long:”+this.longitude;
位置.setText(坐标);
首先:
我们不会像这样在android中创建活动
ShowList locationclass=new ShowList();
操作系统通过其提供的委托(如startActivity和startActivityForResult)创建活动实例。您必须使用同一实例来承载数据,并传递从同一引用读取的数据
在您的情况下,适配器需要携带原始活动引用
其次
GPS位置跟踪有时需要几秒钟到几分钟。有时,它永远不会给你位置更新,这取决于你的设备是否可以使用开放的天空。还取决于其他一些环境因素
因此,只有在位置管理器通知您的活动并提供位置更新后,您才应该调用适配器显示位置。但在此之前,您必须请求位置更新。一旦获得位置,请取消获取进一步更新的请求
你需要在谷歌上搜索更多关于GPS在安卓系统中的工作原理以及与之相关的最佳实践。
希望它能帮助你首先:
我们不会像这样在android中创建活动
ShowList locationclass=new ShowList();
操作系统通过其提供的委托(如startActivity和startActivityForResult)创建活动实例。您必须使用同一实例来承载数据,并传递从同一引用读取的数据
在您的情况下,适配器需要携带原始活动引用
其次
GPS定位跟踪需要几秒钟才能完成