英文:
I`m getting error when run leanback activity
问题
以下是翻译的内容:
所以,我正在尝试实现Leanback活动,我有一个活动:
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 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)
...
我无法弄清楚问题在哪里,因为我按照教程进行操作。
英文:
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
得分: 1
你不应该覆盖 onCreateView
。删除它,应该就能正常工作。
另外,请查阅官方文档:
https://developer.android.com/training/tv/playback/browse?hl=ru
英文:
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论