动态设置可见的ToolBar菜单

时间:2016-03-09 18:53:57

标签: android toolbar

我有2个EditText和1个不可见的工具栏项: <?xml version =" 1.0"编码=" UTF-8"> < menu xmlns:android =" http://schemas.android.com/apk/res/android"     的xmlns:应用程式=" HTTP://schemas.android.com/apk/res-auto"     的xmlns:工具=" HTTP://schemas.android.com/tools"     工具:上下文=" .LoginActivity">     <项         机器人:ID =" @ + ID / loginim"         机器人:orderInCategory =" 300"         机器人:标题=" @串/下一个"         机器人:可见="假"         机器人:图标=" @可绘制/ back_icon"         应用程式:showAsAction ="总是" /> < /菜单> 我在EditText fiels不为空时尝试setVisible项目菜单,但每次应用程序崩溃.. 我的TextWatcher: private TextWatcher textWatcher = new TextWatcher(){     @覆盖     public void beforeTextChanged(CharSequence charSequence,int i,int i2,int i3)     {     }     @覆盖     public void onTextChanged(CharSequence charSequence,int i,int i2,int i3){}     @覆盖     public void afterTextChanged(可编辑的可编辑){         checkFieldsForEmptyValues();     }}; 检查字段编辑文本: boolean checkFieldsForEmptyValues(){         String s1 = log.getText()。toString();         String s2 = pass.getText()。toString();         if(!s1.equals("")&&!s2.equals("")){             MenuItem nextItem = myMenu.findItem(R.id.loginim);             查看nextItemView = findViewById(nextItem.getItemId());             nextItemView.setVisibility(View.VISIBLE);             返回true;         } else {             返回false;         }     } 和方法onCreate: @覆盖     protected void onCreate(Bundle savedInstanceState){         super.onCreate(savedInstanceState);         的setContentView(R.layout.login_layout);         工具栏mActionBarToolbar =(工具栏)findViewById(R.id.toolbar_actionbar);         setSupportActionBar(mActionBarToolbar);          log =(EditText)findViewById(R.id.et_log);          pass =(EditText)findViewById(R.id.et_pass);         log.addTextChangedListener(textWatcher);         pass.addTextChangedListener(textWatcher);         checkFieldsForEmptyValues();     }     @覆盖     public boolean onCreateOptionsMenu(Menu menu){         getMenuInflater()。inflate(R.menu.login_menu,menu);         返回true;     } 这段代码有什么问题? 更新LOg: 03-09 18:25:01.492 6276-6276 / comtime88.vk.myappE / AndroidRuntime:FATAL EXCEPTION:main     显示java.lang.NullPointerException             at comtime88.vk.myapp.LoginActivity.checkFieldsForEmptyValues(LoginActivity.java:56)             at comtime88.vk.myapp.LoginActivity $ 1.afterTextChanged(LoginActivity.java:37)             在android.widget.TextView.sendAfterTextChanged(TextView.java:7247)             在android.widget.TextView $ ChangeWatcher.afterTextChanged(TextView.java:8891)             在android.text.SpannableStringBuilder.sendAfterTextChanged(SpannableStringBuilder.java:970)             在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:497)             在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:435)             在android.text.SpannableStringBuilder.replace(SpannableStringBuilder.java:30)             在android.text.method.QwertyKeyListener.onKeyDown(QwertyKeyListener.java:222)             在android.text.method.TextKeyListener.onKeyDown(TextKeyListener.java:136)             在android.widget.TextView.doKeyDown(TextView.java:5385)             在android.widget.TextView.onKeyDown(TextView.java:5204)             在android.view.KeyEvent.dispatch(KeyEvent.java:2609)             在android.view.View.dispatchKeyEvent(View.java:7205)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)             在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)             在com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)             在android.app.Activity.dispatchKeyEvent(Activity.java:2370)             在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1847)             在android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)             在android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)             在android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:2818)             在android.os.Handler.dispatchMessage(Handler.java:99)             在android.os.Looper.loop(Looper.java:137)             在android.app.ActivityThread.main(ActivityThread.java:5041)             at java.lang.reflect.Method.invokeNative(Native Method)             在java.lang.reflect.Method.invoke(Method.java:511)             在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)             在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)             at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:0)

我想崩溃的线是这样的:

MenuItem nextItem = myMenu.findItem(R.id.loginim);
View nextItemView = findViewById(nextItem.getItemId());

我认为你应该在myMenu之后加findViewById()如下:

View nextItemView = myMenu.findViewById(nextItem.getItemId());

让我知道它是否有效,如果它没有放入logCat而我会重做答案:)

答案 1 :(得分:0)

<强>解决方案:

Sentence = [x for x in range(len(Sentence))]

而不是

 MenuItem nextItem = myMenu.findItem(R.id.loginim);
        nextItem.setVisible(true);