2013-12-16 97 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); 
     // ... 
    } 
} 
相关问题