NullPointerException when trying to mount an Activity to show a video

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 PlayerView class comes from here: link

In my XML I include this:

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

This layout comes from here: link

    
asked by anonymous 11.02.2017 / 02:57

1 answer

0

The link of class ViewPlayer is this: link

Line 577 is a blank line, so you should be using a different version. However, in the constructor of the class we have this:

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

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

For settingsContainer is null , it is because the findViewById method failed. This method probably failed because ResourceUtils.getResourceIdByName failed.

The code for ResourceUtils is here: link

Here is the code for 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 simple_player_settings_container field of the R.id class of its VideoActivity3 package was not found.

However, it is trying to access the contents of the layout file link - That is, it is looking for the wrong layout file.

It is important to note that the tv.lycam.ijkplayer.widget.IjkVideoView class is what renders the video. But it renders only the video and nothing else. The tv.lycam.ijkplayer.widget.PlayerView class is responsible for rendering the video together. At least from what I understood the code, that's it, but I might be wrong.

Therefore, IjkVideoView needs PlayerView to work. However, PlayerView already includes IjkVideoView in your layout.

In your layout XML you have this:

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

That will not 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 hardcoded in simple_player_view_player.xml :

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

  • " 继续 " should be replaced with " Play ", " Rodar " or " Executar ".

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

  • At the end of onCreate method of your 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");
    
  • Another way is to unpack the AAR, change them to switch between Chinese and Portuguese,

  • 11.02.2017 / 05:59