Android Alert对话框未从片段内的片段显示

时间:2019-08-23 12:33:17

标签: android android-fragments android-alertdialog navigation-drawer

我的Android应用程序中具有以下scenaron:

启动屏幕后,我有一个带有一些可单击项的活动(MainActivity),每个项目单击都会启动一个DrawerNavigationActivity,并打开一个像这样的片段

    CardView mediaTxt = findViewById(R.id.mediacardId);
    mediaTxt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(MainActivity.this, DrawerNavigationActivity.class);
            Bundle b = new Bundle();
            b.putInt("link", 2);
            intent.putExtras(b);
            startActivity(intent);
        }
    });

在DraxerNavigationActivity中,我知道:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawer_navigation);
    setTitle(R.string.app_name);
    //########################################################### set initial fragment
    if (savedInstanceState == null){
        Bundle b = getIntent().getExtras();
        Fragment fragment = null;
        if (b.getInt("link") == 1) {
            //fragment = new AgendaMainFragment();
            fragment = new ProgrammeFragment();
        } else if (b.getInt("link") == 2) {
            fragment = new MediaMainFragment();
        } else if (b.getInt("link") == 3) {
            fragment = new ThemesMainFragment();
        } else if (b.getInt("link") == 4) {
            fragment = new AdresseMainFragment();
        } else if (b.getInt("link") == 5) {
            fragment = new InfoMainFragment();
        } else if (b.getInt("link") == 6) {
            fragment = new NewsMainFragment();
        } else if (b.getInt("link") == 7) {
            fragment = new BlockMainFragment();
        } else if (b.getInt("link") == 8) {
            fragment = new DemosMainFragment();
        }

        FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.replace(R.id.frag_drawer_container, fragment);
        ft.commit();
    }

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}
 @Override
public boolean onNavigationItemSelected(MenuItem item) {
    Fragment fragment = null;
    // Handle navigation view item clicks here.
    int id = item.getItemId();
    if (id == R.id.nav_home) {
        //super.onBackPressed();
        //return true;
        startActivity(new Intent(DrawerNavigationActivity.this, MainActivity.class));
        return false;
    } else if (id == R.id.nav_agenda) {
        //fragment = new AgendaMainFragment();
        fragment = new ProgrammeFragment();
    } else if (id == R.id.nav_gallery) {
        fragment = new MediaMainFragment();
    } else if (id == R.id.nav_themes) {
        fragment = new ThemesMainFragment();
    } else if (id == R.id.nav_adresse) {
        fragment = new AdresseMainFragment();
    } else if (id == R.id.nav_info) {
        fragment = new InfoMainFragment();
    } else if (id == R.id.nav_news) {
        fragment = new NewsMainFragment();
    } else if (id == R.id.nav_blocs) {
        fragment = new BlockMainFragment();
    } else if (id == R.id.nav_demo_animation) {
        fragment = new DemosMainFragment();
    } else {
        return true;
    }

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.replace(R.id.frag_drawer_container, fragment);
    ft.commit();

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

,对于MediaMainFragment,我使用tabLyout和FragmentStatePagerAdapter:

public class MediaMainFragment extends Fragment {
...
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {


    TabLayout tabLayout = getView().findViewById(R.id.tabsLayout);
    tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.photos_ttr)));
    tabLayout.addTab(tabLayout.newTab().setText(getResources().getString(R.string.video_ttr)));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = getView().findViewById(R.id.pager);
    final PagerAdapter adapter = new PagerAdapter(getFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));

    tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {
        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {
        }
    });
}

}

在我的PhotosFragment中,我捕捉到一个异常,我想显示一个对话框警报,问题是如果我从抽屉菜单中加载PhotosFragment,它就可以正常工作,但是如果它是从MainActtivity中加载的,它就不会显示出来。

这是我想从PhotosFragment显示对话框的方法,这是PhotosFragment类中的方法

public String loadJSONFromAsset() {
    String json = null;
    try {
        File file = new File(getContext().getFilesDir(), "image.json");
        InputStream is = new FileInputStream(file);
        int size = is.available();
        byte[] buffer = new byte[size];
        is.read(buffer);
        is.close();
        json = new String(buffer, "UTF-8");
    } catch (IOException ex) {
        ex.printStackTrace();

        // check internet message dialog
        new DynamicDialog.Builder(getContext())
                // Set dialog title.
                .setTitle("test")
                .setMessage("Check Internet")
                .show();

        return null;
    }
    return json;
}

我对传递给Builder的上下文有疑问,但是我不知道该如何解决。

0 个答案:

没有答案