R.id.icon
已在Android API级别1中定义。但是,我在使用Android Design Support Library
访问使用R.id.icon
充气的imageview时,在targetSdk
内部代码中获得了NPE。
注意:我不定位奥利奥,因为25
目前是 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.widget.ImageView.getLayoutParams()' on a null object reference
at android.support.design.internal.BottomNavigationItemView.setChecked(BottomNavigationItemView.java:172)
at android.support.design.internal.BottomNavigationItemView.initialize(BottomNavigationItemView.java:97)
at android.support.design.internal.BottomNavigationMenuView.buildMenuView(BottomNavigationMenuView.java:276)
at android.support.design.internal.BottomNavigationPresenter.updateMenuView(BottomNavigationPresenter.java:62)
at android.support.design.widget.BottomNavigationView.inflateMenu(BottomNavigationView.java:238)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:167)
at android.support.design.widget.BottomNavigationView.<init>(BottomNavigationView.java:116)
at java.lang.reflect.Constructor.newInstance(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:288)
at android.view.LayoutInflater.createView(LayoutInflater.java:607)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:743)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.who.android.app.layout.activities.Sample_Mock.onCreate(Sample_Mock.java:40)
at android.app.Activity.performCreate(Activity.java:6033)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
。
这是stacktrace:
ImageView
经过调查,我发现BottomNavigationItemView.java中的代码正在尝试访问使用R.id.icon
资源充实的空 LayoutParams iconParams = (LayoutParams) mIcon.getLayoutParams();
。
mIcon
其中null
为 mIcon = (ImageView) findViewById(R.id.icon);
且已被夸大为
BottomNavigationActivity
编辑:我在Android的画廊中创建了一个新的package com.who.android.app.layout.activities;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
import com.who.android.app.R;
public class Sample_Mock extends AppCompatActivity {
private TextView mTextMessage;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);
return true;
case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);
return true;
case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample__mock);
mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
,并将导航重定向到此模拟类,而不是我的。我仍然面临同样的问题。以下是原始java代码和新模拟活动的布局xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com._._._._.activities.Sample_Mock">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="@+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="@dimen/activity_vertical_margin"
android:layout_marginLeft="@dimen/activity_horizontal_margin"
android:layout_marginRight="@dimen/activity_horizontal_margin"
android:layout_marginTop="@dimen/activity_vertical_margin"
android:text="@string/title_home"/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="@+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="@menu/navigation"/>
</LinearLayout>
这是原始布局文件,我使用BottomNavigationView(由Android创建,我没有触及它)
com.android.support:design:25.3.1
com.android.support:appcompat-v7:25.3.1
我正在使用的Android设计支持库和Android AppCompat支持库的版本都是25.3.1
BottomNavigationActivity
任何人都可以帮助我理解为什么我从R.id.icon中获取一个空引用?此代码无法控制,因为它是Android设计支持库的内部代码。另外,正如我在新创建的Android Design Support
中发现此问题一样,我使用的库版本是否存在问题?因为这个版本在过去的几个月里一直很好用。
编辑2:我创建了另一个项目,其中只有一个BottomNavigationActivity。你猜怎么着?同样的错误。所以在我看来,这是一个<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home"/>
<item
android:id="@+id/navigation_dashboard"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_dashboard"/>
<item
android:id="@+id/navigation_notifications"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_notifications"/>
</menu>
图书馆问题。
编辑3:menu / navigation.xml正如您所看到的,它是标准的。
"Launch 🚀"