簡體   English   中英

為什么我的工具欄中的幫助按鈕不起作用

[英]Why does the help button in my toolbar is not working

當我單擊幫助時,應用程序突然沒有響應。 我在應用程序中添加了一些工具欄,關於工作正常,但它帶有應用程序突然停止的幫助按鈕。

這是我的代碼 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>

那么這是活動幫助的 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>

我應該怎么辦? 請幫幫我。 謝謝

這是幫助按鈕

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 中將其更改為 MainActivity.this 即可this活動稱為 package 上下文。

查看 文檔以獲取更多信息。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM