BekaKK
BekaKK

Reputation: 2243

setVisibility is null in android

I have BaseActivity.I implemented some methods and i used this override methods in child Activity. This is BaseActivity class

 public class BaseActivity extends FragmentActivity {
    public static BaseActivity mThis;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.view_empty_menu);
        mThis = this;
        try {
            IO.Options opts = new IO.Options();
            opts.forceNew = true;
            opts.reconnection = false;
            socketConnectionShowDialog();

            final io.socket.client.Socket socket = IO.socket("http://54e1755a.ngrok.io", opts);
            socket.on(io.socket.client.Socket.EVENT_CONNECT, new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    JSONObject obj = new JSONObject();
                    try {
                        obj.put("host", "*********");
                        obj.put("entity", new DeviceManager(UApplication.getInstance()).getDeviceId());
                        socket.emit("initialize", obj);

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }).on("onconnect", new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    JSONObject obj = (JSONObject) args[0];
                    Log.e("obj", obj.toString());
                    socketConnectionHideDialog();
                }

            }).on("onerror", new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    JSONObject obj = (JSONObject) args[0];
                    Log.e("obj", obj.toString());
                    socketConnectionOnError();

                }
            }).on("device", new Emitter.Listener() {

                @Override
                public void call(Object... args) {
                    JSONObject jsonObject = (JSONObject) args[0];
                    socketConnectionOnDevice(jsonObject);


                }

            }).on(io.socket.client.Socket.EVENT_DISCONNECT, new Emitter.Listener() {

                @Override
                public void call(Object... args) {


                }

            });
            socket.connect();

        } catch (URISyntaxException e) {
            e.printStackTrace();
            socketConnectionHideDialog();

        }

    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        outState.putString("WORKAROUND_FOR_BUG_19917_KEY", "WORKAROUND_FOR_BUG_19917_VALUE");
        super.onSaveInstanceState(outState);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
    }

    protected View getRootView() {
        return this.getWindow().getDecorView().findViewById(android.R.id.content);
    }

    protected void socketConnectionShowDialog() {

    }

    protected void socketConnectionHideDialog() {

    }

    protected void socketConnectionOnDevice(JSONObject jsonObject) {

    }

    protected void socketConnectionOnError() {

    }


}

and this is child Activity java class

  public class MainActivity extends BaseActivity {
        private ImageView videoStatus;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            videoStatus = (ImageView) findViewById(R.id.video_status);


        }


        @Override
        public void onBackPressed() {
            super.onBackPressed();
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            startActivity(intent);
        }


        @Override
        protected void socketConnectionHideDialog() {
            super.socketConnectionHideDialog();
            Log.e("socketConnectionHideDialog", "socketConnectionHideDialog");
        }

        @Override
        protected void socketConnectionOnDevice(JSONObject jsonObject) {
            super.socketConnectionOnDevice(jsonObject);
            Log.e("socketConnectionOnDevice", "socketConnectionOnDevice");


        }

        @Override
        protected void socketConnectionOnError() {
            super.socketConnectionOnError();
            Log.e("socketConnectionOnError", "socketConnectionOnError");

        }

        @Override
        protected void socketConnectionShowDialog() {
            super.socketConnectionShowDialog();
            Log.e("socketConnectionShowDialog", "socketConnectionShowDialog");
            initUniMedia();

        }

        private void initUniMedia() {
            videoStatus.setVisibility(View.VISIBLE);

        }
    }



activity_main.xml code 




  <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">


    <ImageView
        android:id="@+id/video_status"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:layout_gravity="center"
        android:src="@mipmap/pause_icon"
        android:textColor="#ffffff"
        android:textSize="24dp" />


</RelativeLayout>

My override functions working perfect,only i have problem in view's setVisibility.in activity_main.xml o course contains this view How i can solve my problem?

Upvotes: 1

Views: 314

Answers (1)

X3Btel
X3Btel

Reputation: 1428

super.onCreate(savedInstanceState); makes call to socketConnectionShowDialog(); which is called before: videoStatus = (ImageView) findViewById(R.id.video_status);

Aka you are calling view.setVisiblility() before actually initialisint the view PS: In your BaseActivity Id move the logic from onCreat() to onStop() In onStart you know onCreate is completed and all your views are initialised

Upvotes: 3

Related Questions