【发布时间】:2016-05-05 09:29:44
【问题描述】:
我正在制作一个将在谷歌地图上显示多个位置的应用程序,并且我已经通过 csv 文件读取了它。我在 OnCreate 和 displarArrayList 方法中有一个错误。
这是错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.util.ArrayList.size()' on a null object reference
at com.w2s.where2shop.ShowAllMap$CSVFile.displayArrayList(ShowAllMap.java:267)
at com.w2s.where2shop.ShowAllMap.onCreate(ShowAllMap.java:65)
at android.app.Activity.performCreate(Activity.java:5975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:147)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5264)
at java.lang.reflect.Method.invoke(Native Method)
这是 onCreate
private InputStream inputStream;
private CSVFile csvFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_all_map);
setUpMapIfNeeded();
inputStream = getResources().openRawResource(R.raw.allmap);
csvFile = new CSVFile(inputStream);
csvFile.read();
try {
csvFile.displayArrayList();
} catch (IOException e) {
e.printStackTrace();
}
mGoogleApiClient = new GoogleApiClient.Builder(this)
.addConnectionCallbacks(this)
.addOnConnectionFailedListener(this)
.addApi(LocationServices.API)
.build();
// Create the LocationRequest object
mLocationRequest = LocationRequest.create()
.setPriority(LocationRequest.PRIORITY_LOW_POWER)
.setNumUpdates(1);
}
这是我读取 csv 文件的地方,在 CsvFile 类中我得到了 displayarrayItem 方法。
public class CSVFile {
InputStream inputStream;
ArrayList<String[]> addressList;
public CSVFile(InputStream is) {
this.inputStream = is;
}
public ArrayList<String[]> read() {
ArrayList<String[]> addressList = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] row = line.split(",");
addressList.add(row);
}
} catch (IOException e) {
Log.e("Main", e.getMessage());
} finally {
try {
inputStream.close();
} catch (IOException e) {
Log.e("Main", e.getMessage());
}
}
return addressList;
}
public ArrayList<String[]> displayArrayList() throws IOException {
for (int x = 0; x > this.addressList.size(); x++) {
Geocoder gc = new Geocoder(ShowAllMap.this);
List<Address> list = gc.getFromLocationName(addressList.get(x).toString(), 1);
if (list.size() > 0) {
Address add = list.get(0);
double lat = add.getLatitude();
double lng = add.getLongitude();
LatLng latLng = new LatLng(lat, lng);
MarkerOptions options = new MarkerOptions()
.position(latLng);
mMap.animateCamera(CameraUpdateFactory.zoomIn());
mMap.addMarker(options);
}
}
return addressList;
}
}
【问题讨论】:
-
ArrayList
地址列表;你已经声明了 arraylist 但还没有实例化。 -
在
CSVFile.read()方法中将ArrayList<String[]> addressList = new ArrayList<>();替换为addressList = new ArrayList<>();您的本地变量会隐藏您的字段,因为具有相同的名称并且您的字段始终为空 -
感谢 hlp,现在它没有错误了。但是当我运行应用程序时。没有标记。
-
请在您的标题上加倍努力:
There is an error in my app在读者在搜索结果或首页上看到它时并不是很有用。也不鼓励使用hlp plz的 Txtspk - Stack Overflow 不是聊天网站。