0

以下代码在我用 标记的地方给了我一个错误~~~~ ~~~~。它说:

标记的语法错误,错误的构造

有人可以帮我吗?

如果您帮助我解决此错误,我将非常感激,或者给我另一个文件夹复制代码!

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        File srcFolder = new File(Environment.getExternalStorageDirectory()
                + "/folder/");
        File destFolder = new File(
                "/data/data/folder");

        Button button1 = (Button) findViewById(R.id.button1);
        button1.setOnClickListener(new OnClickListener() {
        
            @Override
            public void onClick(View v) {
                
                
                ~~~~public void~~~~ copyFolder(srcFolder, destFolder);
                {

                    if(src.isDirectory()){

                        //if directory not exists, create it
                        if(!dest.exists()){
                           dest.mkdir();
                           Log.i("Status : ", "Directory copied from " 
                                          + srcFolder + "  to " + destFolder);
                        }

                        //list all the directory contents
                        String files[] = src.list();

                        for (String file : files) {
                           //construct the src and dest file structure
                           File srcFile = new File(src, file);
                           File destFile = new File(dest, file);
                           //recursive copy
                           copyFolder(srcFile,destFile);
                        }

                    }else{
                        //if file, then copy it
                        //Use bytes stream to support all file types
                        InputStream in = new FileInputStream(src);
                            OutputStream out = new FileOutputStream(dest); 

                            byte[] buffer = new byte[1024];

                            int length;
                            //copy the file content in bytes 
                            while ((length = in.read(buffer)) > 0){
                               out.write(buffer, 0, length);
                            }

                            in.close();
                            out.close();
                            Log.i("Status : ","File copied from " + srcFolder + " to " + destFolder);
                    }
                }
            }
        });
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
4

2 回答 2

0

在该行中,您应该调用该函数。并且该函数应该在整个 onCreate 方法之后构造:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    File srcFolder = new File(Environment.getExternalStorageDirectory()
            + "/folder/");
    File destFolder = new File(
            "/data/data/folder");
    Button button1 = (Button) findViewById(R.id.button1);
    button1.setOnClickListener(new OnClickListener() {    
        @Override
        public void onClick(View v) {
           copyFolder(srcFolder, destFolder);
        }
    });
}

public void copyFolder(String srcFolder,String  destFolder) {    
                if(src.isDirectory()){    
                    //if directory not exists, create it
                    if(!dest.exists()){
                       dest.mkdir();
                       Log.i("Status : ", "Directory copied from " 
                                      + srcFolder + "  to " + destFolder);
                    }

                    //list all the directory contents
                    String files[] = src.list();

                    for (String file : files) {
                       //construct the src and dest file structure
                       File srcFile = new File(src, file);
                       File destFile = new File(dest, file);
                       //recursive copy
                       copyFolder(srcFile,destFile);
                    }

                }else{
                    //if file, then copy it
                    //Use bytes stream to support all file types
                    InputStream in = new FileInputStream(src);
                        OutputStream out = new FileOutputStream(dest); 

                        byte[] buffer = new byte[1024];

                        int length;
                        //copy the file content in bytes 
                        while ((length = in.read(buffer)) > 0){
                           out.write(buffer, 0, length);
                        }

                        in.close();
                        out.close();
                        Log.i("Status : ","File copied from " + srcFolder + " to " + destFolder);
                }
            }

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
  }
}
于 2013-02-10T18:06:10.123 回答
0

该错误是因为您的 onClick 方法中有一个方法。

你应该只带指令和运行方法,而不是定义新的方法。

一个解决方案可能是将 copyFolder 方法定义到您的类中,并在您的 onClick 方法中运行它。

于 2013-02-10T18:07:37.487 回答