为什么 CardView 的 findViewById returns 为空?
Why does findViewById returns null for CardView?
有人能解释一下为什么我的 cardView 会收到 NPE 吗?对我来说完全是无稽之谈。以下是所有相关代码和堆栈跟踪:
ACTIVITY 托管 CardView
public class MainScreen extends AppCompatActivity implements TipView.OnValueChangedListener
{
private TipView tipView;
private CardView cardView;
private TextView tipTextView;
private TextView taxTextView;
private TextView splitTextView;
private TextView productTextView;
private TextView totalTextView;
private TextView youPayEachPay;
private ImageButton productEditButton;
private ImageButton dataToggle;
private ProductTotalWindow productTotalWindow;
private double productTotal = 0.0d;
private double currentTip = 0.0d;
private double currentTax = 0.0d;
private int currentSplit = 1;
private NumberFormat currencyFormat;
private String pricesDefaults = "[=11=].00";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
cardView = (CardView) findViewById(R.id.tipViewCardLayoutContainer).findViewById(R.id.cardViewId);
//NPE is thrown since, obviously the reference points to a null object
cardView.setCardBackgroundColor(Color.RED);
this.tipView = (TipView) findViewById(R.id.tipView);
tipView.setOnValueChangedListener(this);
dataToggle = (ImageButton) findViewById(R.id.dataToggle);
dataToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandLayout();
}
});
initDataPricesViews();
initProductEditView();
currencyFormat = NumberFormat.getCurrencyInstance();
}
}
XML 主布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bryan.tipease.MainScreen">
<!--
tipview_card_container is the layout hosting my cardview
-->
<include
layout="@layout/tipview_card_container"
android:id="@id/tipViewCardLayoutContainer" />
<FrameLayout
android:layout_alignParentBottom="true"
android:layout_below="@id/tipViewCardLayoutContainer"
android:id="@id/pricesTotalContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/prices_layout" />
</FrameLayout>
</RelativeLayout>
//The cards xml file itself
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/cardViewId"
app:contentPadding="16dp"
app:cardElevation="16dp"
app:cardBackgroundColor="@color/tipViewCardBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.bryan.tipease.TipView
style="@style/WidgetTipViewStyle"
android:id="@id/tipView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
style="@style/BreakerBarContent"
android:src="@drawable/forkknifefab"
android:id="@id/productDialogLauncher"
android:contentDescription="@string/contentDescForkKnifeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
style="@style/BreakerBarContent"
android:src="@drawable/toggle_circle"
android:id="@id/dataToggle"
android:contentDescription="@string/contentDescDataToggleImage"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
堆栈跟踪
02-23 21:06:55.548 26169-26169/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bryan.tipease, PID: 26169
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bryan.tipease/com.example.bryan.tipease.MainScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setCardBackgroundColor(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setCardBackgroundColor(int)' on a null object reference
at com.example.bryan.tipease.MainScreen.onCreate(MainScreen.java:62)
at android.app.Activity.performCreate(Activity.java:6056)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
这是一个奇怪的问题,我只能重现一次。我认为问题出在我如何引用 cardview,关于 include 标签的使用,但是创建一个测试应用程序以查看我是否可以重现这个问题,我的引用没有问题。
P.S。为了简洁起见,我从 activity 中省略了一些方法,但相信我,它们与我这里的问题无关。
P.S.S.我可以很好地引用该层次结构中的任何其他视图。
正如上面评论中提到的@Mike M.。
第一
您不需要像在此处那样链接您的 findViewById()
:
cardView = (CardView) findViewById(R.id.tipViewCardLayoutContainer).findViewById(R.id.cardViewId);
。
将从上到下搜索 View
层次结构。这意味着您应该搜索所需的 android:id="@+id
,而不是链接它们。
第二
你的 <include>
中的 id
android:id="@id/tipViewCardLayoutContainer"
覆盖了你的根目录 View
的 id
,这意味着你的 CardView
现在有tipViewCardLayoutContainer
的 id
而不是 android:id="@id/cardViewId"
。
您需要做的就是从 <include>
中删除 id
并调用
CardView cardview = (CardView) findViewById(R.id.cardViewId);
我认为 id 应该像这样使用 +id this.hope 这可以帮助你。
<include
layout="@layout/tipview_card_container"
android:id="@+id/tipViewCardLayoutContainer" />
有人能解释一下为什么我的 cardView 会收到 NPE 吗?对我来说完全是无稽之谈。以下是所有相关代码和堆栈跟踪:
ACTIVITY 托管 CardView
public class MainScreen extends AppCompatActivity implements TipView.OnValueChangedListener
{
private TipView tipView;
private CardView cardView;
private TextView tipTextView;
private TextView taxTextView;
private TextView splitTextView;
private TextView productTextView;
private TextView totalTextView;
private TextView youPayEachPay;
private ImageButton productEditButton;
private ImageButton dataToggle;
private ProductTotalWindow productTotalWindow;
private double productTotal = 0.0d;
private double currentTip = 0.0d;
private double currentTax = 0.0d;
private int currentSplit = 1;
private NumberFormat currencyFormat;
private String pricesDefaults = "[=11=].00";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
cardView = (CardView) findViewById(R.id.tipViewCardLayoutContainer).findViewById(R.id.cardViewId);
//NPE is thrown since, obviously the reference points to a null object
cardView.setCardBackgroundColor(Color.RED);
this.tipView = (TipView) findViewById(R.id.tipView);
tipView.setOnValueChangedListener(this);
dataToggle = (ImageButton) findViewById(R.id.dataToggle);
dataToggle.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
expandLayout();
}
});
initDataPricesViews();
initProductEditView();
currencyFormat = NumberFormat.getCurrencyInstance();
}
}
XML 主布局代码
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main_screen"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.bryan.tipease.MainScreen">
<!--
tipview_card_container is the layout hosting my cardview
-->
<include
layout="@layout/tipview_card_container"
android:id="@id/tipViewCardLayoutContainer" />
<FrameLayout
android:layout_alignParentBottom="true"
android:layout_below="@id/tipViewCardLayoutContainer"
android:id="@id/pricesTotalContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="@layout/prices_layout" />
</FrameLayout>
</RelativeLayout>
//The cards xml file itself
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@id/cardViewId"
app:contentPadding="16dp"
app:cardElevation="16dp"
app:cardBackgroundColor="@color/tipViewCardBackground">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.example.bryan.tipease.TipView
style="@style/WidgetTipViewStyle"
android:id="@id/tipView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
style="@style/BreakerBarContent"
android:src="@drawable/forkknifefab"
android:id="@id/productDialogLauncher"
android:contentDescription="@string/contentDescForkKnifeImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
style="@style/BreakerBarContent"
android:src="@drawable/toggle_circle"
android:id="@id/dataToggle"
android:contentDescription="@string/contentDescDataToggleImage"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</android.support.v7.widget.CardView>
堆栈跟踪
02-23 21:06:55.548 26169-26169/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.bryan.tipease, PID: 26169
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bryan.tipease/com.example.bryan.tipease.MainScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setCardBackgroundColor(int)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2378)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.CardView.setCardBackgroundColor(int)' on a null object reference
at com.example.bryan.tipease.MainScreen.onCreate(MainScreen.java:62)
at android.app.Activity.performCreate(Activity.java:6056)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2331)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2440)
at android.app.ActivityThread.access0(ActivityThread.java:162)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1348)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5422)
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:914)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:707)
这是一个奇怪的问题,我只能重现一次。我认为问题出在我如何引用 cardview,关于 include 标签的使用,但是创建一个测试应用程序以查看我是否可以重现这个问题,我的引用没有问题。
P.S。为了简洁起见,我从 activity 中省略了一些方法,但相信我,它们与我这里的问题无关。 P.S.S.我可以很好地引用该层次结构中的任何其他视图。
正如上面评论中提到的@Mike M.。
第一
您不需要像在此处那样链接您的 findViewById()
:
cardView = (CardView) findViewById(R.id.tipViewCardLayoutContainer).findViewById(R.id.cardViewId);
。
将从上到下搜索 View
层次结构。这意味着您应该搜索所需的 android:id="@+id
,而不是链接它们。
第二
你的 <include>
中的 id
android:id="@id/tipViewCardLayoutContainer"
覆盖了你的根目录 View
的 id
,这意味着你的 CardView
现在有tipViewCardLayoutContainer
的 id
而不是 android:id="@id/cardViewId"
。
您需要做的就是从 <include>
中删除 id
并调用
CardView cardview = (CardView) findViewById(R.id.cardViewId);
我认为 id 应该像这样使用 +id this.hope 这可以帮助你。
<include
layout="@layout/tipview_card_container"
android:id="@+id/tipViewCardLayoutContainer" />