请任何人可以帮助我解决此错误
这是MainActivity.java文件:
public class MainActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private Toolbar mToolbar;
private ViewPager myViewPager;
private TabLayout myTabLayout;
private TabsPagerAdapter mTabsPagerAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
myViewPager = (ViewPager)findViewById(R.id.main_tabs_pager);
mTabsPagerAdapter = new TabsPagerAdapter(getSupportFragmentManager());
myViewPager.setAdapter(mTabsPagerAdapter);
myTabLayout = (TabLayout)findViewById(R.id.main_tabs);
myTabLayout.setupWithViewPager(myViewPager);
mToolbar = (Toolbar)findViewById(R.id.main_app_bar);
setSupportActionBar(mToolbar);
getSupportActionBar().setTitle("myChat");
}
@Override
protected void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if (currentUser == null){
LogOutUser();
}
}
private void LogOutUser() {
Intent startPageIntent = new Intent(MainActivity.this, StartPageActivity.class);
startPageIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(startPageIntent);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu,menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item){
super.onOptionsItemSelected(item);
if(item.getItemId()== R.id.mainlogout_button){
mAuth.signOut();
LogOutUser();
}
return true;
}
}
这是错误:
由于:java.lang.NullPointerException:尝试调用虚拟 方法“无效” android.support.v7.app.ActionBar.setTitle(java.lang.CharSequence)'上 位于的空对象引用 yassine.com.mychat.MainActivity.onCreate(MainActivity.java:40)
答案 0 :(得分:0)
这是MainActivity.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="yassine.com.mychat.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/appBarLayout">
<include
layout="@layout/app_bar_layout"
android:id ="@+id/main_page_toolbar">
</include>
<android.support.design.widget.TabLayout
android:id="@+id/main_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"></android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/main_tabs_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="@+id/appBarLayout">
</android.support.v4.view.ViewPager>
</RelativeLayout>