我实现了一个底部按钮栏(就像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中实现所选功能?
让你的父活动和访问的对象从它 – Piyush
@PiyushGupta使活动的对象???怎么样? –