首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >androidx.appcompat.widget上的空引用

androidx.appcompat.widget上的空引用
EN

Stack Overflow用户
提问于 2021-12-28 12:02:31
回答 1查看 77关注 0票数 0

我一直收到这个错误,这使我无法运行我的代码,我不知道这个ContentFrameLayout是什么。我的建筑是最新的。我多次使缓存失效,但仍然会出现错误。我重新启动了android工作室,也不得不重新启动我的系统。

代码语言:javascript
运行
复制
Attempt to invoke virtual method 'void androidx.appcompat.widget.ContentFrameLayout.setId(int)' on a null object reference
        at androidx.appcompat.app.AppCompatDelegateImpl.createSubDecor(AppCompatDelegateImpl.java:1000)
        at androidx.appcompat.app.AppCompatDelegateImpl.ensureSubDecor(AppCompatDelegateImpl.java:815)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:703)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:195)
        at com.me.recyclerview.MainActivity.onCreate

MainActivity

代码语言:javascript
运行
复制
@Override
    protected void onCreate(Bundle savedInstanceState) {
   
        super.onCreate(savedInstanceState);
        

     

       if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
                    ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
    
                } else {
    
                   
                    showAll()
    
    
    
                }
}
    
       
 public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        switch (requestCode){
            case 1:
                if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED){
                    Toast.makeText(MainActivity.this,"permission 1 granted",Toast.LENGTH_LONG).show();
                    if(ContextCompat.checkSelfPermission(MainActivity.this,Manifest.permission.WRITE_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
                        showAll();
                    }
}




public void showAll(){
       
        setContentView(R.layout.activity_main);
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);

        toolbarInitialize();
        initialize();
          
        

    }

这是activity_main.xml for MainActivity

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="left"
android:fillViewport="false"
android:fitsSystemWindows="true"
tools:context=".MainActivity">


    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:orientation="vertical"


        >

        <include
            android:id="@+id/include_toolbar"
            layout="@layout/custom_toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:transitionName="transitionToolbar"
            />
        <com.google.android.material.tabs.TabLayout
            android:id="@+id/TabLayout"
            style="@style/mytab"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:minHeight="?attr/actionBarSize"
            app:tabIndicatorColor="#E5F1EF"
            app:tabMode="scrollable"
            app:tabTextColor="@android:color/black"
            android:transitionName="transitionTabLayout"

            >



            <com.google.android.material.tabs.TabItem
                android:id="@+id/Videos"


                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Videos" />

        </com.google.android.material.tabs.TabLayout>

        <androidx.viewpager.widget.ViewPager
            android:id="@+id/Viewpager"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

        </androidx.viewpager.widget.ViewPager>



    </LinearLayout>


<com.google.android.material.navigation.NavigationView
    android:id="@+id/mainNav"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/navigation_drawer_layout"

    />

</androidx.drawerlayout.widget.DrawerLayout>
EN

回答 1

Stack Overflow用户

发布于 2021-12-28 12:26:00

您必须在外部和if条件之前使用setContentView()。当if条件为true时,没有为您的活动设置视图,它将崩溃。在super.onCreate(savedInstanceState)之后使用它并测试代码:

代码语言:javascript
运行
复制
        setContentView(R.layout.activity_main);

        if (ContextCompat.checkSelfPermission(MainActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
            ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
        } else {
            showAll()
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70506795

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档