android中的setVisibility为null

时间:2017-03-17 13:06:59

标签: android android-activity android-imageview

我有BaseActivity.I实现了一些方法,我在子Activity中使用了这种覆盖方法。 这是BaseActivity类

 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() {

    }


}

这是子Activity java类

  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>

我的覆盖功能工作正常,只有我在视图的setVisibility.in activity_main.xml中有问题o当然包含这个视图 我怎么能解决我的问题?

1 个答案:

答案 0 :(得分:3)

super.onCreate(savedInstanceState);调用之前调用的socketConnectionShowDialog();videoStatus = (ImageView) findViewById(R.id.video_status);

在实际初始化视图之前,您正在调用view.setVisiblility() PS:在您的BaseActivity ID中将逻辑从onCreat()移至onStop()在onStart中,您知道onCreate已完成并且所有视图都已初始化