Nullpointerexception when trying to mount an Activity to show a video

Asked

Viewed 78 times

1

My code:

public class VideoActivity3 extends Activity {

    public final static String LOCATION3 = "com.compdigitec.VideoActivity3.location3";
    public final static String mFilePath = "";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        View main = View.inflate(this, R.layout.ijkplayer, null);
        setContentView(main);

        Intent intent = getIntent();
        String mFilePath = intent.getExtras().getString(LOCATION3);

    }

    @Override
    protected void onResume() {
        super.onResume();
        PlayerView player = new PlayerView(this)
                .setTitle("")
                .setScaleType(PlayStateParams.fitparent)
                .hideMenu(true)
                .hideSteam(true)
                .setForbidDoulbeUp(true)
                .hideCenterPlayer(true)
                .hideControlPanl(true);

        player.setPlaySource(mFilePath, true)
                .startPlay();
    }

    @Override
    public void onBackPressed(){
        super.onBackPressed();
        this.finish();
    }

I’m getting this error output:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference
                      at tv.lycam.ijkplayer.widget.PlayerView.<init>(PlayerView.java:577)
                      at org.tech.android.TechMedia.VideoActivity3$override.onResume(VideoActivity3.java:32)
                      at org.tech.android.TechMedia.VideoActivity3$override.access$dispatch(VideoActivity3.java)

This error is produced on this line when starting the player:

PlayerView player = new PlayerView(this)

The class PlayerView comes from here: https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/blob/master/sdk/LycamPlayer/ijkplayer/src/main/java/tv/lycam/ijkplayer/widget/PlayerView.java

In my XML I include this:

<include layout="@layout/simple_player_view_player"/>

This layout comes from here: https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/blob/master/sdk/LycamPlayer/ijkplayer/src/main/res/layout/simple_player_view_player.xml

  • Free, in this your code has no place defining the setVisibility() method. Put the code in which you are actually using method.

  • @Acklay is inside the builder of PlayerView who gave the exception, class that is not his, but something that he imported.

  • settingsContainer = activity.findViewById(ResourceUtils.getResourceIdByName(mContext, "id", "simple_player_settings_container"));&#xA; settingsContainer.setVisibility(View.GONE);

  • This inside the Playerview, which I’m importing from an Aar...

  • Where did you find this class PlayerView? Where is she now?

  • The Playerview class can be seen at this link [https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/raw/master/sdk/LycamPlayer/ijkplayer/src/main/java/tv/lycam/ijkplayer/widget/PlayerView.java] @Victorstafusa

  • @Victorstafusa but then it gets hard O7 hehe

Show 2 more comments

1 answer

0


The class link ViewPlayer that’s the one: https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/raw/master/sdk/LycamPlayer/ijkplayer/src/main/java/tv/lycam/ijkplayer/widget/PlayerView.java

The line 577 is a blank line, and therefore you must be using some different version. However, in the class constructor we have this:

        settingsContainer = activity.findViewById(ResourceUtils.getResourceIdByName(mContext, "id", "simple_player_settings_container"));
        settingsContainer.setVisibility(View.GONE);

The line with the setVisiblity gives the NullPointerException, and therefore the settingsContainer is null.

To the settingsContainer was null, is because the method findViewById failed. This method probably failed because the ResourceUtils.getResourceIdByName failed.

The code of ResourceUtils is here: https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/raw/master/sdk/LycamPlayer/ijkplayer/src/main/java/tv/lycam/ijkplayer/utils/ResourceUtils.java

This is the code of the method in question:

    public static int getResourceIdByName(Context context, String className, String name) {
        int id = 0;
        if (context == null) {
            return id;
        } else {
            String packageName = context.getPackageName();

            try {
                String var10 = packageName + ".R$" + className;
                Class desireClass = Class.forName(var10);
                if (desireClass != null) {
                    id = desireClass.getField(name).getInt(desireClass);
                }
            } catch (ClassNotFoundException var7) {
                Logger.e("ClassNotFoundException: class=%s fieldname=%s", className, name);
            } catch (IllegalArgumentException var8) {
                Logger.e("IllegalArgumentException: class=%s fieldname=%s", className, name);
            } catch (SecurityException var9) {
                Logger.e("SecurityException: class=%s fieldname=%s", className, name);
            } catch (IllegalAccessException var101) {
                Logger.e("IllegalAccessException: class=%s fieldname=%s", className, name);
            } catch (NoSuchFieldException var11) {
                Logger.e("NoSuchFieldException: class=%s fieldname=%s", className, name);
            }

            return id;
        }
    }

