我有一个导航抽屉,在点击我需要打开特定片段的菜单时包含n个菜单。首先,我尝试了活动和意图。在使用碎片的情况下,它看起来不太好。我正在通过id选择它们并从活动中调用片段。在片段中,我正在使用饼图,其中它抛出空指针异常。我尝试从get视图更改,因为它仅在Oncreate View之后进行了实例化。但没有结果。
/** UI Automator Google GPS dialog box */
UiDevice mDevice = UiDevice.getInstance(InstrumentationRegistry.getInstrumentation());
UiObject uiObject = mDevice.findObject(new UiSelector().text("OK"));
try {
uiObject.click();
}catch (UiObjectNotFoundException e) {
e.printStackTrace();
}
答案 0 :(得分:0)
以下是有助于理解的示例代码。 首先使用OnCreate()中的片段替换activity_main.xml内容:
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
然后将此HomeFragment替换为onNavigationItemSelected()中的其他人:
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_home) {
// Handle the camera action
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HomeFragment()).commit();
} else if (id == R.id.nav_current_order) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new MyCurrentOrderFragment(),"Tag").commit();
} else if (id == R.id.nav_received_images) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HistoryFragment()).commit();
} else if (id == R.id.nav_rate_app) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new RateOurAppFragment()).commit();
} else if (id == R.id.nav_help) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new HelpFragment()).commit();
} else if (id == R.id.nav_settings) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new SettingsFragment()).commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
在您的content_main.xml中添加一个FrameLayout,并将其ID设置为" content_frame"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="bd.com.chalo.dsstudio.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:visibility="gone" />
<FrameLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"></FrameLayout>
</RelativeLayout>