我正在製作一個Android應用程序,用於從文件夾中讀取文件,從中生成數組,然後在地圖上繪製數組的緯度+經度點。我起初在一個單獨的程序中開發了數組返回方法作爲void方法(它的功能正確)。我對它做的唯一更改是我使它返回數組,以便Android應用程序可以使用它。不過,我總是收到武力。經過一些調試後,我發現我的方法每次都返回'null'(我設置了一個if語句,如果數組爲空,則顯示一個屏幕顯示「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;
}
所以這是我的活動樣子。
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
因此,誰能幫助我在這裏:添加「如果(凹凸== NULL)」語句,並取消註釋的「for」循環跟隨它以前我logcat的?我已經在這裏呆了幾天,但仍然無法弄清楚。
我將csv文件放入我的資產文件夾中。我查了2個例子,但是一個人說要做一些像「Activity.getAssets()。open(...)」的東西。我在這裏有點困惑。 – Mxyk