android java.langNullPointerException 空对象引用
android java.langNullPointerException null object reference
我正在尝试初始化所有导航栏按钮并指定黑色背景颜色,当用户单击其中一个按钮时,该按钮应更改颜色,而所有其他按钮应保持黑色。
但是我收到以下错误:
Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object
reference
public void bottomNavIcons(View view){
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.bottom_nav_bar);
ImageButton imageButton = new ImageButton(getActivity());
imageButton.setClickable(true);
imageButton.setOnClickListener(onClickListener);
imageButton.setImageResource(Constants.bottom_nav_icon[i]);
imageButton.setBackgroundColor(Color.BLACK);
imageButton.setPadding(70, 0, 70, 0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageButton, layoutParams);
}
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
ImageView imageButton = (ImageView) linearLayout.getChildAt(i);
imageButton.setBackgroundColor(Color.BLACK);
}
v.setBackgroundColor(Color.BLUE);
}
};
我认为这是问题所在:
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
您假设 LinearLayout 位于被单击的视图内 - 我最好的猜测是这不是真的(您可能想要获得被单击视图的父视图,而不是子视图)。
您有 2 个选择:
- 开始将视图层次迭代到顶部(即调用
v.getParent()
),直到到达 LinearLayout。
- 在包含 activity(或片段的根视图)而不是
v
对象上调用 findViewById
。
我正在尝试初始化所有导航栏按钮并指定黑色背景颜色,当用户单击其中一个按钮时,该按钮应更改颜色,而所有其他按钮应保持黑色。 但是我收到以下错误:
Attempt to invoke virtual method 'android.view.View android.widget.LinearLayout.getChildAt(int)' on a null object reference
public void bottomNavIcons(View view){
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
LinearLayout linearLayout = (LinearLayout) view.findViewById(R.id.bottom_nav_bar);
ImageButton imageButton = new ImageButton(getActivity());
imageButton.setClickable(true);
imageButton.setOnClickListener(onClickListener);
imageButton.setImageResource(Constants.bottom_nav_icon[i]);
imageButton.setBackgroundColor(Color.BLACK);
imageButton.setPadding(70, 0, 70, 0);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
linearLayout.addView(imageButton, layoutParams);
}
}
View.OnClickListener onClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
for(int i=0;i<Constants.bottom_nav_icon.length;i++) {
ImageView imageButton = (ImageView) linearLayout.getChildAt(i);
imageButton.setBackgroundColor(Color.BLACK);
}
v.setBackgroundColor(Color.BLUE);
}
};
我认为这是问题所在:
LinearLayout linearLayout = (LinearLayout) v.findViewById(R.id.bottom_nav_bar);
您假设 LinearLayout 位于被单击的视图内 - 我最好的猜测是这不是真的(您可能想要获得被单击视图的父视图,而不是子视图)。 您有 2 个选择:
- 开始将视图层次迭代到顶部(即调用
v.getParent()
),直到到达 LinearLayout。 - 在包含 activity(或片段的根视图)而不是
v
对象上调用findViewById
。