Android 在emulator和真实设备中启动新活动时,应用程序崩溃
当我开始一个新的活动,但应用程序崩溃以下错误Android 在emulator和真实设备中启动新活动时,应用程序崩溃,android,firebase,firebase-realtime-database,Android,Firebase,Firebase Realtime Database,当我开始一个新的活动,但应用程序崩溃以下错误 FATAL EXCEPTION: main Process: com.food.sheenishere.stark, PID: 9976 java.lang.
FATAL EXCEPTION: main
Process: com.food.sheenishere.stark, PID: 9976
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.food.sheenishere.stark/com.food.sheenishere.stark.home}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.food.sheenishere.stark.home.onCreate(home.java:84)
at android.app.Activity.performCreate(Activity.java:6679)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
home.java
下面的某个地方有错误。这是家庭活动守则的一部分。登录后,当我尝试打开此活动时,应用程序崩溃,错误如上所示。该应用程序之前工作正常,但突然在虚拟设备和真实设备上崩溃
public class home extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
FirebaseDatabase database;
DatabaseReference category;
TextView txtFullName;
RecyclerView recycler_menu;
RecyclerView.LayoutManager layoutManager;
private ProgressDialog progressDialog;
FirebaseRecyclerAdapter<Category,MenuViewHolder> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setTitle("Menu");
setSupportActionBar(toolbar);
progressDialog=new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();
//init firebase
database=FirebaseDatabase.getInstance();
category=database.getReference("Category");
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent cartIntent = new Intent(home.this, Cart.class);
startActivity(cartIntent);
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//set name for user
View headerView = navigationView.getHeaderView(0);
txtFullName = (TextView)findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
公共类主页扩展了AppCompative活动
实现NavigationView.OnNavigationItemSelectedListener{
Firebase数据库;
数据库参考类别;
TextView txtFullName;
RecyclerView recycler_菜单;
RecyclerView.LayoutManager LayoutManager;
私有进程对话;
FirebaseRecyclerAdapter适配器;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
Toolbar Toolbar=(Toolbar)findViewById(R.id.Toolbar);
工具栏。设置标题(“菜单”);
设置支持操作栏(工具栏);
progressDialog=新建progressDialog(此);
progressDialog.setMessage(“加载…”);
progressDialog.show();
//init firebase
database=FirebaseDatabase.getInstance();
category=database.getReference(“category”);
FloatingActionButton fab=(FloatingActionButton)findViewById(R.id.fab);
fab.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图){
Intent cartIntent=新Intent(home.this,Cart.class);
星触觉(cartIntent);
}
});
抽屉布局抽屉=(抽屉布局)findViewById(R.id.抽屉布局);
ActionBarDrawerToggle切换=新建ActionBarDrawerToggle(
这,抽屉,工具栏,R.string.navigation\u drawer\u open,R.string.navigation\u drawer\u close);
抽屉。设置抽屉定位器(开关);
toggle.syncState();
NavigationView NavigationView=(NavigationView)findViewById(R.id.nav_视图);
navigationView.setNavigationItemSelectedListener(此);
//为用户设置名称
View headerView=navigationView.getHeaderView(0);
txtFullName=(TextView)findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
尝试以下代码
txtFullName = (TextView)findViewById(R.id.txtFullName);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
不要使用下面的行
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
txtFullName.setText(Common.currentUser.getName());
试试下面的代码
txtFullName = (TextView)findViewById(R.id.txtFullName);
txtFullName = (TextView)headerView.findViewById(R.id.txtFullName);
发布您的活动\ u home.xml请发布您的xml文件,我猜在布局R.layout.activity\ u home中找不到R.id.txtFullName,而是在从中提取headerView的单独xml中。因此它应该是'headerView.findViewById(R.id.textFullName);@Devsil它起作用了,谢谢