Android - 自定义类的导入方式不同,不确定为什么

时间:2016-08-01 15:05:33

标签: android android-studio

我在Android中有一个自定义类,非常简单,我打电话下载并启动应用程序中来自特定URL的文件的共享意图

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Environment;

import java.io.File;

public class DownloadAndShare
{
    Activity activity;
    Context context;
    ProgressDialog dialog;
    String URL;

    public DownloadAndShare(Activity activity, Context context, String URL) {
        this.activity = activity;
        this.context = context;
        this.URL = URL;
    }

    public void startSharing()
    {
        //Start shareIntent for this file
        File fileWithinMyDir = Environment.getExternalStorageDirectory();
        String[] fn = URL.split("/");
        String fileName = fn[fn.length-1];
        File storageFolder = new File(fileWithinMyDir.toString() + "/myappname");
        //If storage folder does not yet exists, create it
        if(!storageFolder.exists()){
            storageFolder.mkdirs();
        }
        final String PATH = fileWithinMyDir.toString() + "/myappname/" + fileName;
        //Check if file exists in internal storage
        File file = new File(PATH.replace(" ", ""));
        if (!file.exists()){
            //file does not exists, download to internal storage for use
            new executeDownloadAndShare().execute(PATH.replace(" ", ""), URL);
        } else {
            Intent shareIntent = Globals.getShareIntent(PATH.replace(" ", ""), false);
            activity.startActivity(Intent.createChooser(shareIntent, "Share using..."));
        }
    }

    private class executeDownloadAndShare extends AsyncTask<String, Integer, Integer> {

        String filePath;

        @Override
        protected void onPreExecute() {
            dialog = ProgressDialog.show(context, "", context.getResources().getString(R.string.general_msg_please_wait));
        }

        @Override
        protected Integer doInBackground(String... params) {
            filePath = params[0];
            try {
                Globals.DownloadFromUrl(params[0], params[1]);
            } catch (Exception e) {
                return AppConstants.STATUS_FAIL;
            }
            return AppConstants.STATUS_SUCCESS;
        }

        @Override
        protected void onPostExecute(Integer result) {
            dialog.dismiss();
            Intent shareIntent = Globals.getShareIntent(filePath, false);
            activity.startActivity(Intent.createChooser(shareIntent, "Share using..."));
        }
    }
}

出于某种原因,当我尝试在我的一个文件中创建它的一个实例时,它需要有完整的com.myappname.android.MyCustomClass myCustomClass = new com.myappname.android.MyCustomClass();在订单文件中我只有MyCustomClass myCustomClass = new MyCustomClass()并且它工作正常但是在一些文件中它需要整个路径。

有没有人知道为什么会这样?我称之为的两个文件都是扩展Fragment,一个工作而另一个需要完整路径。我想也许这是一个私人方法,它应该是公开的等等,但我找不到任何错误。

1 个答案:

答案 0 :(得分:1)

在再次启动之前清理项目并从设备/模拟器卸载应用程序。如果没有帮助,请重启Android Studio。