0

I am trying to change Visibility of FastForward Button present in MediaController Class using concept of Reflection.

Below is my code snippet.

package com.example.reflection;

import java.lang.reflect.Field;
import android.app.Activity;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.widget.ImageButton;
import android.widget.MediaController;
import android.widget.Toast;
import android.widget.VideoView;

public class MainActivity extends Activity {

    private VideoView videoView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Class <? > aClass;

        try {

            MediaController controller = new MediaController(this);

            aClass = Class.forName("android.widget.MediaController");

            Field forwardButton = aClass.getDeclaredField("mFfwdButton");
            forwardButton.setAccessible(true);

 NPE--->>   ImageButton button = (ImageButton) forwardButton.get(controller);

            if (null == button) Toast.makeText(this, "Button is null", Toast.LENGTH_LONG)
                .show();
            else button.setVisibility(View.INVISIBLE);

            videoView = (VideoView) findViewById(R.id.videoView);
            videoView.setVideoPath(Environment.getExternalStorageDirectory() + "/Video/NeYo.flv");
            videoView.setMediaController(controller);
            videoView.requestFocus();

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalArgumentException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }

    }
}

I get NullPointerException while fetching ImageButton.

Can somebody please point out the error I am making?

Thanks

Anyways I got the answer for this question.

mFfwdButton is instantiated only when the View is inflated. Thats why I was getting a null. I was trying to get the variable even before MediaController has been attached to VideoView.

I followed following approach and it worked.It may not be best practice in production environment,but below snippet worked for me.

@Override
protected void onResume() {
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            ImageButton button;
            try {
                button = (ImageButton) forwardButton.get(controller);

                if (null == button) Toast.makeText(MainActivity.this, "Button is null",
                Toast.LENGTH_LONG).show();
                else button.setVisibility(View.INVISIBLE);
            } catch (IllegalArgumentException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IllegalAccessException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }
    }, 50);
    super.onResume();
}
Vipul
  • 27,808
  • 7
  • 60
  • 75
  • Does `getDecraledField()` returns successfully a non null value ? Are you fetching the field before `MediaController` initialize its layout ? – S.D. Nov 27 '12 at 10:10
  • @Singularity Yes it do returns the Non-Null Value. – Vipul Nov 27 '12 at 11:11
  • Override [`onFinishInflate()`](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.1_r1/android/widget/MediaController.java#109) , call `super.onFinishInflate()`, and then you can do your reflection work there. – S.D. Nov 27 '12 at 11:18
  • @Singularity Thanks a lot for the reply.But I got the solution on this.See the accepted answer. – Vipul Nov 27 '12 at 11:19

1 Answers1

1

mFfwdButton is instantiated only when the View is inflated. So you are getting a null. So i guess you cannot use it until the activity has been shown to the user. You are trying to get the variable even before mediacontroller has been attached to VideoView

Try posting a runnable with a delay in OnResume

nandeesh
  • 24,740
  • 6
  • 69
  • 79