I`m getting error when run leanback activity

625 views Asked by At

So, I`m trying to implement Leanback activity, and i have activity:

public class TvNavigationActivity extends LeanbackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_navigation);
}

My layout resource file looks like this:

<?xml version="1.0" encoding="utf-8"?>
<fragment
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_frame"
    android:name="com.app.tv.firetv.ui.fragment.HomeFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

homefragment and its layout looks like this

public class HomeFragment extends BrowseSupportFragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_home, container, false);       

        return view;
    }    
}

HomeFragment.xml

 <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@color/backgroundColor">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="48sp"
            android:text="HOME"
            android:layout_centerInParent="true"/>
    
    </RelativeLayout>

When i run this the app crashes, here is log:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.app.tv.firetv, PID: 32640
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.app.tv.firetv/com.app.tv.firetv.ui.activity.TvNavigationActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v17.leanback.app.HeadersSupportFragment.setAlignment(int)' on a null object reference
        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.NullPointerException: Attempt to invoke virtual method 'void android.support.v17.leanback.app.HeadersSupportFragment.setAlignment(int)' on a null object reference
        at android.support.v17.leanback.app.BrowseSupportFragment.onStart(BrowseSupportFragment.java:1662)
        at android.support.v4.app.Fragment.performStart(Fragment.java:2477)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1494)
        at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1784)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1852)
        at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3269)
        at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3235)
        at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:212)
        at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:628)
        at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
        at android.app.Activity.performStart(Activity.java:6253)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
        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) 

I cant figure out whats wrong, because I was doing it be tutorial.

1

There are 1 answers

1
sdex On BEST ANSWER

You are not supposed to override onCreateView. Remove this and it should work.

Also, check the official docs: https://developer.android.com/training/tv/playback/browse?hl=ru