gpt4 book ai didi

android menuItem文本更改

转载 作者:行者123 更新时间:2023-11-30 04:54:43 25 4
gpt4 key购买 nike

我试图根据用户是否已注册在应用程序启动时更改菜单项的标题,但出现此错误:

Process: com.cloud.services, PID: 25186
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cloud.services/com.cloud.services.MainActivity}:
java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2345)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405)
at android.app.ActivityThread.access$800(ActivityThread.java:149)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:211)
at android.app.ActivityThread.main(ActivityThread.java:5315)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736)
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference
at com.cloud.services.MainActivity.onCreate(MainActivity.java:35)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2405) 
at android.app.ActivityThread.access$800(ActivityThread.java:149) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1324) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:211) 
at android.app.ActivityThread.main(ActivityThread.java:5315) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:941) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:736) 

我的 MainActivity 代码:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";

private SectionsPageAdapter mSectionsPageAdapter;
private FirebaseAuth auth;
private Menu menu;
private ViewPager mViewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


auth = FirebaseAuth.getInstance();

if (auth.getCurrentUser() != null) {
MenuItem item= menu.findItem(R.id.action_settings);
item.setTitle("LogOut");
}


setContentView(R.layout.activity_main);


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.loginmenu, menu);
return true;
}
//and this to handle actions
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
if (item.getTitle().equals("Login\SignUp**strong text**")){
Intent intent4 = new Intent(MainActivity.this, LoginActivity.class);
startActivity(intent4);}
else{
auth.signOut();
}
return true;
}
return super.onOptionsItemSelected(item);
}

}

最佳答案

根据 Android 生命周期,onCreate() 在菜单展开之前首先被调用。因此,在 onCreate() 方法中引用菜单项将导致您的应用程序崩溃,因为菜单尚未展开。这就是为什么你得到这个:

Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.view.MenuItem android.view.Menu.findItem(int)' on a null object reference

要解决此问题,只需覆盖打开选项菜单时调用一次的 onPrepareOptionsMenu(Menu menu),并将您的登录逻辑检查移动到该方法中。像这样:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {

MenuItem item= menu.findItem(R.id.action_settings);
if (auth.getCurrentUser() != null) {
item.setTitle("LogOut");
}
return super.onPrepareOptionsMenu(menu);
}

编辑:此外,menu 将为空,因为您没有为其分配任何内容。因此,对其调用 findItem(id) 会使您的应用程序崩溃。从您的代码中删除全局 menu,因为在这种情况下添加 onPrepareOptionsMenu(menu) 会使其变得多余。

如果您需要进一步说明,请在评论部分告诉我。

关于android menuItem文本更改,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59447788/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com