从超类访问视图

时间:2013-12-17 20:18:59

标签: java android inheritance

我正在尝试将用于处理导航抽屉的代码集中到一个所有活动都扩展的超类中。超类本身扩展了Activity,但我似乎无法弄清楚如何访问子类的视图(findviewbyid返回null)。

如何从CommonCode访问CheckinActivity中膨胀的视图?

Superclass oncreate:

public class CommonCode extends Activity{
....
    @Override
public void onCreate(Bundle b){
super.onCreate(b);
 context=getBaseContext();

SubClass

public class CheckInActivity extends CommonCode {

@Override
public void onCreate(Bundle b) {
    super.onCreate(b);

    setContentView(R.layout.activity_main);
}

1 个答案:

答案 0 :(得分:2)

我所做的是为每个子活动分配单独的XML,但导航抽屉的所有xml ID都是相同的。

接下来,当您启动扩展活动的任何子类时,在包中传递带有contextView id - R.layout.MY_SUB_ACTIVITY的包,并实际为超类中的每个子活动设置内容视图。

之后,你可以使用findViewByID找到你的抽屉,在超类中设置它们,然后从它自己的类中处理你的子类的视图。

您的视图很可能返回null,因为您在子类之前运行超类的onCreate - 因此尚未调用setContentView(因此findViewByID将无效)

编辑:

此外,您无需为所有导航抽屉ID命名相同 - 我只是认为使用<include>标记更容易,以最大限度地减少xml文件中的冗余。

例如:

子类 -

public class InviteActivity extends MainActivity {

public static void openActivity(Activity from_activity) {
    Intent intent = new Intent(from_activity, InviteActivity.class);

    Bundle bundle = new Bundle();
    bundle.putInt(MainActivity.KEY_LAYOUT_ID, R.layout.invite_activity);
    bundle.putBoolean(MainActivity.KEY_HAS_LEFT_DRAWER, true);
    bundle.putBoolean(MainActivity.KEY_HAS_RIGHT_DRAWER, false);

    intent.putExtra(MainActivity.KEY_MAIN_BUNDLE, bundle);
    from_activity.startActivity(intent);
}

@Override
protected void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);
      .....

    }

超类:

public abstract class MainActivity extends Activity {

    /*
     * Bundle keys specifying if the new activity inheriting from MainActivity
     * has a left and/or right navigation drawer.
     */
    public static final String KEY_MAIN_BUNDLE = "com.smashingboxes.bevii.MAIN_BUNDLE_KEY";
    public static final String KEY_HAS_RIGHT_DRAWER = "com.smashingboxes.bevii.HAS_RIGHT_DRAWER";
    public static final String KEY_HAS_LEFT_DRAWER = "com.smashingboxes.bevii.HAS_LEFT_DRAWER";
    public static final String KEY_LAYOUT_ID = "com.smashingboxes.bevii.LAYOUT_ID_KEY";
    public static final String KEY_SECTION_TITLE = "comm.smashingboxes.bevii.SECTION_TITLE";

    /*
     * Boolean indicators for each activity specifying their active navigation
     * drawers.
     */
    private boolean hasRightDrawer;
    private boolean hasLeftDrawer;

    /* The current activity's layout id for setContentView */
    private String sectionTitle;
    private int contentID;

    /* UI Elements */
    private DrawerLayout mDrawerLayout;
    private ActionBarDrawerToggle mDrawerToggle;

    /* Reference to the ActionBar title to toggle text on drawer changes. */
    protected CustomTextView mTitle;

    private ListView mDrawerListLeft;
    private LeftNavigationAdapter leftAdapter;
    private ListView mDrawerListRight;
    private LeftMessagesAdapter rightAdapter;

    /**
     * Handles the navigation drawer set up for each class inheriting from main
     * activity. A bundle must be specified indicating the number of navigation
     * drawers present in the activity's UI.
     */
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (!getIntent().hasExtra(KEY_MAIN_BUNDLE))
            throw new IllegalArgumentException(
                    "Cannot access a subclass of MainActivity without including a main bundle.");

        Bundle mainBundle = getIntent().getBundleExtra(KEY_MAIN_BUNDLE);
        hasRightDrawer = mainBundle.getBoolean(KEY_HAS_RIGHT_DRAWER);
        hasLeftDrawer = mainBundle.getBoolean(KEY_HAS_LEFT_DRAWER);
        contentID = mainBundle.getInt(KEY_LAYOUT_ID);
        sectionTitle = mainBundle.getString(KEY_SECTION_TITLE);

        setContentView(contentID);

        if (hasRightDrawer && hasLeftDrawer) {
            handleLeftNavigationDrawer();
            handleRightNavigationDrawer();
        } else if (hasLeftDrawer) {
            handleLeftNavigationDrawer();
        } 
    }