findViewById()在OnLayoutInflatedListener中返回null

时间:2014-06-27 20:08:45

标签: android wear-os

我已经调整了默认代码来引用CircledImageView,以便我可以设置它的背景颜色,但它返回null。我也尝试在视图的xml文件中设置src / background,但都没有工作。他们返回错误,试图给视图充气。

编辑:我认为问题可能出在CircledImageView - 是否有人成功使用过它?

public class MyActivity  extends Activity {

private CircledImageView mCircle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my);
    final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
    stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
        @Override
        public void onLayoutInflated(WatchViewStub stub) {
            mCircle = (CircledImageView) stub.findViewById(R.id.circle);
            mCircle.setCircleColor(Color.RED);
        }
    });
}
}

这是watch-view-stub xml代码

<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.WatchViewStub
    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/watch_view_stub"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:rectLayout="@layout/rect_activity_my"
    app:roundLayout="@layout/round_activity_my"
    tools:context="com.larvalabs.weartest.MyActivity"
    tools:deviceIds="wear">
</android.support.wearable.view.WatchViewStub>

它返回以下错误

06-27 15:49:14.262    1679-1679/com.larvalabs.weartest E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.larvalabs.weartest, PID: 1679
java.lang.NullPointerException
        at com.larvalabs.weartest.MyActivity$1.onLayoutInflated(MyActivity.java:25)
        at android.support.wearable.view.WatchViewStub.inflate(WatchViewStub.java:133)
        at android.support.wearable.view.WatchViewStub.onMeasure(WatchViewStub.java:141)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
        at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
        at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2321)
        at android.view.View.measure(View.java:16648)
        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1959)
        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1145)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1340)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1032)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5657)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5026)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
        at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

setContentView而不是onReadyForContent onCreate,你应该做得好。

答案 1 :(得分:0)

确保带有id&#34; circle&#34;的CircledImageView;在您的WatchViewStub中指定的圆形和矩形布局中。在您的情况下,layout / rect_activity_my.xml和layout / round_activity_my.xml都必须具有带id =&#34; @ + id / circle&#34;的CircledImageView。在运行时,矩形或圆形布局将根据屏幕的形状进行膨胀,因此如果该布局没有CircledImageView,则findViewById(R.id.circle)将返回null。