gpt4 book ai didi

android - setVisibility 在 android 中为 null

转载 作者:行者123 更新时间:2023-11-29 17:01:13 26 4
gpt4 key购买 nike

我有 BaseActivity。我实现了一些方法,并在子 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>

我的覆盖功能工作完美,只有我在 View 的 setVisibility.in activity_main.xml 中有问题,当然包含此 View 我该如何解决我的问题?

最佳答案

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

也就是在实际初始化 View 之前调用 view.setVisiblility()PS:在您的 BaseActivity 中,我将逻辑从 onCreat() 移动到 onStop() 在 onStart 中,您知道 onCreate 已完成并且您的所有 View 都已完成初始化

关于android - setVisibility 在 android 中为 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42858378/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com