我发现了这个错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.kkk.findyourbook / com.kkk.findyourbook.activities.MainActivity}:java.lang.NullPointerException:尝试调用虚方法' android.view .View android.support.v7.widget.Toolbar.findViewById(int)'在空对象引用上
引起:java.lang.NullPointerException:尝试调用虚拟 方法' android.view.View android.support.v7.widget.Toolbar.findViewById(INT)'在null对象上 参考
这很奇怪,因为它刚刚发生,我的意思是我曾经没有任何问题地运行我的应用程序。
我的onCreate()
方法如下所示:
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
getNavigationDrawerItems();
setupToolbar();
setupDrawerToggle();
DrawerItemCustomAdapter adapter = new DrawerItemCustomAdapter(this, R.layout.drawer_item, mNavigationDrawerItems);
drawerList.setAdapter(adapter);
drawerList.setOnItemClickListener(new DrawerItemClickListener());
mActionBar = getSupportActionBar();
mActionBar.setDisplayHomeAsUpEnabled(false);
mActionBar.setHomeButtonEnabled(true);
mActionBar.setDisplayShowTitleEnabled(true);
assert drawerLayout != null;
drawerLayout.addDrawerListener(drawerToggle);
sFragmentManager = getSupportFragmentManager();
// Check that the activity is using the layout version with
// the fragment_container FrameLayout
if (findViewById(R.id.container) != null) {
// However, if we're being restored from a previous state,
// then we don't need to do anything and should return or else
// we could end up with overlapping fragments.
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
Fragment fragment = new SampleFragment();
// In case this activity was started with special instructions from an
// Intent, pass the Intent's extras to the fragment as arguments
// firstFragment.setArguments(getIntent().getExtras());
// Add the fragment to the 'fragment_container' FrameLayout
sFragmentManager.beginTransaction()
.add(R.id.container, fragment)
.commit();
}
}
setupToolbar()
方法:
void setupToolbar() {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
在我的片段中:
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View fragment = inflater.inflate(R.layout.fragment_sample, container, false);
ButterKnife.bind(this, fragment);
toolbarInit();
return fragment;
}
private void toolbarInit() {
mToolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
TextView toolbarTitle = (TextView) mToolbar.findViewById(R.id.toolbarTitle);
toolbarTitle.setText(getActivity().getResources().getString(R.string.cat_my_books));
}
你能帮我解决这个错误吗?
编辑:
工具栏布局:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:local="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="@color/colorPrimary"
local:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
local:popupTheme="@style/ThemeOverlay.AppCompat.Light" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/toolbarIconLeft"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="end"
android:padding="4dp"
android:src="@drawable/logo"
android:visibility="gone"
android:contentDescription="toolbarIcon" />
<TextView
android:id="@+id/toolbarTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center"
android:layout_marginRight="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:gravity="center_horizontal"
android:textSize="18sp"
android:textAllCaps="true"
android:textColor="@android:color/white"
android:textStyle="bold" />
<ImageView
android:id="@+id/toolbarIcon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="end"
android:layout_marginRight="12dp"
android:layout_marginEnd="12dp"
android:visibility="invisible"
android:src="@drawable/logo"
android:contentDescription="toolbarIcon" />
</LinearLayout>
</android.support.v7.widget.Toolbar>