4

我正在尝试将存储在单独文本文件中的行读取到我的 android 应用程序中,并让每一行显示在列表视图中。

问题是,尽管我的代码说它们不是错误,但我无法显示任何内容。当您阅读此代码时,我将澄清我的 ListView 名为“addresslist”,而我正在读取的文件名为“myaddress.txt”。我很感激任何帮助。

ListView listView= (ListView) findViewById(R.id.addresslist); 
try{
    InputStream instream = openFileInput("myaddress.txt");

    InputStreamReader inputreader = new InputStreamReader(instream);
    BufferedReader buffreader = new BufferedReader(inputreader);


    ArrayList<String> lines = new ArrayList<String>();
    boolean hasNextLine =true;
    while (hasNextLine){
        String line =  buffreader.readLine();
        lines.add(line);
        hasNextLine = line != null;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.id.addresslist,lines);

    listView.setAdapter(adapter);

    instream.close();

    }
    catch(java.io.FileNotFoundException e){

    }catch(java.io.IOException e){

    }

}

日志中出现以下错误:

12-08 01:21:30.394: E/Trace(1500): [ 12-08 01:21:30.864  1500: 1500 V/Home to School 4828 N. Crescent Norridge IL 60706, 5500 N St Louis Avenue Chicago IL 60625; Home to School 4828 N. Crescent Norridge IL 60706, 5500 N St Louis Avenue Chicago IL 60625; 
12-08 01:21:30.864: D/AndroidRuntime(1500): Shutting down VM
12-08 01:21:30.924: W/dalvikvm(1500): threadid=1: thread exiting with uncaught exception (group=0xb3e92288)
12-08 01:21:30.944: E/AndroidRuntime(1500): FATAL EXCEPTION: main
12-08 01:21:30.944: E/AndroidRuntime(1500): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tripapp/com.tripapp.Frontpage}: java.lang.NullPointerException: println needs a message
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.os.Looper.loop(Looper.java:137)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread.main(ActivityThread.java:4745)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at java.lang.reflect.Method.invokeNative(Native Method)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at java.lang.reflect.Method.invoke(Method.java:511)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at dalvik.system.NativeStart.main(Native Method)
12-08 01:21:30.944: E/AndroidRuntime(1500): Caused by: java.lang.NullPointerException: println needs a message
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.util.Log.println_native(Native Method)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.util.Log.v(Log.java:117)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at com.tripapp.Frontpage.onCreate(Frontpage.java:61)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.Activity.performCreate(Activity.java:5008)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
12-08 01:21:30.944: E/AndroidRuntime(1500):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
12-08 01:21:30.944: E/AndroidRuntime(1500):     ... 11 more
4

4 回答 4

1

首先将文件移动到项目的 assets 文件夹中,然后使用此代码从 assets 文件夹中读取文件:

    try{
    InputStream inputreader = getAssets().open("myaddress.txt");
    BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader));

   // your code here
于 2012-12-08T06:01:04.140 回答
0

此示例适用于具有 ansi 编码的文件。您可以通过将“Cp1252”替换为“UTF-8”来打开一个 utf8 文件。

活动主.xml:

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="5dp"
    android:layout_marginRight="5dp" >
</ListView>

MainActivity.java:

public class MainActivity extends ActionBarActivity {

    ListView listView1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView1 = (ListView) findViewById(R.id.listView1);

      try {
          String filePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myfile.txt";
          BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(filePath), "Cp1252"), 100);

          String line;
          ArrayList<String> lines = new ArrayList<String>();          
          while ((line = br.readLine()) != null) {            
              lines.add(line);        
          }

          br.close();

          ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,lines);


          listView1.setAdapter(adapter);


          } catch (Exception e) {
              e.printStackTrace();
          }
    }

androidmanifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />    
于 2015-03-25T18:02:41.447 回答
0
ListView listView= (ListView) findViewById(R.id.addresslist); 
try{

InputStream inputreader = getAssets().open("myaddress.txt");
BufferedReader buffreader = new BufferedReader(new InputStreamReader(inputreader));


ArrayList<String> lines = new ArrayList<String>();
boolean hasNextLine =true;
while (hasNextLine){
    String line =  buffreader.readLine();
    lines.add(line);
    hasNextLine = line != null;
}

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,R.id.addresslist,lines);

listView.setAdapter(adapter);

instream.close();

}
catch(java.io.FileNotFoundException e){

}catch(java.io.IOException e){

}

}
于 2017-05-18T09:02:07.393 回答
0

这对于 Kotlin 可能对某人有帮助

//getting listview from resource
val listView= findViewById(R.id.addresslist)
try{
//write file directory wherever it is, i used external storage just for example
val file = File(Environment.getExternalStorageDirectory(), "myaddress.txt")
//read lines from txt file into arraylist
val lines = ArrayList(file.readLines())
//set listview adapter using the previous arraylist that contain lines from txt file
val adapter = ArrayAdapter(this,R.id.addresslist,lines)
// show it in list view 
listView.adapter = adapter
}
catch(e : Exception){
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show()
}
}
于 2018-06-26T19:54:31.363 回答