数组总是返回Null



我正在制作一个Android应用程序,从文件夹中读取文件,从中生成数组,然后在地图上绘制数组的纬度和经度点。我首先在一个单独的程序中开发数组返回方法作为void方法(并且它正常工作)。我对它所做的唯一改变是我让它返回数组,以便android应用程序可以使用它。然而,我总是强行关闭。经过一些调试,我发现我的方法每次都返回'null'(我设置了一个if语句,如果数组为空,则显示一个屏幕,说"失败!"——应用程序总是显示"失败!")。

我检查了一下,以确保文件位于正确的位置-它肯定是。仍然,我不确定为什么程序返回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  

有谁能帮帮我吗?我已经琢磨了好几天了,还是想不明白。

File fileToUse = new File("/Users/csrobot/Desktop/TrainingData/Training4.csv");

这是NullPointerException的来源。

文件的路径必须是/data/data/yourpackagename/somefolder/yourfile

你不能这样做。该文件必须存储在模拟器/手机数据存储中。您必须将文件推送到模拟器数据,例如将csv文件移动到assets/raw resource中,然后将其加载到InputStreamReader对象中,并执行您计划执行的操作。搜索更多从assets文件夹或res/raw文件夹打开文件的示例

相关内容

  • 没有找到相关文章

最新更新