我的应用程序使用带有几个菜单项的导航抽屉,这些菜单项基本上可以打开不同的片段。每个片段可以运行一个 AsyncTask,之后显示 Toast 消息。但是,当用户在另一个片段正在运行时尝试打开一个片段时,我会收到一个空指针错误,这是可以理解的,因为原始片段已被分离。
但是,即使在那时也有可能举行 Toast 表演吗?当在导航器上单击项目时,我基本上有此代码。
1 2 3 4 5 6 | public void setFragment(Fragment fragment) { getSupportFragmentManager() .beginTransaction() .replace(R.id.content, fragment) .commit(); } |
然后在片段上只是一个简单的 asynctask,它在 PostExecute 上显示 Toast 消息。
1 | Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); |
感谢任何帮助。
更新(2018 年 3 月 28 日)
我尝试使用监听器,它似乎工作。
我基本上有一个 BaseFragment,我的所有片段都在其中扩展。我只是在其中添加了一个回调。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | public class BaseFragment extends Fragment {
private OnToast callback;
@Override public void onAttach(Context context) { super.onAttach(context); try { callback = (BaseFragment.OnToast) context; } catch (ClassCastException e) { throw new ClassCastException(context.toString() +" must implement OnAuthenticateListener"); } }
public OnToast getCallback() { return callback; }
@Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setRetainInstance(true); setHasOptionsMenu(true); }
public interface OnToast { void toast(String message); } } |
在我的实际片段中,我有:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 | public class JailsFragment extends BaseFragment {
private List<Jail> jailList = new ArrayList<>();
@Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); setTitle("Jails"); setAdapter(new JailAdapter());
if (savedInstanceState == null) { onSwipeRefresh(); }
}
@Override public void onSwipeRefresh() { super.onSwipeRefresh(); setRefreshing(true); jailList.clear(); FreeNAS.getInstance().getJails().getJails(new Callback<List<Jail>>() { @Override public void onResponse(Call<List<Jail>> call, Response<List<Jail>> response) { if (response.code() == 200) { jailList.addAll(response.body()); notifyDatasetChanged(); } else { //Toast.makeText(getContext(), response.message(), Toast.LENGTH_LONG).show(); getCallback().toast(response.message()); } setRefreshing(false); }
@Override public void onFailure(Call<List<Jail>> call, Throwable t) { //Toast.makeText(getContext(), t.getLocalizedMessage(), Toast.LENGTH_LONG).show(); getCallback().toast(t.getLocalizedMessage()); setRefreshing(false); } }); }
} |
然后在我的 MainActivity 中:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, BaseFragment.OnToast {
private SharedPreferences pref; private Toolbar toolbar; private DrawerLayout drawer; private ActionBarDrawerToggle toggle; private NavigationView navigationView;
@Override public void toast(String message) { Toast.makeText(getApplicationContext(), message, Toast.LENGTH_LONG).show(); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
pref = PreferenceManager.getDefaultSharedPreferences(this);
toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout); toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close); drawer.addDrawerListener(toggle); toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) { setFragment(new AuthenticateFragment()); }
}
@Override public void onBackPressed() { if (drawer.isDrawerOpen(GravityCompat.START)) { drawer.closeDrawer(GravityCompat.START); } else { super.onBackPressed(); } }
@SuppressWarnings("StatementWithEmptyBody") @Override public boolean onNavigationItemSelected(MenuItem item) { // Handle navigation view item clicks here.
final int id = item.getItemId();
drawer.closeDrawer(GravityCompat.START); drawer.postDelayed(new Runnable() { @Override public void run() {
if (id == R.id.nav_plugins) { setFragment(new PluginsFragment()); } else if (id == R.id.nav_jails) { setFragment(new JailsFragment()); } else if (id == R.id.nav_jails_configuration) { setFragment(new ConfigurationFragment()); } else if (id == R.id.nav_services) { setFragment(new ServicesFragment()); } else if (id == R.id.nav_alerts) { setFragment(new AlertsFragment()); } else if (id == R.id.nav_updates) { setFragment(new UpdatesFragment()); } } }, 300);
return true; }
public void setFragment(Fragment fragment) { getSupportFragmentManager() .beginTransaction() .replace(R.id.content, fragment) .commit(); }
} |
本文来自网络,不代表无忧安卓网立场,转载请注明出处。
所有评论(0)