Note that there are some explanatory texts on larger screens.

plurals
  1. POLoop to fill an array for use in listview (fill array from txt file strings)
    primarykey
    data
    text
    <p>I am trying to fill an array for use in a listview in android. I want the array to be filled with data stored in several .txt files on the sdcard. I have an Int names Mx that tells the loop how many times to run (well thats what i would like it to do). The value Mx is also the number of entries required in the array.</p> <p>I am currently using this code but it doesnt work:</p> <pre><code> public void arrayload() { ///////////////////////////////////working here///////////////////////////////// //new looping code for (int i=1; i&lt;=Mx; i++){ try{ String fileName = String.valueOf(i) +".txt"; File myFile = new File("/sdcard/mx/" +fileName); FileInputStream fIn = new FileInputStream(myFile); BufferedReader myReader = new BufferedReader( new InputStreamReader(fIn)); String aDataRow = ""; String aBuffer = ""; while ((aDataRow = myReader.readLine()) != null) { aBuffer += aDataRow + "\n"; } String[] lvdata = new String[Mx]; lvdata[i] = aBuffer; ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.logbooklayout, lvdata); lv1.setAdapter(adapter); lv1.scrollTo(0, mxlv); // set the listview at a sensible point } catch (Exception e) { Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT).show(); } } </code></pre> <p>I have previously been hardcoding each entry into the array but am trying to tidy the code up so it loops based on the value Mx. I know the array and listview work fine if i manually fill them, wheni use the code below the app crashes.</p> <p>Logcat output is</p> <pre><code>03-02 16:41:23.888: E/AndroidRuntime(31497): FATAL EXCEPTION: main 03-02 16:41:23.888: E/AndroidRuntime(31497): java.lang.NullPointerException 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.AbsListView.obtainView(AbsListView.java:2159) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.ListView.measureHeightOfChildren(ListView.java:1246) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.ListView.onMeasure(ListView.java:1158) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.View.measure(View.java:15513) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:645) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:425) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.View.measure(View.java:15513) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.View.measure(View.java:15513) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.View.measure(View.java:15513) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4827) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310) 03-02 16:41:23.888: E/AndroidRuntime(31497): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2176) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.View.measure(View.java:15513) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1874) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1089) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1265) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:989) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4351) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.Choreographer.doCallbacks(Choreographer.java:562) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.Choreographer.doFrame(Choreographer.java:532) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.os.Handler.handleCallback(Handler.java:725) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.os.Handler.dispatchMessage(Handler.java:92) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.os.Looper.loop(Looper.java:137) 03-02 16:41:23.888: E/AndroidRuntime(31497): at android.app.ActivityThread.main(ActivityThread.java:5191) 03-02 16:41:23.888: E/AndroidRuntime(31497): at java.lang.reflect.Method.invokeNative(Native Method) 03-02 16:41:23.888: E/AndroidRuntime(31497): at java.lang.reflect.Method.invoke(Method.java:511) 03-02 16:41:23.888: E/AndroidRuntime(31497): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795) 03-02 16:41:23.888: E/AndroidRuntime(31497): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562) 03-02 16:41:23.888: E/AndroidRuntime(31497): at dalvik.system.NativeStart.main(Native Method) </code></pre> <p>Any help with this is really welcome. I am just cutting my teeth with Java so my code is in need of improving lots! </p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload