0

我正在尝试填充一个数组以在 android 的列表视图中使用。我希望数组填充存储在 sdcard 上的几个 .txt 文件中的数据。我有一个 Int 名称 Mx,它告诉循环运行多少次(这就是我想要它做的)。值 Mx 也是数组中所需的条目数。

我目前正在使用此代码,但它不起作用:

 public void arrayload() {
        ///////////////////////////////////working here///////////////////////////////// 
         //new looping code
         for (int i=1; i<=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<String> adapter = new ArrayAdapter<String>(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();
                    }
         }

我之前一直在将每个条目硬编码到数组中,但我正在尝试整理代码,以便它根据值 Mx 循环。我知道如果我手动填充数组和列表视图工作正常,当我使用应用程序崩溃下方的代码时。

Logcat 输出为

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)

对此的任何帮助都非常受欢迎。我只是在使用 Java,所以我的代码需要改进很多!

谢谢

4

1 回答 1

1

试试这个 !

public void arrayload() {
        ///////////////////////////////////working here///////////////////////////////// 

     String[] lvdata = new String[Mx];

     for (int i=0 ; i < Mx ;i++){
        lvdata[i]="";
     }

         for (int i=0; i<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";
                        }

                lvdata[i] = aBuffer;

                }
                 catch (Exception e) {
                        Toast.makeText(getBaseContext(), e.getMessage(),
                                Toast.LENGTH_SHORT).show();
                }
     }  
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.logbooklayout, lvdata);
    lv1.setAdapter(adapter);
        lv1.scrollTo(0, mxlv); // set the listview at a sensible point
}

您的代码中有两个问题:

  • 首先,您将在每个循环中填充的数组实例化。所以想象 Mx=3 i =2,因为你只是在 lvDate[2] 中输入了一个值,所以 lvdata[0] 和 lvdata[1] 为空,因此为 NullpointerException。
  • 其次,您也实例化每个循环的适配器。

尝试使用 Logcat 而不是 Toast "Log.e(tag, msg, tr)" 你可以直接传递你的异常。

希望它会有所帮助

于 2013-03-02T18:10:20.723 回答