我不明白为什么在这种情况下无法确认我的菜单项。
我还有另一项具有类似代码的活动,并且工作正常,但在这种情况下,我正在使用NPE。
activity_completas
)<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 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=".activities.CompletasActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_breviario_general" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_breviario_general
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<include
android:id="@+id/include"
layout="@layout/tv_zoomable" />
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="true"
app:layout_constraintBottom_toTopOf="@+id/include"
app:layout_constraintEnd_toStartOf="@+id/include"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
</androidx.constraintlayout.widget.ConstraintLayout>
<?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"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".activities.BreviarioActivity">
<item
android:id="@+id/item_voz"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="@drawable/ic_play"
android:visible="false"
android:title="@string/leer"
app:showAsAction="ifRoom" />
<item
android:id="@+id/item_calendario"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:icon="@drawable/ic_calendar_toolbar"
android:title="@string/all_calendar"
app:showAsAction="ifRoom" />
</menu>
仅在这种情况下相关的部分:
public class CompletasActivity extends AppCompatActivity {
//...
private Menu menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_completas);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
showData();
}
private void showData() {
sbReader = new StringBuilder();
//I fill sbReader correctly
if (sbReader.length()>0) {
menu.findItem(R.id.item_voz).setVisible(true); //******NPE here*******
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
this.menu = menu;
getMenuInflater().inflate(R.menu.toolbar_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
if (tts != null) {
tts.cerrar();
}
NavUtils.navigateUpFromSameTask(this);
return true;
case R.id.item_voz:
String html = String.valueOf(Utils.fromHtml(sbReader.toString()));
String[] textParts = html.split(SEPARADOR);
tts = new TTS(getApplicationContext(), textParts);
return true;
case R.id.item_calendario:
Intent i = new Intent(this, CalendarioActivity.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onBackPressed() {
super.onBackPressed();
if (tts != null) {
tts.cerrar();
}
}
}
java.lang.NullPointerException:尝试调用接口方法 'android.view.MenuItem android.view.Menu.findItem(int)'在null上 对象引用
我在其他活动中也做同样的事情,但是我没有NEP。这里发生了什么事?
答案 0 :(得分:1)
由于没有实例化菜单,因此出现空点异常。如我所见,您在创建菜单之前正在调用showData。在onCreateMenu中调用showData。这应该可以解决您的问题。