我正在尝试向我的应用添加一些菜单。这是我的代码:
java代码:
package com.example.excercise;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Date;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView my_text;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
my_text=findViewById(R.id.textView);
my_text.setText(new Date().toString());
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Random random=new Random();
my_text.setTextColor(Color.rgb(random.nextInt(256),random.nextInt(256),random.nextInt(256)));
}
});
button.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Intent intent = new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
return true;
}
});
}
@Override
protected void onResume() {
super.onResume();
Toast.makeText(this, "MainActivity: OnResume()", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
}
main_menu xml:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Item1"/>
<item android:title="Item2"/>
<item android:title="Item3"/>
<item
android:id="@+id/app_bar_search"
android:icon="@drawable/ic_search_black_24dp"
android:title="Search"
app:actionViewClass="android.widget.SearchView"/>
<item android:title="Item4">
<menu>
<item android:title="SubItem1"
android:onClick="itemClick"/>
<item android:title="SubItem2"/>
</menu>
</item>
我在logcat中以红色显示此消息:
2019-07-14 02:49:18.148 6160-6160 / com.example.excercise E / AndroidRuntime:致命异常:main 流程:com.example.execercise,PID:6160 android.view.InflateException:无法解析com.example.excercise.MainActivity类中的菜单项onClick处理程序itemClick 在androidx.appcompat.view.SupportMenuInflater $ InflatedOnMenuItemClickListener。(SupportMenuInflater.java:254) 在androidx.appcompat.view.SupportMenuInflater $ MenuState.setItem(SupportMenuInflater.java:482) 在androidx.appcompat.view.SupportMenuInflater $ MenuState.addItem(SupportMenuInflater.java:530) 在androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:206) 在androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:184) 在androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:128) 在com.example.excercise.MainActivity.onCreateOptionsMenu(MainActivity.java:69) 在android.app.Activity.onCreatePanelMenu(Activity.java:4055) 在androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378) 在androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) 在androidx.appcompat.app.AppCompatDelegateImpl $ AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549) 在androidx.appcompat.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589) 在androidx.appcompat.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869) 在androidx.appcompat.app.AppCompatDelegateImpl $ 2.run(AppCompatDelegateImpl.java:230) 在android.os.Handler.handleCallback(Handler.java:883) 在android.os.Handler.dispatchMessage(Handler.java:100) 在android.os.Looper.loop(Looper.java:214) 在android.app.ActivityThread.main(ActivityThread.java:7319) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:492) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934) 引起原因:java.lang.NoSuchMethodException:com.example.excercise.MainActivity.itemClick [接口 android.view.MenuItem] 在java.lang.Class.getMethod(Class.java:2072) 在java.lang.Class.getMethod(Class.java:1693) 在androidx.appcompat.view.SupportMenuInflater $ InflatedOnMenuItemClickListener。(SupportMenuInflater.java:250) 在androidx.appcompat.view.SupportMenuInflater $ MenuState.setItem(SupportMenuInflater.java:482)
at androidx.appcompat.view.SupportMenuInflater$MenuState.addItem(SupportMenuInflater.java:530) at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:206) at androidx.appcompat.view.SupportMenuInflater.parseMenu(SupportMenuInflater.java:184) at androidx.appcompat.view.SupportMenuInflater.inflate(SupportMenuInflater.java:128) at com.example.excercise.MainActivity.onCreateOptionsMenu(MainActivity.java:69) at android.app.Activity.onCreatePanelMenu(Activity.java:4055) at androidx.fragment.app.FragmentActivity.onCreatePanelMenu(FragmentActivity.java:378) at androidx.appcompat.view.WindowCallbackWrapper.onCreatePanelMenu(WindowCallbackWrapper.java:94) at androidx.appcompat.app.AppCompatDelegateImpl$AppCompatWindowCallback.onCreatePanelMenu(AppCompatDelegateImpl.java:2549) at androidx.appcompat.app.AppCompatDelegateImpl.preparePanel(AppCompatDelegateImpl.java:1589) at androidx.appcompat.app.AppCompatDelegateImpl.doInvalidatePanelMenu(AppCompatDelegateImpl.java:1869) at androidx.appcompat.app.AppCompatDelegateImpl$2.run(AppCompatDelegateImpl.java:230) at android.os.Handler.handleCallback(Handler.java:883) at android.os.Handler.dispatchMessage(Handler.java:100) at android.os.Looper.loop(Looper.java:214) at android.app.ActivityThread.main(ActivityThread.java:7319) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:934)
问题出在哪里,我该如何解决?我应该在红色消息引用的所有Java类中添加我,出现“无法解析R符号”错误。
答案 0 :(得分:0)
在您的main_menu.xml中向 item 元素
添加一个ID<?xml version="1.0" encoding="utf-8"?>
<item android id="@+id/item1"
android:title="Item1"/>
<item android id="@+id/item2"
android:title="Item2"/>
<item android id="@+id/item3"
android:title="Item3"/>
在您的MainActivity.java中,添加 onOptionsItemSelected 方法
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main_menu,menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
//respond to menu item selection
switch (item.getItemId()) {
case R.id.item1:
Toast.makeText(this, "You clicked on item 1", Toast.LENGTH_SHORT).show();
return true;
case R.id.item2:
Toast.makeText(this, "You clicked on item 1", Toast.LENGTH_SHORT).show();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
最后解决无法解决R符号错误 ...您尚未将R类导入到Java文件中。执行此操作有两个步骤
第一步
单击R符号,您应该看到一个红色灯泡...单击那个红色灯泡...您将看到一个显示导入类的弹出窗口...单击导入
或第二步
单击R符号...然后按 Alt + Enter
我希望这对您有帮助...