繁体   English   中英

在类(非活动)之间传递数据会导致nullpointerexception

[英]pass data between classes (not activities) results in nullpointerexception

我有一个活动ShowList ,其中有一些edittext字段和buttons 我有一个“ get locationbutton ,可通过GPS获取current location

ShowList activity (效果很好,我可以找到location ):

  public class ShowList extends Activity implements OnClickListener{


GPSTracker gps;   //i use GPSTracker to get the location (it is not an activity)
     .........

 case R.id.getlocation:
    // create class object
   gps = new GPSTracker(ShowList.this);

  // check if GPS enabled
 if(gps.canGetLocation()){

          double latitude = gps.getLatitude();
          double longitude = gps.getLongitude();

Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
                    }else{
                        ...
                    }

我有一个自定义适配器

    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 ...”中收到nullpointer

因此,如何将locationShowList activity传递到adapter

您无法通过new 实例化一个Activity 否则,您应该传递上下文; 构造函数中Adapter纬度和经度:

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);

您不应将活动实例创建为

ShowList locationclass=new ShowList();

而是使用您要传递的参数调用适配器

MyAdapter myAdapter = new MyAdaper(latitude, longitude);

并相应地更改MyAdapter的构造函数。

只需注意:请勿以小写字母开头课程名称。 请遵循正确的命名约定。

编辑:(回复OP的评论)

如果要在活动ShowList.java中创建适配器类的对象,则将调用更改为:

MyAdapter theAdapter = new MyAdaper(latitude, longitude);

然后将类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是一项活动,最有可能您实现了onCreate方法来进行所有初始化。 但是,当使用new ShowList()创建类时,不会调用onCreate方法,而是如果定义了默认构造函数ShowList则会调用该方法。 我怀疑您尚未定义它,因此您的gps成员变量为null导致您的NPE。

首先:

我们不会像这样在Android中创建Activity

ShowList locationclass=new ShowList();

Os通过其提供的委托(如startActivity和startActivityForResult)创建活动实例。 您必须使用相同的实例来承载数据,并从相同的引用传递数据读取。

并且在您的情况下,您的适配器需要携带该原始活动参考。

其次。

GPS位置跟踪有时需要几秒钟到几分钟。 有时,它永远不会为您提供位置更新,这取决于您的设备是否可用。 并取决于其他一些环境因素。

因此,只有在位置管理器通知您的活动并更新位置之后,才应调用适配器以显示位置。 不过,您必须在此之前请求位置更新。 并且一旦您获得位置。 取消获取进一步更新的请求。

您需要更多地谷歌搜索GPS在Android中的工作方式以及与之相关的最佳做法。 希望能帮助到你

暂无
暂无

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

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