在类之间传递数据(不是活动)导致nullpointerexception
我有一个活动ShowList
其中我有一些edittext
字段和buttons
。 我有button
“get location
”,它得到current location
和GPS
。在类之间传递数据(不是活动)导致nullpointerexception
的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);
我收到nullpointer
在“字符串纬度。 ..“。
那么,如何将location
从ShowList activity
传递到adapter
?
无法通过实例化的。否则,你应该通过上下文;经纬向您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);
:问题是,我使用公共myAdapter(上下文上下文, ArrayList
只是经纬度添加到您的适配器的构造,看到我的编辑 – Houcine 2013-04-08 11:37:32
:是的,我做到了,程序运行,但我得到的值在MainAcivity myAdapter myAdapterItems =新myAdapter( \t \t MainActivity null.I使用.this,myList); \t \t myListView.setAdapter(myAdapterItems);当我尝试:myAdapter myAdapterItems = new myAdapter( \t \t MainActivity.this,myList,lat,lo); \t \t myListView.setAdapter(myAdapterItems); (我初始化拉特,罗)我拿空。 – George 2013-04-08 11:39:28
考虑到ShowList
是一项活动,很可能您实施了方法onCreate
来完成所有初始化。但是,当您使用new ShowList()
创建类时,将不会调用onCreate
方法,而是在定义一个默认构造函数ShowList
时调用该方法。我怀疑你没有定义它,因此你的gps
成员变量是null
- 导致你的NPE。
你不应该创建活动的实例作为
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);
首先:
我们不会在Android中创建活动像这样
ShowList locationclass=new ShowList();
O,通过它像startActivity提供委托创建活动实例和startActivityForResult。您必须使用相同的实例来传输数据,并传递来自同一参考的数据读取。
而在您的情况下,您的适配器需要携带原始活动参考。其次,
GPS定位追踪有时需要几秒到几分钟的时间。有时它永远不会根据您的设备是否可以使用开放天空来提供位置更新。并取决于其他一些环境因素。
因此,只有在位置更新的位置管理器通知您的活动后,才应该调用适配器以显示位置。尽管如此,您必须先请求位置更新。一旦你获得了位置。取消提取进一步更新的请求。
您需要了解更多GPS如何在Android中运行以及与之相关的最佳实践。 希望它有帮助
您正在创建一个新的对象,我猜这个类内的“gps”的默认值为null。 – Carnal 2013-04-08 11:14:47
你有两个单独的文件类吗?或者相同的类出现在同一个文件中? – Carnal 2013-04-08 11:16:08
@Carnal:你好,他们在同一个项目中。实际上,我希望通过使用ShowList活动来获取位置并将这些值传递给我的适配器以便使用它们。 – George 2013-04-08 11:18:46