单击其中一个抽屉菜单后应用程序停止工作
Application stop working after clicking on one of drawer menus
我正在学习如何使用抽屉更改不同菜单中的片段,但是当我使用脚本单击菜单时,它是 "Unfortunately, DrawerTest2 has stopped"
谁能看看我的代码,看看我做错了什么?
MainActivity.java
package th.ac.sd.drawertest2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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 void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
SMSFragment smsFragment = new SMSFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout_sms, smsFragment, smsFragment.getTag()).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
SMSFragment.java
package th.ac.sd.drawertest2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class SMSFragment extends Fragment {
public SMSFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sm, container, false);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
content_main.xml
<?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:id="@+id/content_main"
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="th.ac.sd.drawertest2.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
fragment_sm.xml(我想替换的片段content_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="th.ac.sd.drawertest2.SMSFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/layout_sms" />
</RelativeLayout>
错误日志(从开始到应用程序崩溃)
01-11 23:51:52.017 1065-1065/? I/art: Not late-enabling -Xcheck:jni (already on)
01-11 23:51:52.073 1065-1065/th.ac.sd.drawertest2 W/System: ClassLoader referenced unknown path: /data/app/th.ac.sd.drawertest2-1/lib/x86
01-11 23:51:52.088 1065-1065/th.ac.sd.drawertest2 I/InstantRun: Instant Run Runtime started. Android package is th.ac.sd.drawertest2, real application class is null.
[ 01-11 23:51:52.124 1605: 1626 D/ ]
HostConnection::get() New Host Connection established 0x9c53bd40, tid 1626
01-11 23:51:53.149 1065-1065/th.ac.sd.drawertest2 W/System: ClassLoader referenced unknown path: /data/app/th.ac.sd.drawertest2-1/lib/x86
01-11 23:51:53.276 1065-1065/th.ac.sd.drawertest2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-11 23:51:53.339 1065-1107/th.ac.sd.drawertest2 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 01-11 23:51:53.342 1065: 1065 D/ ]
HostConnection::get() New Host Connection established 0xaa23b180, tid 1065
[ 01-11 23:51:53.344 1065: 1065 W/ ]
Process pipe failed
[ 01-11 23:51:53.383 1065: 1107 D/ ]
HostConnection::get() New Host Connection established 0xaa23b3c0, tid 1107
01-11 23:51:53.385 1065-1107/th.ac.sd.drawertest2 I/OpenGLRenderer: Initialized EGL, version 1.4
01-11 23:51:53.468 1065-1107/th.ac.sd.drawertest2 E/EGL_emulation: tid 1107: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
01-11 23:51:53.468 1065-1107/th.ac.sd.drawertest2 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaabfbc0, error=EGL_BAD_MATCH
01-11 23:52:04.548 1065-1065/th.ac.sd.drawertest2 W/PathParser: Points are too far apart 4.000000596046461
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 E/FragmentManager: No view found for id 0x7f0c0077 (th.ac.sd.drawertest2:id/layout_sms) for fragment SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 E/FragmentManager: Activity state:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Local FragmentActivity 575a3e1 State:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mCreated=truemResumed=true mStopped=false mReallyStopped=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mLoadersStarted=true
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Active Fragments in 3e0a4a2:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: #0: SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mFragmentId=#7f0c0077 mContainerId=#7f0c0077 mTag=null
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mAdded=true mRemoving=false mFromLayout=false mInLayout=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mRetainInstance=false mRetaining=false mUserVisibleHint=true
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mFragmentManager=FragmentManager{3e0a4a2 in HostCallbacks{1ba533}}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Added Fragments:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: #0: SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: FragmentManager misc state:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mCurState=5 mStateSaved=false mDestroyed=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: View Hierarchy:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: com.android.internal.policy.PhoneWindow$DecorView{8d10f0 V.E..... ... 0,0-1440,2560}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{d98669 V.E..... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.ViewStub{1c868ee G.E..... ... 0,0-0,0 #10203a9 android:id/action_mode_bar_stub}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{861618f V.E..... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.FitWindowsLinearLayout{ca4c1c V.E..... ... 0,0-1440,2392 #7f0c0055 app:id/action_bar_root}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ViewStubCompat{2fb2025 G.E..... ... 0,0-0,0 #7f0c0056 app:id/action_mode_bar_stub}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ContentFrameLayout{a5625fa V.E..... ... 0,0-1440,2392 #1020002 android:id/content}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v4.widget.DrawerLayout{f9c87ab VFED.... ... 0,0-1440,2392 #7f0c0069 app:id/drawer_layout}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.CoordinatorLayout{3e0e08 V.ED.... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.AppBarLayout{4e299a1 V.E..... ... 0,84-1440,280}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.Toolbar{2d0a7c6 V.E..... ... 0,0-1440,196 #7f0c006b app:id/toolbar}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.TextView{f19f387 V.ED.... ... 210,51-609,144}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.ImageButton{7aa02b4 VFED..C. ... 0,0-196,196}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ActionMenuView{b6e2edd V.E..... ... 1300,0-1440,196}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{c1f7a52 VFED..C. ... 0,14-140,182}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.RelativeLayout{e184123 V.E..... ... 0,280-1440,2392 #7f0c006d app:id/content_main}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{83e9620 V.ED.... ... 56,56-321,122}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.FloatingActionButton{123dbd9 VFED..C. ... 1188,2140-1384,2336 #7f0c006c app:id/fab}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.NavigationView{c16e99e V.E..... ... 0,0-980,2392 #7f0c006a app:id/nav_view}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuView{f23cc7f VFED.V.. F.. 0,0-980,2392 #7f0c0073 app:id/design_navigation_view}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{3c6f44c V.E..... ... 0,0-980,588 #7f0c0072 app:id/navigation_header_container}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{8ed5c95 V.E..... ... 0,0-980,560}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatImageView{f8401aa V.ED.... ... 56,92-224,316 #7f0c0078 app:id/imageView}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{732b19b V.ED.... ... 56,316-924,438}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{b950938 V.ED.... ... 56,438-693,504 #7f0c0079 app:id/textView}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{942d11 VFED..C. ... 0,588-980,756}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{f88e76 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{a80cc77 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{22d80e4 VFED..C. ... 0,756-980,924}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{efd894d V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{571c02 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{b2bb913 VFED..C. ... 0,924-980,1092}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{bb3c750 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{a266d49 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{71ef64e VFED..C. ... 0,1092-980,1260}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{58ed36f V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{916087c V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{df9505 V.E..... ... 0,1260-980,1292}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.View{578295a V.ED.... ... 0,28-980,32}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{65f378b V.ED.... ... 0,1292-980,1460}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{9f93068 VFED..C. ... 0,1460-980,1628}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{4497c81 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{3ff8126 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{687c167 VFED..C. ... 0,1628-980,1796}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{164eb14 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{d105fbd V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.View{65289b2 V.ED.... ... 0,2392-1440,2560 #1020030 android:id/navigationBarBackground}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/AndroidRuntime: Shutting down VM
--------- beginning of crash
01-11 23:52:05.430 1065-1065/th.ac.sd.drawertest2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: th.ac.sd.drawertest2, PID: 1065
java.lang.IllegalArgumentException: No view found for id 0x7f0c0077 (th.ac.sd.drawertest2:id/layout_sms) for fragment SMSFragment{4b506d #0 id=0x7f0c0077}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
有人知道我做错了什么吗?这是我的第一个应用程序,所以我可能做错了。
谢谢!
您没有用另一个替换您的 content_main
布局。您希望您的 content_main
有一个像这样的片段容器:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在您的代码中,您应该使用此代码片段将您的片段放入 FrameLayout
:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,fragment)
.commit();
在你的情况下,将你的 content_main
布局复制到另一个文件,比如 first_fragmet.xml
。为它创建另一个片段并像在 SMSFragment
中那样膨胀它。
在您的 activity 的 onCreate()
方法中,使用上述代码放置第一个片段。例如
fragment = new FirstFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,fragment)
.commit();
当您希望用 SMSFragment
替换当前视图时,只需使用上面相同的代码,但使用 fragment
作为您的 SMSFragment
对象。
有关详细信息,请参阅 this tutorial。
我正在学习如何使用抽屉更改不同菜单中的片段,但是当我使用脚本单击菜单时,它是 "Unfortunately, DrawerTest2 has stopped"
谁能看看我的代码,看看我做错了什么?
MainActivity.java
package th.ac.sd.drawertest2;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
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 void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
SMSFragment smsFragment = new SMSFragment();
FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.layout_sms, smsFragment, smsFragment.getTag()).commit();
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
SMSFragment.java
package th.ac.sd.drawertest2;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {@link Fragment} subclass.
*/
public class SMSFragment extends Fragment {
public SMSFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_sm, container, false);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="@layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="@+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="@layout/nav_header_main"
app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
content_main.xml
<?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:id="@+id/content_main"
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="th.ac.sd.drawertest2.MainActivity"
tools:showIn="@layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</RelativeLayout>
fragment_sm.xml(我想替换的片段content_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="th.ac.sd.drawertest2.SMSFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="@string/hello_blank_fragment"
android:id="@+id/layout_sms" />
</RelativeLayout>
错误日志(从开始到应用程序崩溃)
01-11 23:51:52.017 1065-1065/? I/art: Not late-enabling -Xcheck:jni (already on)
01-11 23:51:52.073 1065-1065/th.ac.sd.drawertest2 W/System: ClassLoader referenced unknown path: /data/app/th.ac.sd.drawertest2-1/lib/x86
01-11 23:51:52.088 1065-1065/th.ac.sd.drawertest2 I/InstantRun: Instant Run Runtime started. Android package is th.ac.sd.drawertest2, real application class is null.
[ 01-11 23:51:52.124 1605: 1626 D/ ]
HostConnection::get() New Host Connection established 0x9c53bd40, tid 1626
01-11 23:51:53.149 1065-1065/th.ac.sd.drawertest2 W/System: ClassLoader referenced unknown path: /data/app/th.ac.sd.drawertest2-1/lib/x86
01-11 23:51:53.276 1065-1065/th.ac.sd.drawertest2 W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
01-11 23:51:53.339 1065-1107/th.ac.sd.drawertest2 D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
[ 01-11 23:51:53.342 1065: 1065 D/ ]
HostConnection::get() New Host Connection established 0xaa23b180, tid 1065
[ 01-11 23:51:53.344 1065: 1065 W/ ]
Process pipe failed
[ 01-11 23:51:53.383 1065: 1107 D/ ]
HostConnection::get() New Host Connection established 0xaa23b3c0, tid 1107
01-11 23:51:53.385 1065-1107/th.ac.sd.drawertest2 I/OpenGLRenderer: Initialized EGL, version 1.4
01-11 23:51:53.468 1065-1107/th.ac.sd.drawertest2 E/EGL_emulation: tid 1107: eglSurfaceAttrib(1165): error 0x3009 (EGL_BAD_MATCH)
01-11 23:51:53.468 1065-1107/th.ac.sd.drawertest2 W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xaaabfbc0, error=EGL_BAD_MATCH
01-11 23:52:04.548 1065-1065/th.ac.sd.drawertest2 W/PathParser: Points are too far apart 4.000000596046461
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 E/FragmentManager: No view found for id 0x7f0c0077 (th.ac.sd.drawertest2:id/layout_sms) for fragment SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 E/FragmentManager: Activity state:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Local FragmentActivity 575a3e1 State:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mCreated=truemResumed=true mStopped=false mReallyStopped=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mLoadersStarted=true
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Active Fragments in 3e0a4a2:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: #0: SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mFragmentId=#7f0c0077 mContainerId=#7f0c0077 mTag=null
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mState=1 mIndex=0 mWho=android:fragment:0 mBackStackNesting=0
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mAdded=true mRemoving=false mFromLayout=false mInLayout=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHidden=false mDetached=false mMenuVisible=true mHasMenu=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mRetainInstance=false mRetaining=false mUserVisibleHint=true
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mFragmentManager=FragmentManager{3e0a4a2 in HostCallbacks{1ba533}}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: Added Fragments:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: #0: SMSFragment{4b506d #0 id=0x7f0c0077}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: FragmentManager misc state:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mHost=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mContainer=android.support.v4.app.FragmentActivity$HostCallbacks@1ba533
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: mCurState=5 mStateSaved=false mDestroyed=false
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: View Hierarchy:
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: com.android.internal.policy.PhoneWindow$DecorView{8d10f0 V.E..... ... 0,0-1440,2560}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{d98669 V.E..... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.ViewStub{1c868ee G.E..... ... 0,0-0,0 #10203a9 android:id/action_mode_bar_stub}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{861618f V.E..... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.FitWindowsLinearLayout{ca4c1c V.E..... ... 0,0-1440,2392 #7f0c0055 app:id/action_bar_root}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ViewStubCompat{2fb2025 G.E..... ... 0,0-0,0 #7f0c0056 app:id/action_mode_bar_stub}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ContentFrameLayout{a5625fa V.E..... ... 0,0-1440,2392 #1020002 android:id/content}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v4.widget.DrawerLayout{f9c87ab VFED.... ... 0,0-1440,2392 #7f0c0069 app:id/drawer_layout}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.CoordinatorLayout{3e0e08 V.ED.... ... 0,0-1440,2392}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.AppBarLayout{4e299a1 V.E..... ... 0,84-1440,280}
01-11 23:52:05.428 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.Toolbar{2d0a7c6 V.E..... ... 0,0-1440,196 #7f0c006b app:id/toolbar}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.TextView{f19f387 V.ED.... ... 210,51-609,144}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.ImageButton{7aa02b4 VFED..C. ... 0,0-196,196}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ActionMenuView{b6e2edd V.E..... ... 1300,0-1440,196}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.ActionMenuPresenter$OverflowMenuButton{c1f7a52 VFED..C. ... 0,14-140,182}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.RelativeLayout{e184123 V.E..... ... 0,280-1440,2392 #7f0c006d app:id/content_main}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{83e9620 V.ED.... ... 56,56-321,122}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.FloatingActionButton{123dbd9 VFED..C. ... 1188,2140-1384,2336 #7f0c006c app:id/fab}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.widget.NavigationView{c16e99e V.E..... ... 0,0-980,2392 #7f0c006a app:id/nav_view}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuView{f23cc7f VFED.V.. F.. 0,0-980,2392 #7f0c0073 app:id/design_navigation_view}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{3c6f44c V.E..... ... 0,0-980,588 #7f0c0072 app:id/navigation_header_container}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.LinearLayout{8ed5c95 V.E..... ... 0,0-980,560}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatImageView{f8401aa V.ED.... ... 56,92-224,316 #7f0c0078 app:id/imageView}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{732b19b V.ED.... ... 56,316-924,438}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{b950938 V.ED.... ... 56,438-693,504 #7f0c0079 app:id/textView}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{942d11 VFED..C. ... 0,588-980,756}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{f88e76 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{a80cc77 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{22d80e4 VFED..C. ... 0,756-980,924}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{efd894d V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{571c02 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{b2bb913 VFED..C. ... 0,924-980,1092}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{bb3c750 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{a266d49 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{71ef64e VFED..C. ... 0,1092-980,1260}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{58ed36f V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{916087c V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{df9505 V.E..... ... 0,1260-980,1292}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.View{578295a V.ED.... ... 0,28-980,32}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatTextView{65f378b V.ED.... ... 0,1292-980,1460}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{9f93068 VFED..C. ... 0,1460-980,1628}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{4497c81 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{3ff8126 V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.design.internal.NavigationMenuItemView{687c167 VFED..C. ... 0,1628-980,1796}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.support.v7.widget.AppCompatCheckedTextView{164eb14 V.ED.... ... 56,0-924,168 #7f0c0074 app:id/design_menu_item_text}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.widget.FrameLayout{d105fbd V.E..... ... 924,0-924,168 #7f0c0076 app:id/design_menu_item_action_area}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/FragmentManager: android.view.View{65289b2 V.ED.... ... 0,2392-1440,2560 #1020030 android:id/navigationBarBackground}
01-11 23:52:05.429 1065-1065/th.ac.sd.drawertest2 D/AndroidRuntime: Shutting down VM
--------- beginning of crash
01-11 23:52:05.430 1065-1065/th.ac.sd.drawertest2 E/AndroidRuntime: FATAL EXCEPTION: main
Process: th.ac.sd.drawertest2, PID: 1065
java.lang.IllegalArgumentException: No view found for id 0x7f0c0077 (th.ac.sd.drawertest2:id/layout_sms) for fragment SMSFragment{4b506d #0 id=0x7f0c0077}
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1059)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1252)
at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:742)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1617)
at android.support.v4.app.FragmentManagerImpl.run(FragmentManager.java:517)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
有人知道我做错了什么吗?这是我的第一个应用程序,所以我可能做错了。
谢谢!
您没有用另一个替换您的 content_main
布局。您希望您的 content_main
有一个像这样的片段容器:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在您的代码中,您应该使用此代码片段将您的片段放入 FrameLayout
:
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,fragment)
.commit();
在你的情况下,将你的 content_main
布局复制到另一个文件,比如 first_fragmet.xml
。为它创建另一个片段并像在 SMSFragment
中那样膨胀它。
在您的 activity 的 onCreate()
方法中,使用上述代码放置第一个片段。例如
fragment = new FirstFragment();
getSupportFragmentManager().beginTransaction()
.replace(R.id.fragment_container,fragment)
.commit();
当您希望用 SMSFragment
替换当前视图时,只需使用上面相同的代码,但使用 fragment
作为您的 SMSFragment
对象。
有关详细信息,请参阅 this tutorial。