gpt4 book ai didi

android - 抽屉导航 + Viewpager + setVisibility = InflateException/NullPointer

转载 作者:太空宇宙 更新时间:2023-11-03 12:55:17 25 4
gpt4 key购买 nike

我开发了一个 Android 应用程序,但遇到了一些问题。

在我的 MainActivity 中,我有以下组件:

  • Navigation Drawer(实例化并加载许多 fragment )
  • ViewPager(仅用于显示文章详细 View 的ArticleFragment)

通常情况下,ViewPager 与特定的 Activity 相关,但我真的想同时使用 Navigation Drawer 和 ViewPager。唯一的方法是在同一个 Activity 中实现这两个元素。

DrawerLayout 由抽屉导航本身和内容区域组成。在我的内容区域中,我有一个 FrameLayout 和一个 ViewPager。问题是 ViewPager 始终显示。

所以我的想法是使用 android:visibility 属性来交替显示/隐藏 FrameLayout 和 ViewPager。如果您查看我的 activity_main.xml,您会注意到 ViewPager 上的 android:visibility 设置为 invisible

/layout/activity_main.xml

    <!-- The main content view -->
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">

<FrameLayout
android:id="@+id/container"
android:paddingTop="?android:attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="match_parent" />

<!-- ViewPager -->
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />

</RelativeLayout>

<!-- The navigation drawer -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:name="NavigationDrawerFragment" />

</android.support.v4.widget.DrawerLayout>

然后:

MainActivity.java

public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks {

private NavigationDrawerFragment mNavigationDrawerFragment;
private FrameLayout mContainer;

private ViewPager mPager;
private PagerAdapter mPagerAdapter;

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

// Set up the navigation drawer
mContainer = (FrameLayout) findViewById(R.id.container);
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));

// Set up the viewpager
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
}

//-------------------------------//
// NAVIGATION DRAWER //
//-------------------------------//

/**
* Update the main content by replacing fragments
*/
@Override
public void onNavigationDrawerItemSelected(int position) {
Fragment fragment = null;
Boolean viewPagerArticles = false;

switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FooFragment();
break;
case 2:
viewPagerArticles = true;

// Hide navigation drawer container and show the viewpager
mContainer.setVisibility(FrameLayout.GONE);
mPager.setVisibility(ViewPager.VISIBLE);

// NullPointerException with new this line. Why?
// Fragment already instanciate in ScreenSlidePagerAdapter -> getItem() -> ArticleFragment.create()?
//fragment = new ArticleFragment();
break;
default:
break;
}

// Show the view pager and hide navigation drawer container
// =================
// THE BUG IS HERE
// =================
if (!viewPagerArticles) {
mContainer.setVisibility(FrameLayout.VISIBLE);
mPager.setVisibility(ViewPager.GONE);
}

if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
}

...

//-------------------------------//
// VIEWPAGER //
//-------------------------------//

private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
return DayFragment.create(position);
}

@Override
public int getCount() { ... }
}
...
}

NavigationDrawerFragment.java

http://pastebin.com/LwKZnNiB

显示ViewPager内容

在我的 HomeFragment 上,我有一个带有 onClickListener 的按钮:

mButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((MainActivity)getActivity()).onNavigationDrawerItemSelected(2);
}
});

当我加载 MainActivity 时出现这些错误:InflateException 和 NullPointerException

AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android/com.example.android.ui.MainActivity}: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #34: Error inflating class fragment
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
at android.app.Activity.setContentView(Activity.java:1895)
at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
at com.example.android.ui.MainActivity.onCreate(MainActivity.java:42)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.android.ui.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:117)
at com.example.android.ui.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:202)
at com.example.android.ui.NavigationDrawerFragment.onCreate(NavigationDrawerFragment.java:80)
at android.support.v4.app.Fragment.performCreate(Fragment.java:1477)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:893)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1082)
at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1184)
at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:291)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
            at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:267)
            at android.app.Activity.setContentView(Activity.java:1895)
            at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
            at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
            at com.example.android.ui.MainActivity.onCreate(MainActivity.java:42)
            at android.app.Activity.performCreate(Activity.java:5133)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
            at android.app.ActivityThread.access$600(ActivityThread.java:141)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:5103)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:525)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
            at dalvik.system.NativeStart.main(Native Method)

一切都很好(没有错误, Activity 已加载)没有:

    // =================
// THE BUG IS HERE
// =================
if (!viewPagerArticles) {
mContainer.setVisibility(FrameLayout.VISIBLE);
mPager.setVisibility(ViewPager.GONE);
}

...但是它不符合我的预期,因为抽屉导航 fragment 总是隐藏(这是正常的,因为 mContainer.setVisibility(FrameLayout.GONE);。我只是想反转 mContainer/mPager 可见性。

我希望我说得足够清楚...!

有什么想法吗?

最佳答案

你明白了 NullPointerException因为你构建 NavigationDrawerFragment 的方式分段。更准确地说,在其 onCreate()你调用的方法selectItem()通过 Activity 传播(注册为监听器)到 onNavigationDrawerItemSelected()打回来。问题是 fragment 将在 setContentView() 时膨胀。被称为(在 onCreate() 中),这意味着您将输入 onNavigationDrawerItemSelected()行前回调:

mContainer = (FrameLayout) findViewById(R.id.container);
mPager = (ViewPager) findViewById(R.id.pager);

运行,渲染那些引用 nullonNavigationDrawerItemSelected() .

要解决这个问题,您可以简单地替换当前的 <fragment />来自 Activity 的标签使用基本容器布局(例如占位符为空 FrameLayout)并添加 NavigationDrawerFragment手动输入 onCreate()在上面的行之后(绑定(bind) View 的地方)。可能有另一个更好的解决方案(或者我可以提出其他建议),但你不太清楚你想如何处理可见性。也许您可以对此进行扩展。

编辑 2:

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

// Set up the navigation drawer
mContainer = (FrameLayout) findViewById(R.id.container);


// Set up the viewpager
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
if (savedInstanceState == null) {
NavigationDrawerFragment ndf = new NavigationDrawerFragment();
ndf.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
getSupportFragmentManager().beginTransaction().add(R.id.nav_drawer, ndf).commit();
}
}

编辑 1:

//...
<!-- ViewPager -->
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />

</RelativeLayout>
<!-- The navigation drawer -->
<FrameLayout android:id="@+id/nav_drawer"
android:layout_width="@dimen/navigation_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
/>

</android.support.v4.widget.DrawerLayout>

并且在 onCreate()最后:

//...
mPager.setAdapter(mPagerAdapter);
mPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
invalidateOptionsMenu();
}
});
getSupportFragmentManager().beginTransaction().add(R.id.nav_drawer, new NavigationDrawerFragment()).commit();

如果上面的代码出现异常,它在同一个 onNavigationDrawerItemSelected() 中方法?

关于android - 抽屉导航 + Viewpager + setVisibility = InflateException/NullPointer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21309508/

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