我正在制作一个Android应用程序,它从文件夹中读取文件,从中生成一个数组,然后在地图上绘制数组的纬度+经度点。我首先在单独的程序中开发了数组返回方法作为void方法(并且它正常运行)。我对它做的唯一更改是我让它返回数组,以便android应用程序可以使用它。但是,我总是被迫关闭。经过一些调试后,我发现我的方法每次都返回'null'(我设置一个if语句,如果数组为null,则显示一个显示“FAIL!”的屏幕 - 并且应用程序总是显示“FAIL!”) 。
我检查确保文件位于正确的位置 - 肯定是。不过,我不确定程序为什么返回null?这是我的一些代码:
package net.learn2develop.GoogleMaps;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.List;
import java.util.StringTokenizer;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
import com.google.android.maps.Overlay;
import com.google.android.maps.MapView.LayoutParams;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Point;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MapsActivity extends MapActivity
{
MapView mapView;
MapController mc;
GeoPoint p;
GeoPoint p2;
GeoPoint p99;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Map code goes here.....
// Add points from ReadCsv.java
File fileToUse = new File("/Users/csrobot/Desktop/TrainingData/Training4.csv");
String[][] bump = getArray(fileToUse);
if(bump == null){
setContentView(R.layout.deleteme);
}
// for(int i = 0; i < bump.length; i++) {
// String coordinates99[] = {bump[i][0], bump[i][1]};
// double lat99 = Double.parseDouble(coordinates99[0]);
// double lng99 = Double.parseDouble(coordinates99[1]);
// p99 = new GeoPoint(
// (int) (lat99 * 1E6),
// (int) (lng99 * 1E6));
// MapOverlay mapOverlay99 = new MapOverlay();
// List<Overlay> listOfOverlays99 = mapView.getOverlays();
// listOfOverlays99.add(mapOverlay99);
// }
mapView.invalidate();
}
@Override
protected boolean isRouteDisplayed() {
return false;
}
public String[][] getArray(File file) {
try {
int row = 0;
int col = 0;
String[][] numbers=new String[5258][16];
BufferedReader bufRdr;
bufRdr = new BufferedReader(new FileReader(file));
String line = null;
//read each line of text file
while((line = bufRdr.readLine()) != null) {
StringTokenizer st = new StringTokenizer(line,",");
col=0;
while (st.hasMoreTokens()) {
//get next token and store it in the array
numbers[row][col] = st.nextToken();
col++;
}
row++;
}
// ... Here is the code that will make the array
// ...
//close the file
bufRdr.close();
return arrayOfBumps;
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch(Exception e) {
// System.out.println("The following error occurred "+e);
}
return null;
}
这就是我的活动的样子。在添加“if(bump == null)”语句并取消注释后面的'for'循环之前,我的logcat是:
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): FATAL EXCEPTION: main
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.learn2develop.GoogleMaps/net.learn2develop.GoogleMaps.MapsActivity}: java.lang.NullPointerException
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.os.Handler.dispatchMessage(Handler.java:99)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.os.Looper.loop(Looper.java:123)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at java.lang.reflect.Method.invokeNative(Native Method)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at java.lang.reflect.Method.invoke(Method.java:521)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at dalvik.system.NativeStart.main(Native Method)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): Caused by: java.lang.NullPointerException
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at net.learn2develop.GoogleMaps.MapsActivity.onCreate(MapsActivity.java:89)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-28 09:11:10.962: ERROR/AndroidRuntime(13014): ... 11 more
那么有人可以帮助我吗?我已经在这几天了,仍然无法理解。
答案 0 :(得分:2)
File fileToUse = new File("/Users/csrobot/Desktop/TrainingData/Training4.csv");
这是NullPointerException
。
文件的路径必须采用此格式/data/data/yourpackagename/somefolder/yourfile
你不能这样做。该文件必须存储在仿真器/电话数据存储器中。您必须将文件推送到模拟器数据,例如将csv文件移动到assets/raw resource
,然后将其加载到InputStreamReader
对象中并执行您计划执行的操作。从assets
文件夹或res/raw
文件夹