开发者

generate csv file force close android [closed]

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center. Closed 11 years ago.

I am generating csv file and it force close, why? my code is

   package contactlist.pkg;

       import java.io.FileWriter;
       import java.io.IOException;

     public class GenerateCsv
   {
 public static void main(String [] args)
 {
   generateCsvFile("C:\\test.csv"); 
 }

   private static void generateCsvFile(String sFileName)
   {
 try
  {
    FileWriter writer = new FileWriter(sFileName);

    writer.开发者_JAVA技巧append("DisplayName");
    writer.append(',');
    writer.append("Age");
    writer.append('\n');

    writer.append("MKYONG");
    writer.append(',');
    writer.append("26");
        writer.append('\n');

    writer.append("YOUR NAME");
    writer.append(',');
    writer.append("29");
    writer.append('\n');

    //generate whatever data you want

    writer.flush();
    writer.close();
}
catch(IOException e)
{
     e.printStackTrace();
} 
   }
}

errors are

   07-27 13:12:30.508: ERROR/AndroidRuntime(939): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{contactlist.pkg/contactlist.pkg.GenerateCsv}: java.lang.ClassCastException: contactlist.pkg.GenerateCsv
   07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
    07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
      07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.os.Handler.dispatchMessage(Handler.java:99)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.os.Looper.loop(Looper.java:123)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.main(ActivityThread.java:3683)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at java.lang.reflect.Method.invokeNative(Native Method)
       07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at java.lang.reflect.Method.invoke(Method.java:507)
     07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
      07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
         07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at dalvik.system.NativeStart.main(Native Method)
           07-27 13:12:30.508: ERROR/AndroidRuntime(939): Caused by: java.lang.ClassCastException: contactlist.pkg.GenerateCsv
         07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
       07-27 13:12:30.508: ERROR/AndroidRuntime(939):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)


package contactlist.pkg;

import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

public class MainActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        String dir = "/Android/data/contactlist.pkg/csv/";
        String fullDir = Environment.getExternalStorageDirectory().toString() + dir;
        generateCsvFile(fullDir, "data.csv");
    }

    private static void generateCsvFile(String dir, String fileName) {
        try {

            File theDir = new File(dir);
            theDir.mkdirs();

            FileWriter writer = new FileWriter(dir + fileName);

            writer.append("DisplayName");
            writer.append(',');
            writer.append("Age");
            writer.append('\n');

            writer.append("MKYONG");
            writer.append(',');
            writer.append("26");
            writer.append('\n');

            writer.append("YOUR NAME");
            writer.append(',');
            writer.append("29");
            writer.append('\n');

            //generate whatever data you want

            writer.flush();
            writer.close();
        } catch (IOException e) {
            Log.i("file", e.getMessage());
        }
    }
}

String dir = "/Android/data/contactlist.pkg/csv/"; is just and example of how you should structure your application folder in the external storage, it should be like this: /Android/data/<package_name>/files/, and you will need to add the permission on the AndroidManifest.xml, directly in manifest tag.

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

And I humbly suggest you to read this Android Data Storage documentation :)


Caused by: java.lang.ClassCastException: contactlist.pkg.GenerateCsv

In android, you don't have a main method, instead, you implement an activity, service or receiver and override some of its methods (such as onCreate()). Please refer to the basic android tutorial: Hello world


You need to learn more about the file structure in Android. There is no C: drive only sdcard to which you write. Use getExternalStorageDirectory to get the correct file path and then do not forget to add WRITE_SDCARD permission.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