【问题标题】:Why does the help button in my toolbar is not working为什么我的工具栏中的帮助按钮不起作用
【发布时间】:2021-06-06 04:25:50
【问题描述】:

当我单击“帮助”时,应用突然没有响应。 我在应用程序中添加了一些工具栏,关于工作正常,但它出现了应用程序突然停止的帮助按钮。

这是我的代码 mainactivity 代码

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_detection);
    //this hides the back button and I thank you
    getSupportActionBar().setDisplayHomeAsUpEnabled(false);
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setTitle(getString(R.string.progress_dialog_title));

    // Disable button "detect" as the image to detect is not selected.
    setDetectButtonEnabledStatus(false);

    LogHelper.clearDetectionLog();
}

   

     @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater menuInflater = getMenuInflater();
        menuInflater.inflate(R.menu.menu, menu);
        return true;
    }
        @SuppressLint("NonConstantResourceId")
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
    
            switch(item.getItemId()){
                case R.id.menuAbout:
   
                    View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
    
                    AlertDialog.Builder builder = new AlertDialog.Builder(this);
                    builder.setIcon(R.drawable.smile);
                    builder.setTitle(R.string.app_name);
                    builder.setView(messageView);
                    builder.create();
                    builder.show();
                    break;
    
                case R.id.menuHelp:
                  
                    Intent help = new Intent(this, HelpActivity.class);
                    startActivity(help);
                    break;
    
    
            }
            return true;
        }

那么这是帮助活动代码

    public class HelpActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_help);
        getSupportActionBar().hide();

       // Toolbar toolbar = (Toolbar)findViewById(R.id.app_bar);
       // setSupportActionBar(toolbar);


        ActionBar bar = getSupportActionBar();
        bar.setDisplayHomeAsUpEnabled(true);
        bar.setTitle(R.string.help);
    }
}

然后这里是菜单的xml

    <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:id="@+id/menuHelp"
        android:title="Help"
        android:icon="@drawable/ic_help_black_24dp"/>

    <item
        android:id="@+id/menuAbout"
        android:title="About"
        android:icon="@drawable/ic_info_black_24dp"/>
  <!--  <item
        android:id="@+id/menuLogout"
        android:title="Logout" /> -->

</menu>

那么这是activity_help的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=".ui.HelpActivity">



    <TextView
        android:id="@+id/help_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/help_tip"
        android:textSize="17sp"
        android:textColor="@android:color/black"
        android:layout_margin="10dp"
       />

</RelativeLayout>

我该怎么办?请帮帮我。谢谢

This is the help button

【问题讨论】:

  • 您好,如果对您有帮助,请考虑接受我的回答。谢谢。

标签: java android android-studio android-layout android-toolbar


【解决方案1】:
case R.id.menuHelp:
                   // Toast.makeText(this, "You clicked settings", Toast.LENGTH_SHORT).show();
                    Intent help = new Intent(this, HelpActivity.class);
                    startActivity(help);
                    break;

问题

关键字this 指的是菜单帮助项,而不是活动。由于 Intent 的第一个参数是 packageContext,this 将不起作用。

解决方案

只需将 Intent 中的 this 更改为 MainActivity.this 即可将该活动称为包上下文。

查看docs 了解更多信息。

【讨论】:

  • getApplicationContext() 也应该可以工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-03-28
  • 1970-01-01
相关资源
最近更新 更多