1

这个答案来自这里:2Darray to 1darray with for loop

我不知道是否可以在回答后重新打开它。如果是这种情况,我深表歉意。

我对该函数进行了一些修改,以像这样结束:

public  String[] initarray() {

xFINAL = new String [data_array.length];
int i = 0;
int j=0;
int UMB1 = 100;
int l = data_array.length;

for (i=0 ; i < l ; i++) {
        LATIT = Double.parseDouble(data_array [i][2]);
        LONGIT = Double.parseDouble(data_array [i][3]); 

        dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT,  LATIT);

        if (dist < UMB1){

            xFINAL[j++] = data_array [i][0];
                    System.out.println(Arrays.toString(xFINAL));
                    System.out.println("j++" + data_array[i][0]);

                }

            }

return xFINAL;

}

dist = 2 个位置之间的米差,返回一个 int。

data_array 之前已经定义过。

这是“一种工作”。我从该代码中得到的是这样的数组(data1 和 data3 在条件内,data2 为假)

[data1 , data3 , null]

我认为应该是这样的:

[data1, data3]

当我尝试将 xFINAL 数组与微调器一起使用时出现错误:

public final void rel() {




String[] array_spinner = MYSQL.xFINAL;

    ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, array_spinner);
    spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    sp_1.setAdapter(spinnerArrayAdapter);
}

当我尝试加载微调器时,它给了我以下信息:

03-25 13:25:26.367: E/AndroidRuntime(30843): FATAL EXCEPTION: main
03-25 13:25:26.367: E/AndroidRuntime(30843): java.lang.NullPointerException
03-25 13:25:26.367: E/AndroidRuntime(30843):    at     android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at  android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at  android.widget.Spinner.measureContentWidth(Spinner.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.Spinner.onMeasure(Spinner.java:442)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4825)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.View.measure(View.java:15518)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.Choreographer.doFrame(Choreographer.java:532)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.os.Handler.handleCallback(Handler.java:725)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.os.Looper.loop(Looper.java:137)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at java.lang.reflect.Method.invokeNative(Native Method)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at java.lang.reflect.Method.invoke(Method.java:511)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 13:25:26.367: E/AndroidRuntime(30843):    at dalvik.system.NativeStart.main(Native Method)

如果我只是将 3 个数据输入到微调器中,它就可以工作......所以我迷路了。

请问有什么帮助吗?:)

提前致谢。

[编辑]

03-25 19:39:23.081: I/System.out(3501): [00000001]
03-25 19:39:23.081: I/System.out(3501): 00000001
03-25 19:39:23.091: I/System.out(3501): [00000001, 00000003]
03-25 19:39:23.091: D/AndroidRuntime(3501): Shutting down VM
03-25 19:39:23.091: W/dalvikvm(3501): threadid=1: thread exiting with uncaught    exception (group=0x40bac930)
03-25 19:39:23.091: E/AndroidRuntime(3501): FATAL EXCEPTION: main
03-25 19:39:23.091: E/AndroidRuntime(3501): java.lang.IndexOutOfBoundsException:   Invalid index 2, size is 2
03-25 19:39:23.091: E/AndroidRuntime(3501):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at java.util.ArrayList.get(ArrayList.java:304)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at com.example.mvlpres.c_sql.recorre_array2(c_sql.java:635)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at com.example.mvlpres.SincronizacionFragment$1.onClick(SincronizacionFragment.java:179)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.view.View.performClick(View.java:4204)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.view.View$PerformClick.run(View.java:17355)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.os.Handler.handleCallback(Handler.java:725)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.os.Handler.dispatchMessage(Handler.java:92)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.os.Looper.loop(Looper.java:137)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at android.app.ActivityThread.main(ActivityThread.java:5041)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at java.lang.reflect.Method.invokeNative(Native Method)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at java.lang.reflect.Method.invoke(Method.java:511)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-25 19:39:23.091: E/AndroidRuntime(3501):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1
 System.out.println(Arrays.toString(xFINAL));

This line should be after for loop completes.

For references (anything that holds an object) that is null.

For int/short/byte that is a 0.

For float/double that is a 0.0

For booleans that is a false.

When you create an array of String, all entries are null initially. Your array size is 3 I think so initially it is initialized with [null,null, null]. Two null get replaced with the values and one remains as it is.

EDIT: Use ArrayList instead Array

List<String> list = new ArrayList<String>();
for (i=0 ; i < l ; i++) {
    LATIT = Double.parseDouble(data_array [i][2]);
    LONGIT = Double.parseDouble(data_array [i][3]); 

    dist = calculateDistanceByHaversineFormula(Localizacion.LONGITUD_D, Localizacion.data_arrayUD_D,LONGIT,  LATIT);

    if (dist < UMB1){

        list.add(data_array [i][0]);
                System.out.println(list);
                System.out.println(list.get(i));

     }

}
于 2013-03-25T13:51:00.273 回答