数组总是返回空

发布于 2024-11-26 22:24:14 字数 5721 浏览 2 评论 0原文

我正在制作一个 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  

那么有人可以帮我吗?我已经在这个问题上呆了几天了,但仍然无法弄清楚。

I'm making an Android app that reads a file from a folder, generates an array from it, then plots the array's latitude + longitude points on a map. I at first developed the array returning method as a void method in a seperate program (and it functioned properly). The only changes I made to it was that I made it return the array, so that the android app can use it. However, I always got force closes. After some debugging, I found that my method was returning 'null' everytime (I set an if statement saying that if the array is null, display a screen that says "FAIL!" - and the app always is showing "FAIL!").

I checked to make sure that the file is located in the right spot - it definitely is. Still, I'm unsure as to why the program is returning null? Here's some of my code:

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;

}

So that is what my activity looks like. My logcat before adding the "if(bump == null)" statement and un-commenting the 'for' loop that follows it was:

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  

So can anyone help me out here? I've been at this for a few days and still can't figure it out.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

樱花细雨 2024-12-03 22:24:14

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

这是 NullPointerException 的来源。

文件的路径必须采用这种形式 /data/data/yourpackagename/somefolder/yourfile

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

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

This is the source of the NullPointerException.

The path of the file must be in this form /data/data/yourpackagename/somefolder/yourfile

You cant do this like this. The file must be stored inside Emulator/phone data storage. You must push the file to the emulator data, like moving your csv file into assets/raw resource and then load it into InputStreamReader object and do what you have planned to do. Search for some more examples of openning file from assets folder or res/raw folder

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文