gpt4 book ai didi

java - Android:VerticalGridFragment 在使用 ErrorFragment 时导致应用程序崩溃?

转载 作者:行者123 更新时间:2023-11-30 01:13:56 24 4
gpt4 key购买 nike

我见过很多关于 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" />

主要 Activity .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);
}
}

主要 fragment .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)
{

}
});
}

...
}

我的日志:

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 一起使用。

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

谢谢!

最佳答案

如果仔细查看 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();
}

然而,根据日志,当时,onCreateViewHolderNULL 中调用引用

因此,我们可以得出结论 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);
...

这并没有完全回答你的问题......但我认为它可以帮助你找到出路......

问候

关于java - Android:VerticalGridFragment 在使用 ErrorFragment 时导致应用程序崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38152917/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com