在活动中打开csv文件时,android应用程序崩溃

时间:2012-03-21 04:40:38

标签: android csv

我在我的应用程序中使用opencsv,但每当我尝试读取我的csv文件时,应用程序崩溃。

这是我正在使用的代码:

        String next[] = {};
        List<String[]> list = new ArrayList<String[]>();

        try {
            CSVReader reader = new CSVReader(new InputStreamReader(getAssets().open("sample.csv")));

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

每当我尝试评论这些行时,该应用程序工作得很好。这可能是什么问题?

每当我点击按钮打开使用上述代码的新活动时,就会显示日志:

03-21 11:59:28.147: E/AndroidRuntime(825): FATAL EXCEPTION: main
03-21 11:59:28.147: E/AndroidRuntime(825): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.meralco.pms/com.meralco.pms.SinActivity}: java.lang.NullPointerException
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.os.Looper.loop(Looper.java:123)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread.main(ActivityThread.java:4627)
03-21 11:59:28.147: E/AndroidRuntime(825):  at java.lang.reflect.Method.invokeNative(Native Method)
03-21 11:59:28.147: E/AndroidRuntime(825):  at java.lang.reflect.Method.invoke(Method.java:521)
03-21 11:59:28.147: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-21 11:59:28.147: E/AndroidRuntime(825):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-21 11:59:28.147: E/AndroidRuntime(825):  at dalvik.system.NativeStart.main(Native Method)
03-21 11:59:28.147: E/AndroidRuntime(825): Caused by: java.lang.NullPointerException
03-21 11:59:28.147: E/AndroidRuntime(825):  at com.meralco.pms.SinActivity.onCreate(SinActivity.java:36)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-21 11:59:28.147: E/AndroidRuntime(825):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-21 11:59:28.147: E/AndroidRuntime(825):  ... 11 more

2 个答案:

答案 0 :(得分:0)

BufferedReader CSVFile = 
        new BufferedReader(new FileReader(new InputStreamReader(getAssets().open("sample.csv")));

  String dataRow = CSVFile.readLine(); 


  while (dataRow != null){
   String[] dataArray = dataRow.split(",");
   for (String item:dataArray) { 
      System.out.print(item + "\t"); 
   }
   System.out.println(); 
   dataRow = CSVFile.readLine(); 
  }
  // Close the file once all data has been read.
  CSVFile.close();

**或* * 使用CSV阅读器

 CsvReader products = new CsvReader("sample.csv");

                products.readHeaders();

                while (products.readRecord())
                {
                    String productID = products.get("field1");
                    String productName = products.get("field2");
                    String supplierID = products.get("field3");
                    String categoryID = products.get("field4");
    }
    products.close();

希望它有所帮助

答案 1 :(得分:0)

请浏览整个日志。

03-21 11:59:28.147: E/AndroidRuntime(825): Caused by: java.lang.NullPointerException
03-21 11:59:28.147: E/AndroidRuntime(825):  at com.meralco.pms.SinActivity.onCreate(SinActivity.java:36)

如果您在那里犯了任何错误,请参阅第36行.Post onCreate code so look to look

希望这有帮助。