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();
}