我一直收到这个错误,这使我无法运行我的代码,我不知道这个ContentFrameLayout是什么。我的建筑是最新的。我多次使缓存失效,但仍然会出现错误。我重新启动了android工作室,也不得不重新启动我的系统。
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
@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
<?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>
发布于 2021-12-28 12:26:00
您必须在外部和if条件之前使用setContentView()
。当if条件为true时,没有为您的活动设置视图,它将崩溃。在super.onCreate(savedInstanceState)
之后使用它并测试代码:
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()
}
https://stackoverflow.com/questions/70506795
复制相似问题