Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用findViewById访问自定义视图_Java_Android - Fatal编程技术网

Java 无法使用findViewById访问自定义视图

Java 无法使用findViewById访问自定义视图,java,android,Java,Android,我已经围绕ExoPlayer编写了一个小包装,以抽象出一些实现细节,并添加一些我们自己的业务逻辑。此包装的签名如下所示: public class BFPlayer extends PlayerView { 现在我有了一个使用此视图的活动,我希望以编程方式访问该视图,以便可以对其调用方法。以下是我当前的代码: public class PlayBroadcastActivity extends AppCompatActivity { @Override protected v

我已经围绕ExoPlayer编写了一个小包装,以抽象出一些实现细节,并添加一些我们自己的业务逻辑。此包装的签名如下所示:

public class BFPlayer extends PlayerView {
现在我有了一个使用此视图的活动,我希望以编程方式访问该视图,以便可以对其调用方法。以下是我当前的代码:

public class PlayBroadcastActivity extends AppCompatActivity {

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

        BFPlayer player = findViewById(R.id.player);

        player.displayMessage("Test");
    }
}
以及此活动的XML:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".PlayBroadcastActivity">

    <com.blueframetech.bfplayer.BFPlayer
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>

为什么会这样?错误引用了
com.google.android.exoplayer2.ui.PlayerView
,但我正在尝试访问
PlayerView
子类。为什么它要访问这个依赖于第三方的库?另外,为什么尝试失败?

对于Java/Kotlin代码来说,要使用一个类,编译器需要访问继承层次结构中的所有类,因此它可以访问全部
public
成员。由于
BFPlayer
扩展了
PlayerView
,因此
BFPlayer
的消费者需要访问
PlayerView

但是,在包含
BFPlayer
的模块中,您使用
实现集成了ExoPlayer。这表示“使用此模块的依赖项,但不要将其标记为依赖于此模块的任何其他模块的可传递依赖项”。对于应用程序模块,
实现
是可以的,因为没有其他模块依赖于应用程序模块

但是,
bPlayer
在库模块中。因此,
implementation
阻止依赖于
BFPlayer
模块的模块访问ExoPlayer

99.44%的时候,在库模块中,您希望您的主要依赖项使用
api
,而不是
implementation
,因此库模块的使用者也会引入这些其他(可传递的)依赖项


因此,对于ExoPlayer依赖项(以及其他任何依赖项),在
BFPlayer
模块中将
implementation
更改为
api
,您应该能够克服您的问题。

“它为什么试图访问这个第三方依赖库?”——我想是出于继承目的。“为什么尝试失败?”——
BFPlayer
PlayBroadcastActivity
在同一个模块中,还是在单独的模块中?@commonware
BFPlayer
在单独的模块中。我们的目标是将
BFPlayer
发布为一个JAR,可以包含在第三方应用程序中(这样,我们的客户可以定制应用程序,使用我们的业务逻辑和广告源播放我们的内容)。因此有一个Android库模块用于
BFPlayer
和一个应用程序模块用于测试它。在包含
BFPlayer
的模块中,对于
dependencies
中的ExoPlayer依赖项,您是否使用
api
实现
,还是别的?@commonware我目前正在使用
implementation'com.google.android.exoplayer:exoplayer:2.9.5'
> Task :app:compileDebugJavaWithJavac FAILED
/Users/stevenbarnett/Source/BluePlayer/AndroidSDK/app/src/main/java/com/blueframetech/blueframesdk/PlayBroadcastActivity.java:16: error: cannot access PlayerView
        BFPlayer player = findViewById(R.id.player);
                          ^
  class file for com.google.android.exoplayer2.ui.PlayerView not found
1 error