作者:我我檬檬我我186 | 来源:互联网 | 2023-08-07 19:48
问题正如标题所述。当选择导航抽屉中的某个项目时,发送片段将以字符串形式将url发送给主要活动
发送Frag的代码:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
String newurl = webview.getUrl();
System.out.println(newurl);
Intent intent = new Intent(getactivity().getBaseContext(),Mainactivity.class);
intent.putExtra("newurl",newurl);
getactivity().startactivity(intent);
return true;
}
接收活动的代码:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
int itemId = item.getItemId();
//creating fragment object
Fragment fragment = null;
String newurl = this.getIntent().getExtras().getString("newurl");
System.out.println(newurl);
//initializing the fragment object which is selected
if (itemId == R.id.nav_assignments) {
Fragment fragment1 = new HomeFragment();
((HomeFragment) fragment1).setName(newurl);
displaySelectedFragment(fragment1);
} else if (itemId == R.id.nav_grades) {
fragment = new GalleryFragment();
displaySelectedFragment(fragment);
} else if (itemId == R.id.nav_people) {
fragment = new SlideshowFragment();
displaySelectedFragment(fragment);
} else if (itemId == R.id.nav_syllabus) {
fragment = new ToolsFragment();
displaySelectedFragment(fragment);
} else if (itemId == R.id.nav_discussions) {
fragment = new ShareFragment();
displaySelectedFragment(fragment);
} else if (itemId == R.id.nav_settings) {
fragment = new SendFragment();
displaySelectedFragment(fragment);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
我一直在使用System.out.println调试url何时为空,并且发送片段中的那段代码没有初始化。当我按导航抽屉中的项目时,应用程序崩溃。我该如何解决? (顺便说一下,一些额外的代码是因为我正在将数据从发送碎片发送到主活动,然后再发送到另一个碎片)