Java 函数和类调用

Java 函数和类调用,java,android,image,Java,Android,Image,我目前有一些源代码,可以在onCreate上调用这个函数吗 public static Intent getPickImageIntent(Context context) { Intent chooserIntent = null; List<Intent> intentList = new ArrayList<>(); Intent pickIntent = new Intent(Intent.ACTION_PICK,

我目前有一些源代码,可以在onCreate上调用这个函数吗

public static Intent getPickImageIntent(Context context) {
    Intent chooserIntent = null;

    List<Intent> intentList = new ArrayList<>();

    Intent pickIntent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePhotoIntent.putExtra("return-data", true);
    takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(context)));
    intentList = addIntentsToList(context, intentList, pickIntent);
    intentList = addIntentsToList(context, intentList, takePhotoIntent);

    if (intentList.size() > 0) {
        Log.d("mytag", "e");
        chooserIntent = Intent.createChooser(intentList.remove(intentList.size() - 1),
                context.getString(R.string.pick_image_intent_text));
        chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, intentList.toArray(new Parcelable[]{}));
    }

    return chooserIntent;
}
但是没有运气。调用函数时仍有错误


可以从Main.class呼叫二等舱吗?2.class拥有所有的工作和Main.class只传递和检索数据、图像和文本?

您没有提供任何详细信息,但我将尝试假设/猜测其中的一些,并尝试回答。为了将来,请修正你的问题,并尽可能精确

可以从Main.class呼叫二等舱吗?第二节课已经完成了所有的工作,主课只传递和检索数据、图像和文本

是的,这是可能的。不完全是调用类,而是调用类内的方法。如果该方法是静态的,您可以这样调用它:

SecondClassName.methodName(parameter1, parameter2); // some method
SecpmdClassName.getPickImageIntent(context);        // method from Your question
如果方法不是静态的,则需要类的实例来调用该方法,例如:

SecondClassName secondClassInstance = new SecondClassName();
secondClassInstance.methodName(parameter1, parameter2);
secondClassInstance.getPickImageIntent(context); // You can also call a static method

你提到了一个名字,比如Main.class和2nd.class。Java中的文件名必须等于内部声明的公共类。如果这是你的类名“第二”,那么它是不正确的。类的名称不能以数字开头-这就是为什么我在上面的示例中使用了“SecondClassName”而不是“second”。

这是什么
公共静态意图getPickImageIntent(上下文上下文)
。这不是调用方法的方式。你只要重新定义它们。要调用一个方法,只需使用
getPickImageIntent(context)
,其中
context
是参数,在这种情况下,它可以是
this
或任何
context
。哦,对不起。我得到这个错误:错误:(61,13)错误:无效的方法声明;返回类型必需的错误:(61,39)错误:预期缺少方法体,或声明摘要是否可以将其作为onClick或其他内容放在按钮上?试过一次,但什么都没有。开始。对不起。它最初是Methods.class。我只是用二等舱作为样本。所以我会在Main.class上加上“SecondClassName.methodName(参数1,参数2);”?我不需要任何东西来导入?如果你的方法类和主类在同一个包中,那么你不需要在导入部分添加任何东西。否则您需要:导入com.package.of.your.Methods;然后它将在主类中可见,并且可以使用。因此,它将在Main.class上导入com.package.Methods。
SecondClassName secondClassInstance = new SecondClassName();
secondClassInstance.methodName(parameter1, parameter2);
secondClassInstance.getPickImageIntent(context); // You can also call a static method