0
votes

As a quick visualization, this is what my layout looks like:

<android.support.design.widget.CoordinatorLayout 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/main_root_view"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context="me.smac89.sample.MainActivity">

    <android.support.v4.view.ViewPager
        android:id="@+id/main_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="@dimen/cards_margin"
        app:layout_behavior="@string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>

When I set an ID (android:id="") for the Coordinator layout, the app crashes upon start up, however removing the id allows it to run normally. What is going on? Why does setting an ID for the root view cause the app the crash?

I've seen people using this findViewById(android.R.id.content) to get the root view; is this the only way to obtain the rootview?


Crash log

07-06 18:30:42.253 9385-9385/me.smac89.deloittepixel E/AndroidRuntime: FATAL EXCEPTION: main Process: me.smac89.deloittepixel, PID: 9385 java.lang.RuntimeException: Unable to start activity ComponentInfo{me.smac89.sample/me.smac89.sample.MainActivity}: java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.app.Activity at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) at android.app.ActivityThread.-wrap11(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) Caused by: java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.app.Activity at butterknife.internal.Finder$2.getContext(Finder.java:34) at butterknife.internal.Finder.getResourceEntryName(Finder.java:131) at butterknife.internal.Finder.findRequiredViewAsType(Finder.java:86) at me.smac89.sample.MainActivity$$ViewBinder$InnerUnbinder.(MainActivity$$ViewBinder.java:32) at me.smac89.sample.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:20) at me.smac89.sample.MainActivity$$ViewBinder.bind(MainActivity$$ViewBinder.java:17) at butterknife.ButterKnife.bind(ButterKnife.java:122) at me.smac89.sample.MainActivity.onCreate(MainActivity.java:50) at android.app.Activity.performCreate(Activity.java:6237) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)  at android.app.ActivityThread.-wrap11(ActivityThread.java)  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)  at android.os.Handler.dispatchMessage(Handler.java:102)  at android.os.Looper.loop(Looper.java:148)  at android.app.ActivityThread.main(ActivityThread.java:5417)  at java.lang.reflect.Method.invoke(Native Method)  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

MainActivity.java

public class MainActivity extends AppCompatActivity implements ViewPager.OnPageChangeListener {

    public static final String TAG = MainActivity.class.getName();

    @BindView(R.id.main_content)
    ViewPager viewPager;

    @BindView(R.id.main_tab_spinner)
    Spinner spinner;

    @BindView(R.id.main_tab)
    TabLayout tabLayout;

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

        setKeyAndSecret();
        ButterKnife.bind(this);
        spinner.setAdapter(new SpinnerDummyAdapter(this));

        initTabs();
        viewPager.addOnPageChangeListener(this);
    }
    ...
}
1
What id do you set?jbarat
@jbarat Initially this android:id="@+id/main_root_view", but after the first crash, I tried android:id="@+id/blahblah" and still got the same errorsmac89
Where do you set that layout? How do you find view Id for root? Can you show the crash log?Liem Vo
@LiemVo See my edits above. I have a feeling this is not related to android but rather to Butterknifesmac89
According the log, the error is relative to casting java.lang.ClassCastException: android.support.design.widget.CoordinatorLayout cannot be cast to android.app.Activity att. If you don't mind, please show me the code that you find id for CoordinatorLayoutLiem Vo

1 Answers

0
votes

I had a similar issue and solved it with: Build > Clean Project