有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java从超类访问视图

我试图将处理导航抽屉的代码集中到一个所有活动都扩展的超类中。超级类本身扩展了活动,但我似乎不知道如何访问子类的视图(findviewbyid返回null)

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

超类oncreate:

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

子类

public class CheckInActivity extends CommonCode {

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

    setContentView(R.layout.activity_main);
}

共 (1) 个答案

  1. # 1 楼答案

    我要做的是为每个子活动提供单独的xml,但导航抽屉的所有xml ID都是相同的

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

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

    您的视图可能会返回null,因为您在运行子类的onCreate之前运行了超类的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();
            } 
        }