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);
并将class
MyAdapter.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);
并将class
MyAdapter.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定位跟踪需要几秒钟才能完成