Android:VerticalGridFragment导致应用在使用ErrorFragment时崩溃?

时间:2016-07-01 19:41:44

标签: java android android-tv

我见过很多关于Binary XML file line #2: Binary XML file line #2: Error inflating class fragment的问题但是没有一个与我的有关,甚至尝试了一些解决方案也没有用。

我的问题是,当我使用VerticalGridFragment并且我提​​出ErrorFragment时,它会崩溃。但是,如果我将MainFragment扩展为使用DetailsFragment,则该应用不会崩溃,我可以使用ErrorFragment

这是我的示例代码:

activity_main.xml中:

<?xml version="1.0" encoding="utf-8"?>
<fragment 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_browse_fragment"
    android:name="com.ui.MainFragment"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.ui.MainActivity"
    tools:deviceIds="tv"
    tools:ignore="MergeRootFrame" />

MainActivity.java:

public class MainActivity extends Activity
{
    private static final String TAG = "MyActivity";

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

MainFragment.java:

public class MainFragment extends VerticalGridFragment
{
    public final ErrorFragment error = new ErrorFragment();

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);

        displayInternetError();
    }

    public void displayInternetError()
    {
        getFragmentManager().beginTransaction().replace(R.id.main_browse_fragment, error)
                .addToBackStack(null).commit();

        error.setImageDrawable(getResources().getDrawable(R.drawable.lb_ic_sad_cloud, null));
        error.setMessage(getResources().getString(R.string.no_internet_message));
        error.setDefaultBackground(true);

        error.setButtonText(getResources().getString(R.string.retry_connection));

        error.setButtonClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View arg0)
            {

            }
        });
    }

    ...
}

我的Logcat:

FATAL EXCEPTION: main
Process: com.example.vietmytv_androidtv, PID: 17619
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.vietmytv_androidtv/com.example.vietmytv_androidtv.ui.MainActivity}: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment 
    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: android.view.InflateException: Binary XML file line #2: Binary XML file line #2: Error inflating class fragment
    at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)        
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
    at android.app.Activity.setContentView(Activity.java:2166)
    at com.ui.MainActivity.onCreate(MainActivity.java:42)
    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)
Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class fragment
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:782)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393) 
    at android.app.Activity.setContentView(Activity.java:2166) 
    at com.ui.MainActivity.onCreate(MainActivity.java:42) 
    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)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v17.leanback.widget.VerticalGridPresenter$ViewHolder android.support.v17.leanback.widget.VerticalGridPresenter.onCreateViewHolder(android.view.ViewGroup)' on a null object reference
    at android.support.v17.leanback.app.VerticalGridFragment.onViewCreated(VerticalGridFragment.java:170)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 
    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1228)
    at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2201)
    at android.app.FragmentController.onCreateView(FragmentController.java:98)
    at android.app.Activity.onCreateView(Activity.java:5546)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:754)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
    at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:393)
    at android.app.Activity.setContentView(Activity.java:2166)
    at com.ui.MainActivity.onCreate(MainActivity.java:42)
    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)

根据我显示布局的方式,我需要使用VerticalGridLayout,但我不确定如何将其与ErrorFragment一起使用。

有人能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:1)

如果你谨慎地看一下logcat,你会发现问题与XML无关,而是与XML通胀期间调用的某些方法有关(可能是一些callBack方法)。

错误

所以,错误是可能的:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.support.v17.leanback.widget.VerticalGridPresenter$ViewHolder android.support.v17.leanback.widget.VerticalGridPresenter.onCreateViewHolder(android.view.ViewGroup)' on a null object reference
    at android.support.v17.leanback.app.VerticalGridFragment.onViewCreated(VerticalGridFragment.java:170)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:954)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126) 
    at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1228)        

mGridPresenter为NULL

我查看了android.support.v17.leanback.app.VerticalGridFragment HERE的源代码:

您可以确认方法VerticalGridFragment.onViewCreated()调用VerticalGridPresenter.onCreateViewHolder()

@Override
public void More ...onViewCreated(View view, Bundle savedInstanceState) {
    ViewGroup gridDock = (ViewGroup) view.findViewById(R.id.browse_grid_dock);
    mGridViewHolder = mGridPresenter.onCreateViewHolder(gridDock);
    gridDock.addView(mGridViewHolder.view);

    updateAdapter();
}

但是,根据日志,当时onCreateViewHolder引用中会调用NULL

因此,我们可以得出结论mGridPresenterNULL

如何修复

检查android.support.v17.leanback.app.VerticalGridFragment源代码,您可以看到mGridPresenter仅在方法setGridPresenter()中设置。

public void setGridPresenter(VerticalGridPresenter gridPresenter) {
    if (gridPresenter == null) {
        throw new IllegalArgumentException("Grid presenter may not be null");
    }
    mGridPresenter = gridPresenter;
    ...
}

因此,您可能需要在使用VerticalGridFragmet

之前调用该方法

从Google(HERE)查看此示例,您可以看到他们致电setGridPresenter()。所以,也许,你错过了这一步。

private void setupFragment() {
    VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
    gridPresenter.setNumberOfColumns(NUM_COLUMNS);
    setGridPresenter(gridPresenter);
...

这不能完整回答你的问题...但我认为这有助于你找到方法......

问候