我正在尝试填充一个数组,以便在android中的listview中使用。我希望数组填充存储在SD卡上的几个.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切齿,所以我的代码需要改进很多!
由于
答案 0 :(得分: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
}
您的代码中存在两个问题:
尝试使用Logcat而不是Toast“Log.e(tag,msg,tr)”,您可以直接传递异常。
希望它会有所帮助