Java NavigationDrawer的BaseActivity在访问视图时提供NullPointerException
我一直试图将Java NavigationDrawer的BaseActivity在访问视图时提供NullPointerException,java,android,navigation-drawer,Java,Android,Navigation Drawer,我一直试图将导航抽屉添加到多个活动,但我一直遇到错误。我读了几乎所有我能找到的东西,似乎最简单的方法就是创建一个BaseActivity,并扩展所有需要NavigationDrawer的其他活动。这是我根据在这里找到的教程整理的 BaseActivity.java package com.example.eddie.drawerapp2; //I did not include the imports to make this shorter abstract publi
导航抽屉
添加到多个活动
,但我一直遇到错误。我读了几乎所有我能找到的东西,似乎最简单的方法就是创建一个BaseActivity
,并扩展所有需要NavigationDrawer
的其他活动。这是我根据在这里找到的教程整理的
BaseActivity.java
package com.example.eddie.drawerapp2;
//I did not include the imports to make this shorter
abstract public class BaseActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener
{
protected FrameLayout frameLayout;
private DrawerLayout drawer;
private NavigationView navView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
// Here, ask the derived class for the layout resource to inflate into the content frame layout
View stubView = inflater.inflate(getContentLayoutResId(), frameLayout, false);
frameLayout.addView(stubView, lp);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.layout_drawer);
android.support.v7.app.ActionBarDrawerToggle toggle = new android.support.v7.app.ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
navView = (NavigationView)findViewById(R.id.nav_view);
navView.setNavigationItemSelectedListener(this);
}
abstract protected int getContentLayoutResId();
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
//skipped imports to make this shorter
public class MainActivity extends BaseActivity{
private DrawerLayout drawer;
private NavigationView navView;
@Override
protected int getContentLayoutResId() {
return R.layout.activity_main;
}
}
base\u activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:layout_gravity="left">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
活动\u main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/layout_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:layout_gravity="left">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer"
/>
</android.support.v4.widget.DrawerLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="TextView" />
</RelativeLayout>
它指向BaseActivity
中的这一行:
frameLayout.addView(stubView, lp);
首先,尝试调用自定义的
setContentView
:
@Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
super.onCreate(savedInstanceState);
super.onCreateDrawer();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.base_activity);
onCreateDrawer();
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
// Here, ask the derived class for the layout resource to inflate into the content frame layout
View stubView = inflater.inflate(getContentLayoutResId(), frameLayout, false);
frameLayout.addView(stubView, lp);
}
abstract protect int getContentLayoutResId();
但是空值检查frameLayout
:
@Override
public void setContentView(int layoutResID) {
if (frameLayout != null) {
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup.LayoutParams lp = new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
View stubView = inflater.inflate(layoutResID, frameLayout, false);
frameLayout.addView(stubView, lp);
}
}
直到稍后才实际初始化:
protected void onCreateDrawer() {
frameLayout = (FrameLayout)findViewById(R.id.content_frame);
}
所以,当然,你没有膨胀一个布局,从中得到你的抽屉布局,因此你的崩溃
我建议你花点时间学习如何做
希望有帮助
更新: 好的,我想把你的问题指给你会帮助你自己解决这个问题。以下是我的建议: BaseActivity应该设置派生类将膨胀到的基本布局(请注意,我们没有覆盖
setContentView
):
那么你的主要活动就是:
@Override
protected int getContentLayoutResId() {
return R.layout.activity_main;
}
你为什么不使用片段呢?我强烈建议你玩片段,而不是活动。活动不应该共享uicomponents@AshishKumar当然,为什么不呢?我愿意接受任何事情,只要它有效。因此,如果我这样做,那么每个
NavigationDrawer
项目是一个单独的片段,还是每个拥有导航抽屉的活动都是一个活动中显示的片段。我想你可以把这个作为一个答案,再加上一点更详细的解释。谢谢这篇文章讨论了这个问题,但没有说明如何解决它。好的,我更新了我的答案。将来我建议你不要在别人花时间试图帮助你的时候否决他们的答案,因为这让我重新考虑是否会浪费时间更新我的答案。哦,我没有足够的分数来否决投票。如果这能解决我的问题,我会投票的。终于成功了,非常感谢。我个人对此表示赞赏。经过两周多的试用和出错,终于可以正常工作了。我应该将通常会放入onCreate()
的代码放在哪里?它只是getContentLayoutResId()
?