Android Studio 按下“关于”菜单后崩溃

gcmastyq  于 2022-12-19  发布在  Android
关注(0)|答案(1)|浏览(128)

我不知道我有什么错误。我已经把主活动放在新意图和这个之间了。
这是我的mainactivity.java

public boolean onCreateOptionsMenu(Menu menu){
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.menu, menu);
    return true;
}

public boolean onOptionsItemSelected(MenuItem item){
  switch (item.getItemId()){
      case R.id.:
          Intent intent = new Intent(MainActivity.this, AboutActivity.class);
          startActivity(intent);
          break;
  }
  return super.onOptionsItemSelected(item);
}

这是menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/about"
        android:title="About"
        app:showAsAction="never"/>
</menu>

这里是AboutActivity.java

package com.example.zakatcalculator;

import android.os.Bundle;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

public class AboutActivity extends AppCompatActivity {

    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_about);
        textView = findViewById(R.id.textViewLink);
        textView.setMovementMethod(LinkMovementMethod.getInstance());
    }
}

这是安卓清单. xml

<activity
    android:name=".AboutActivity"
    android:exported="false">

</activity>

当我按下关于按钮,然后它崩溃。

zyfwsgd6

zyfwsgd61#

public boolean onOptionsItemSelected(MenuItem item){
switch (item.getItemId()){
  case R.id.about:
      Intent intent = new Intent(MainActivity.this, AboutActivity.class);
      startActivity(intent);
      break;
}
return super.onOptionsItemSelected(item);
}

R.id.替换为该R.id. about,然后重试。

相关问题