AndroidX:无法解析方法findViewById(工具栏)

时间:2019-06-27 22:17:44

标签: android android-toolbar androidx findviewbyid

我导入了AndroidX库,并且工具栏的findViewById方法不再起作用。

BaseAppCompatActivity.java:

import androidx.coordinatorlayout.widget.CoordinatorLayout;
import com.google.android.material.navigation.NavigationView;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.widget.Toolbar;

FrameLayout lytContent;
    //protected FrameLayout leftDrawer;
    NavigationView leftDrawer;
    //  protected FrameLayout rightDrawer;
    DrawerLayout drawerLayout;
    ActionBarDrawerToggle mDrawerToggle;
    //protected  RelativeLayout lytContents;

final Handler mHandler = new Handler();

protected SwipeRefreshLayout swipeView;
protected CoordinatorLayout coordinatorLayout;
protected Toolbar toolbar;

private MenuItem menuProgress;
private View lytDrawer;
private View lytDrawerHeader;
private View lytProgress;
private ProgressBar progressBase;
private View lytMessage;
private CustomTextView txtMessage;
private ImageView ivMessage;

private ImageView ivUserDP;
private ProfileBean profileBean;
private CustomTextView txtName;
private CustomTextView txtPhone;
private CustomTextView txtEmail;


private boolean isDriverLocationUpdated;
toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);


        //leftDrawer = (FrameLayout) findViewById(R.id.leftDrawer);
        leftDrawer = (NavigationView) findViewById(R.id.navigation_view_base_appcompat);
        //  rightDrawer = (FrameLayout)findViewById(R.id.rightDrawer);
        drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout_appcompat);
        lytContent = (FrameLayout) findViewById(R.id.lyt_contents_appcompat);
        coordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorlayout_base_appcompat);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            toolbar.setPadding(0, App.getStatusBarHeight(getApplicationContext()), 0, 0);
            leftDrawer.setPadding(0, 0, 0, 0);
            getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
        }

getWindow方法也不起作用。我正在使用sdkVersion 28。

  

实现'androidx.legacy:legacy-support-v4:1.0.0'

     

实现'androidx.appcompat:appcompat:1.0.2'

     

实现'com.google.android.material:material:1.0.0'

     

实现'androidx.recyclerview:recyclerview:1.0.0'

     

实现'androidx.cardview:cardview:1.0.0'

2 个答案:

答案 0 :(得分:0)

toolbar = (Toolbar) findViewById(R.id.toolbar);

这是onCreate()方法中的吗?应该的!

答案 1 :(得分:0)

您可以尝试导入此androidx.appcompat.app.AppCompatActivity吗? 对于绑定视图,必须在onCreate方法中进行绑定,或者可以使用butterKnife lib。

我通常创建方法bindView()并将其放在方法onCreate()上。