2015-03-03 51 views
1

我必須爲我的Android Wear使用Watchviewstub,這會導致我產生NullpointerException。當我不使用WatchviewStub時,AnimationDrawable工作正常。嘗試使用WatchViewStub啓動AnimationDrawable時產生NullPointerException

基本上我試圖解決它,就像它被描述爲here。在閱讀post時,我也無法得到答案。

main_layout.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/too/white" 
    android:padding="1dp" 

    android:id="@+id/watch_view_stub" 

    android:layout_width="match_parent" 
    android:layout_height="match_parent" 

    app:rectLayout="@layout/rect_activity_main" 
    app:roundLayout="@layout/round_activity_main" 

    tools:context=".MainActivity" 
    tools:deviceIds="wear"> 

    </android.support.wearable.view.WatchViewStub> 

rect_activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:keepScreenOn="true" 
    android:background="@color/white" 
    app:layout_box="all" 
    xmlns:app="http://schemas.android.com/tools" 
    xmlns:android="http://schemas.android.com/apk/res/android"> 

    <ImageView 
     android:layout_width="40dp" 
     android:layout_height="40dp" 
     android:layout_marginTop="6dp" 
     android:layout_marginRight="2dp" 
     android:id="@+id/heart_animation" 
     android:layout_gravity="right|top" 
     android:src="@drawable/heart_animation"/> 

    <TextView 
     android:id="@+id/rate" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textSize="15sp" 
     android:text="89" 
     android:textColor="@color/white" 
     android:layout_marginTop="10dp" 
     android:layout_marginRight="13dp" 
     android:layout_gravity="right|top"/> 

</FrameLayout> 

抽拉/ heart_animation.xml

<?xml version="1.0" encoding="utf-8"?> 
<animation-list 
    android:id="@+id/heart_animation_drawable" 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:oneshot="false"> 
    <item android:drawable="@drawable/heart_grey" android:duration="500" /> 
    <item android:drawable="@drawable/heart_red" android:duration="900" /> 
    <item android:drawable="@drawable/heart_grey" android:duration="200" /> 
    <item android:drawable="@drawable/heart_red" android:duration="350" /> 
    <item android:drawable="@drawable/heart_grey" android:duration="200" /> 
    <item android:drawable="@drawable/heart_red" android:duration="350" /> 
</animation-list> 

MainActivity.java

ImageView view; 
    AnimationDrawable frameAnimation; 

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     view = (ImageView) findViewById(R.id.heart_animation); 
     view.setBackgroundResource(R.drawable.heart_animation); 
     frameAnimation = (AnimationDrawable) view.getBackground(); 
} 

    // Called when Activity becomes visible or invisible to the user 
    @Override 
    public void onWindowFocusChanged(boolean hasFocus) { 
     super.onWindowFocusChanged(hasFocus); 
     if (hasFocus) { 
      // Starting the animation when in Focus 
      frameAnimation.start(); 
     } else { 
      // Stoping the animation when not in Focus 
      frameAnimation.stop(); 
     } 
    } 

錯誤:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setBackgroundResource(int)' on a null object reference 
     at de.denjo.test.MainActivity.onCreate(MainActivity.java:72) 

回答

1

如果使用WatchViewStub,你不能直接得到你需要的佈局的內容,但你必須獲取WatchViewStub對象,並在其上調用setOnLayoutInflatedListener。當調用回調函數時,可以使用WatchViewStub對象查找所需的對象。例如

final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub); 
stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() { 
    @Override 
    public void onLayoutInflated(WatchViewStub watchViewStub) { 
      view = (ImageView) watchViewStub.findViewById(R.id.heart_animation); 
      view.setBackgroundResource(R.drawable.heart_animation); 
      frameAnimation = (AnimationDrawable) view.getBackground(); 
    } 
}); 
+0

是的,我沒有這種方式得到NullPointerException。但是,動畫還沒有開始。 :( – 2015-03-03 14:41:55

+0

嘗試調用'frameAnimation.start();''剛剛在'frameAnimation =(AnimationDrawable)view.getBackground();' – Blackbelt 2015-03-03 14:44:52

+0

不會這樣工作...... – 2015-03-03 16:03:38

相關問題