我已经调整了默认代码来引用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)
在onReadyForContent
中做setContentView
而不是onCreate
,你应该很好。
确保一个id为"circle"的CircledImageView在你的圆形和矩形布局中,如你的WatchViewStub中指定的。在您的情况下,布局/rect_activity_my.xml和布局/round_activity_my.xml必须有一个CircledImageView与id="@+id/circle"。在运行时,矩形或圆形布局将根据屏幕的形状膨胀,所以如果布局没有CircledImageView, findViewById(R.id.circle)将返回null。