运行 leanback activity 时出现错误
I`m getting error when run leanback activity
所以,我正在尝试实施 Leanback activity,并且我有 activity:
public class TvNavigationActivity extends LeanbackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
}
我的布局资源文件如下所示:
<?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 及其布局如下所示
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>
当我 运行 这个应用程序崩溃时,这是日志:
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)
我不知道哪里出了问题,因为我正在做教程。
您不应该覆盖 onCreateView
。删除它,它应该可以工作。
另外,查看官方文档:
https://developer.android.com/training/tv/playback/browse?hl=ru
所以,我正在尝试实施 Leanback activity,并且我有 activity:
public class TvNavigationActivity extends LeanbackActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation);
}
我的布局资源文件如下所示:
<?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 及其布局如下所示
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>
当我 运行 这个应用程序崩溃时,这是日志:
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)
我不知道哪里出了问题,因为我正在做教程。
您不应该覆盖 onCreateView
。删除它,它应该可以工作。
另外,查看官方文档: https://developer.android.com/training/tv/playback/browse?hl=ru