2013-12-16 95 views
0

我實現了一個底部按鈕欄(就像iPhone中的標籤欄控制器)。爲此,我創建了一個公共佈局(button_bar.xml,5個圖像按鈕)幷包含在其他活動xml文件中。並且爲了管理點擊操作,我創建了一個從Activity擴展的BaseActivity.java並執行點擊操作。並且我擴展了其他需要使用此BaseActivity的按鈕欄的活動,該工作正常。現在我想包含一個選定的狀態到這些按鈕,但是當我訪問基本活動中的按鈕時,它會給出一個空指針錯誤。我該如何解決這個問題。如何在父活動類(Android)中獲取子活動視圖?

button_bar.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/bottomButtonBar" 
style="@android:style/ButtonBar" 
... > 
<ImageButton 
    android:id="@+id/btnGuests" 
    android:onClick="showAllGuests" 
    android:src="@drawable/ic_guest_list" /> 

<ImageButton 
    android:id="@+id/btnAddGuest" 
    android:onClick="selectGuestType" 
    android:src="@drawable/ic_add_guest" /> 

<ImageButton 
    android:id="@+id/btnAddParty" 
    android:onClick="showAddParty" 
    android:src="@drawable/ic_add_party" /> 
    .... 
</LinearLayout> 

public class BaseActivity extends Activity { 
    // common to other activities. 
    public void showAddParty(View view) { 
    //showAddParty is one one of the buttons click method. 4 more buttons are there 
     Intent intent = new Intent(this, AddPartyActivity.class); 
     startActivity(intent); 

     // I can get "view.findViewById(R.id.btnAddParty)" here 
     // but I can't get "findViewById(R.id.btnAddGuest)" here. how this possible 
    } 
} 

public class AddPartyActivity extends BaseActivity{ 
    .... 
} 

在這裏,我可以從參數查看相應的視圖,並更改backgroundIbageSource。但是當它轉到從Baseactivity繼承的「AddPartyActivity」時,新圖像會被舊圖像替換。我如何在BaseActivity中實現所選功能?

+0

讓你的父活動和訪問的對象從它 – Piyush

+0

@PiyushGupta使活動的對象???怎麼樣? –

回答

0

您可以在BaseActivity中使用booloean來保存按鈕的狀態(單擊或不按鈕),並在AddPropertyActivity的onCreate()方法中檢查這些變量的值。

public class BaseActivity extends Activity { 
    protected static boolean isButton1Clicked = false; 
    protected static boolean isButton2Clicked = false; 
    protected static boolean isButton3Clicked = false; 
    protected static boolean isButton4Clicked = false; 
    ..... 
    public void showAddParty(View view) { 
     isButton1Clicked = true; 
     //showAddParty is one one of the buttons click method. 4 more buttons are there 
     Intent intent = new Intent(this, AddPartyActivity.class); 
     startActivity(intent); 
    } 
} 


public class AddPartyActivity extends BaseActivity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_layout); 
     Button button1 = (Button)findViewById(R.id.button1); 
     if(isButton1Clicked) { 
      button1.setBackground(R.drawable.clicked); 
     } else { 
      button1.setBackground(R.drawable.not_clicked); 
     } 
     ...... 
     ...... 
     } 
} 

並記住所有布爾變量必須是static變量。 (每個對象都有自己的實例變量副本,但所有對象都會共享一個靜態成員的副本)。

public class BaseActivity extends Activity { 

    protected static boolean isButton1Clicked = false; 
    protected static boolean isButton2Clicked = false; 
    protected static boolean isButton3Clicked = false; 
    protected static boolean isButton4Clicked = false; 
    protected Button button1; 
    protected Button button2; 
    protected Button button3; 
    protected Button button4; 

    protected void checkButtonsState() { 
     if (isButton1Clicked) { 
      button1.setBackgroundResource(R.drawable.image1); 
     } else { 
      button1.setBackgroundResource(R.drawable.image2); 
     } 
     .......... 
    } 

    public void showAddParty(View view) { 
     isButton1Clicked = true; 
     // showAddParty is one one of the buttons click method. 4 more 
     // buttons are there 
     Intent intent = new Intent(this, AddPartyActivity.class); 
     startActivity(intent); 
    } 
} 

public class AddPartyActivity extends BaseActivity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     button1 = (Button) findViewById(R.id.button); 
     button2 = (Button) findViewById(R.id.item1); 
      ..... 
     checkButtonsState(); 
    } 
} 
+0

是的,這是一個解決方案,但問題是許多活動繼承基地活動,所以我需要在每個地方編寫代碼。這就是爲什麼我在BaseActivity本身看起來可以做的事情。 – bolt123

+0

@ bolt123檢查我編輯的答案...... –

+0

@ bolt123如果所有活動佈局都有相同的按鈕,可以將初始化部件移動到基​​本活動(在基本活動中寫入一個方法,如initializeButtons()並在ChildActivity中調用該方法設置contentView –

0

從因爲您使用view.findViewById(),但對象是趕上click事件的看法變量對應(看在你的XML中,'android:onClick="showAddParty"')

所以來你的錯誤,你的參數有查看實例直接指向btnAddParty按鈕。 就是爲什麼你可以用findViewById訪問btnAddParty ID,但不btnAddGuest ID。

您可以通過直接findViewById作爲訪問所有視圖層次結構活動類的方法:

View btAddParty = findViewById(R.id.btnAddParty) 

View btAddGuest = view.findViewById(R.id.btnAddGuest) 

現在,你可以有一個完整的解決方案,以實現在BaseActivity一種特殊的方法(setMyInternalContent下面的示例中),並保持每個按鈕的實例,之後,你就必須更新那裏點擊動作狀態:

public class BaseActivity extends Activity { 
    protected Button mBtAddParty; 
    protected Button mBtAddGuest; 
    // ... 

    protected void setMyContentView(int resId) { 
     setContentView(resId); 
     mBtAddParty = (Button)findViewById(R.id.btnAddParty);    
     mBtAddParty = (Button)findViewById(R.id.btnAddGuest); 
     // ...    
    } 

    public void showAddParty(View view) { 
     mBtAddParty.setClickable(true); 
     mBtAddGuest.setClickable(false); 
     // ... 

     //showAddParty is one one of the buttons click method. 4 more buttons are there 
     Intent intent = new Intent(this, AddPartyActivity.class); 
     startActivity(intent); 
    } 
} 


public class AddPartyActivity extends BaseActivity { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setMyContentView(R.layout.activity_layout); 

     // Do not forget to set initial button state:    
     mBtAddParty.setClickable(true); 
     mBtAddGuest.setClickable(false); 
     // ... 
    } 
} 
相關問題