Fill an android spinner from a String list

时间:2015-06-30 13:55:12

标签: android android-spinner

I have a problem with putting in a spinner the elements from a list. It gives me "Application has stopped".

This is the code:

private void connectToPC(){
        try {
            "..."
            String[] files = smbFile.list();

            for (String fileName : files) {
                Log.e("Test", "Fisier: " + fileName);
            }

            ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.id.fisierDate, files);
            fisierDate.setVisibility(View.VISIBLE);
            fisierDate.setAdapter(adapter);


        } catch (IOException e) {
            e.printStackTrace();
            Log.e("", "", e);
        }
    }

and this is the error that I get in Logcat:

06-30 16:46:30.932: E/AndroidRuntime(27236): FATAL EXCEPTION: main
06-30 16:46:30.932: E/AndroidRuntime(27236): Process: com.rasp.pi, PID: 27236
06-30 16:46:30.932: E/AndroidRuntime(27236): android.content.res.Resources$NotFoundException: Resource ID #0x7f0a0002 type #0x12 is not valid
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.content.res.Resources.loadXmlResourceParser(Resources.java:3915)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.content.res.Resources.getLayout(Resources.java:2161)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.LayoutInflater.inflate(LayoutInflater.java:413)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:197)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.Spinner.onMeasure(Spinner.java:526)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.View.measure(View.java:18586)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:728)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:464)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.View.measure(View.java:18586)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.View.measure(View.java:18586)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:447)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.View.measure(View.java:18586)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5827)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:430)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2897)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.View.measure(View.java:18586)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2273)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1331)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1573)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1216)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6672)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.Choreographer.doCallbacks(Choreographer.java:590)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.Choreographer.doFrame(Choreographer.java:560)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.os.Handler.handleCallback(Handler.java:739)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.os.Handler.dispatchMessage(Handler.java:95)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.os.Looper.loop(Looper.java:145)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at android.app.ActivityThread.main(ActivityThread.java:5944)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at java.lang.reflect.Method.invoke(Native Method)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at java.lang.reflect.Method.invoke(Method.java:372)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
06-30 16:46:30.932: E/AndroidRuntime(27236):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)

The connectToPC() method is called onClick event of a button.

I've used this: Android : Fill Spinner From Java Code Programmatically

Thanks.

3 个答案:

答案 0 :(得分:0)

you are using the wrong constructor of ArrayAdapter. The second parameter should be the layout which contains the TextView with id fisierDate

Change

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.id.fisierDate, files);

with

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.layout_with_fisierDate, R.id.fisierDate, files);

答案 1 :(得分:0)

You need to add the string disease_prompt to your strings.xml.

答案 2 :(得分:0)

Your trouble in error in .xml layout file near resource id R.id.fisierDate, check it for correctness and try again.