This method must have returned 0 because the field simple_player_settings_container class R.id of your pack VideoActivity3 was not found.

However, it is trying to access the contents of the layout file https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/blob/master/sdk/LycamPlayer/ijkplayer/src/main/res/layout/simple_player_view_player.xml - That is, it is searching for the wrong layout file.

It is important to note that the class tv.lycam.ijkplayer.widget.IjkVideoView is the one that renders the video. But it renders only the video and nothing else. Already the class tv.lycam.ijkplayer.widget.PlayerView is responsible for rendering the video together. At least as far as I understand the code, that’s it, but I could be wrong.

Accordingly, the IjkVideoView needs the PlayerView to function. However, the PlayerView already includes the IjkVideoView in its layout.

In your layout XML you have this:

<include layout="@layout/simple_player_view_player"/>

This is not going to work. What you want is this:

<tv.lycam.ijkplayer.widget.PlayerView
    android:id="@+id/player"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

The code of your VideoActivity3 will also have to have some changes:

public class VideoActivity3 extends Activity {

    public final static String LOCATION3 = "com.compdigitec.VideoActivity3.location3";
    public final static String mFilePath = "";

    private PlayerView player;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        player = (PlayerView) View.inflate(this, R.layout.player, null);
        setContentView(main);

        Intent intent = getIntent();
        String mFilePath = intent.getExtras().getString(LOCATION3);
    }

    @Override
    protected void onResume() {
        super.onResume();
        player.setTitle("")
                .setScaleType(PlayStateParams.fitparent)
                .hideMenu(true)
                .hideSteam(true)
                .setForbidDoulbeUp(true)
                .hideCenterPlayer(true)
                .hideControlPanl(true)
                .setPlaySource(mFilePath, true)
                .startPlay();
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        this.finish();
    }
}

One thing that will still be pending is about these texts hardcoded in the simple_player_view_player.xml:

  • "您正在使用移动网络播放视频\n可能产生较高流量费用" must be exchanged for "Você está executando\no vídeo usando a rede móvel.\nIsso pode acarretar maiores\ncustos de tráfego de dados."

  • "继续" must be exchanged for "Play", "Rodar" or "Executar".

I believe there are two ways to exchange these texts for their equivalents in Portuguese:

  1. At the end of the method onCreate of his VideoActivity3, put this:

    LinearLayout a = (LinearLayout) findViewById(ResourceUtils.getResourceIdByName(player, "id", "app_video_netTie"));
    
    ((TextView) a.getChildAt(0)).setText("Você está executando\no vídeo usando a rede móvel.\nIsso pode acarretar maiores\ncustos de tráfego de dados.");
    ((TextView) a.getChildAt(1)).setText("Rodar");
    
  2. Another way is to unpack the AAR, change them to exchange the texts in Chinese for Portuguese and then repack them.

  • This project on the link seems to be hosted on some kind of Chinese github! will it?! Hi

  • @Acklay Apparently that’s right. A Ching-Ling version of github.

  • 1

    It is, eheh. see: https://coding.net/u/lycam

  • Thanks @Victorstafusa, in the layout linked to this context I include <include layout="@layout/simple_player_view_player"/>, and in this layout of it there is the simple_player_settings_container so much that the same comes to be rendered by android studio in the preview.

  • @Victorstafusa, here’s the layout.... https://coding.net/u/lycam/p/lycamplus-android-ijkplayer-sdk/git/blob/master/sdk/LycamPlayer/ijkplayer/src/main/res/layout/simple_player_view_player.xml

  • @Freeflix You can edit your question to put there how is your layout file?

  • @Freeflix I edited the answer. See if it now works.

  • @Victorstafusa, yes it worked, thank you very much...

  • @Freeflix If this answer solved your problem, could you then mark it as accepted/correct by clicking on what’s up there below the voting numbers? By doing this, you also mark your question as solved/solved. Thank you.

  • Done @Victorstafusa, thanks..

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.