2016-07-24 58 views
0

我正在嘗試從Facebook實現chatHeads。這部分工作。Android聊天頭浮動活動

我希望它的行爲與facebooks聊天頭類似。當你點擊泡泡時,它會顯示一個視圖,然後再次點擊關閉視圖。

到目前爲止,我的問題是決定這個視圖應該是什麼。

我試圖通過以下來完成它。

我決定使用浮動活動,因爲我認爲它會提供最大的靈活性。

這是正確的方法來做到這一點。最後,我想在那裏貼一個視圖尋呼機。

public class BubbleOverlay extends Activity { 

private boolean active = false; 
private View mainView; 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    setContentView(R.layout.overlay_layout); 
    mainView = findViewById(R.id.main_overlay_layout); 
} 

public void setActive(boolean value){ 
    active = value; 
} 

public void hide(){ 
    active = false; 
    mainView.setVisibility(View.INVISIBLE); 
} 

public void show(){ 
    active = true; 
    mainView.setVisibility(View.VISIBLE); 
} 

public boolean getActive(){ 
    return this.active; 
} 

@Override 
protected void onResume() { 
    active = true; 
    super.onResume(); 
} 

@Override 
protected void onPause() { 
    active = false; 
    super.onPause(); 
} 

@Override 
protected void onDestroy() { 
    active = false; 
    super.onDestroy(); 
} 

的Chathead是農具和切換,像這樣的活動:

 overlay = new BubbleOverlay(); 
    Intent intent = new Intent(this, overlay.getClass()).setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP); 
    startActivity(intent); 

    Handler handler = new Handler(); 
    Runnable runnable_longClick = new Runnable() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      overlay.hide(); 
     } 
    }; 
    handler.postDelayed(runnable_longClick, 5000); 

這個崩潰對以下錯誤:

07-24 01:27:33.195 4752-4752/com.some.package E/AndroidRuntime: FATAL EXCEPTION: main 
                   Process: com.some.package, PID: 4752 
                   java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference 
                    at com.some.package.BubbleOverlay.hide(BubbleOverlay.java:34) 
                    at com.some.package.BubbleService$5.run(BubbleService.java:170) 
                    at android.os.Handler.handleCallback(Handler.java:751) 
                    at android.os.Handler.dispatchMessage(Handler.java:95) 
                    at android.os.Looper.loop(Looper.java:154) 
                    at android.app.ActivityThread.main(ActivityThread.java:6044) 
                    at java.lang.reflect.Method.invoke(Native Method) 
                    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

回答