2017-03-17 69 views
1

我有BaseActivity.I實現了一些方法,我在子Activity中使用了這種重寫方法。 這是BaseActivity類setVisibility在android中爲null

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

    } 


} 

,這是

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> 

我的覆蓋功能的工作完美的孩子活動的Java類,只有我在視圖的setVisibility.in activity_main.xml中Ø當然問題包含這一觀點 我如何解決我的問題?

+0

問題不清楚。請妥善解釋。也添加xml文件。 –

+0

在我的BaseActivity中,我有socket.io連接方法,我想使用這個BaseActivity類的另一個活動,正如我所說的,連接和方法工作完美,但我只有setVisibility問題@J – BekaKK

回答

3

super.onCreate(savedInstanceState);,使呼叫socketConnectionShowDialog();被稱爲前: videoStatus = (ImageView) findViewById(R.id.video_status);

阿卡您呼叫view.setVisiblility()其實之前initialisint認爲 PS:在你BaseActivity ID已從onCreat()邏輯移動到onStop()處理通信,你知道的onCreate是完成,所有的意見初始化

+0

如何更改我的代碼?你可以告訴我@ X3Btel – BekaKK

+0

@Baggio我編輯了 – X3Btel

+0

我不明白它可以讓我看看完整的源代碼嗎? @ X3Btel – BekaKK

相關問題