Android Wear: error inflating android.support.wearable.view.WatchViewStub

1.7k Views Asked by At

I get following error message when starting my Wearable demo app:

"Error inflating class android.support.wearable.view.WatchViewStub"

Exception Stack Trace:

 FATAL EXCEPTION: main
 Process: com.example.demowearapp, PID: 1503
 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.demowearapp/com.example.demowearapp.MainActivity}: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.wearable.view.WatchViewStub
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2197)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2258)
    at android.app.ActivityThread.access$800(ActivityThread.java:138)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1209)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    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)
 Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class android.support.wearable.view.WatchViewStub
    at android.view.LayoutInflater.createView(LayoutInflater.java:620)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:469)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:309)
    at android.app.Activity.setContentView(Activity.java:1930)
    at com.example.demowearapp.MainActivity.onCreate(MainActivity.java:15)
    at android.app.Activity.performCreate(Activity.java:5242)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161)
    ... 11 more
 Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Constructor.constructNative(Native Method)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at android.view.LayoutInflater.createView(LayoutInflater.java:594)
    ... 21 more
 Caused by: java.lang.NoClassDefFoundError: android.support.wearable.R$styleable
    at android.support.wearable.view.WatchViewStub.<init>(WatchViewStub.java:52)
    at android.support.wearable.view.WatchViewStub.<init>(WatchViewStub.java:46)
    ... 24 more

My MainActivity:

public class MainActivity extends Activity {

    private TextView mTextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final WatchViewStub stub = (WatchViewStub) findViewById(R.id.watch_view_stub);
        stub.setOnLayoutInflatedListener(new WatchViewStub.OnLayoutInflatedListener() {
            @Override
            public void onLayoutInflated(WatchViewStub stub) {
                mTextView = (TextView) stub.findViewById(R.id.text);
            }
        });
    }
}

My activity_main.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"
    app:roundLayout="@layout/round"
    tools:context="com.example.demowearapp.MainActivity"
    tools:deviceIds="wear" >
</android.support.wearable.view.WatchViewStub>

The demo project is from: Android Wear Template

I am using Eclipse Luna for this. If I directly inflate R.layout.rect, it works.

1

There are 1 best solutions below

2
On

Seems you have not included the support-wearable lib project

Caused by: java.lang.NoClassDefFoundError: android.support.wearable.R$styleable
    at android.support.wearable.view.WatchViewStub.<init>(WatchViewStub.java:52)
    at android.support.wearable.view.WatchViewStub.<init>(WatchViewStub.java:46)
    ... 24 more

(gradle equivalent compile 'com.google.android.support:wearable:1.1.0')

  1. download library zip from here
  2. unzip and create eclipse android library project
  3. add library project to your existing project

unzip and try again