我在运行Leanback活动时遇到错误。

huangapple go评论81阅读模式
英文:

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:

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

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

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

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 &#39;void android.support.v17.leanback.app.HeadersSupportFragment.setAlignment(int)&#39; 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 &#39;void android.support.v17.leanback.app.HeadersSupportFragment.setAlignment(int)&#39; 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)&#160;
        at android.app.ActivityThread.-wrap11(ActivityThread.java)&#160;
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)&#160;
        at android.os.Handler.dispatchMessage(Handler.java:102)&#160;
        at android.os.Looper.loop(Looper.java:148)&#160;
        at android.app.ActivityThread.main(ActivityThread.java:5417)&#160;
        at java.lang.reflect.Method.invoke(Native Method)&#160;
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)&#160;
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)&#160;

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

huangapple
  • 本文由 发表于 2020年10月1日 22:37:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64157641.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定