我正在创建导航抽屉,它一直有效,直到我开始添加片段,它在fragmentTransaction.commit();
之后崩溃。但我无法理解为什么会这样。谢谢!
这是我的代码:
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
FragmentTransaction fragmentTransaction;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar= (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle=new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.drawer_open,R.string.drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.add(R.id.main_container, new HomeFragment());
fragmentTransaction.commit();
getSupportActionBar().setTitle("Home . . .");
}
这是日志:
FATAL EXCEPTION: main
Process: com.example.hakobm.navdrawer, PID: 351
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.hakobm.navdrawer/com.example.hakobm.navdrawer.MainActivity}: java.lang.RuntimeException: com.example.hakobm.navdrawer.MainActivity@f0b255b must implement OnFragmentInteractionListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
Caused by: java.lang.RuntimeException: com.example.hakobm.navdrawer.MainActivity@f0b255b must implement OnFragmentInteractionListener
at com.example.hakobm.navdrawer.HomeFragment.onAttach(HomeFragment.java:75)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1231)
at android.support.v4.app.FragmentTransition.addToFirstInLastOut(FragmentTransition.java:1085)
at android.support.v4.app.FragmentTransition.calculateFragments(FragmentTransition.java:976)
at android.support.v4.app.FragmentTransition.startTransitions(FragmentTransition.java:95)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2146)
at android.support.v4.app.FragmentManagerImpl.optimizeAndExecuteOps(FragmentManager.java:2103)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2013)
at android.support.v4.app.FragmentController.execPendingActions(FragmentController.java:388)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:607)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:178)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1237)
at android.app.Activity.performStart(Activity.java:6268)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
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)
答案 0 :(得分:0)
使用以下代码调用您的片段...
FragmentTransaction fragmenttransaction = getSupportFragmentManager().beginTransaction();
HomeFragment regcomplainfragment = new HomeFragment();
fragmenttransaction.replace(R.id.main_container, regcomplainfragment).addToBackStack("tag");
fragmenttransaction.commit();
并删除以下行;
getSupportActionBar().setTitle("Home . . .");
答案 1 :(得分:0)
我认为这对你有用
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawer = (RelativeLayout) findViewById(R.id.drawer);
mDrawerList = (ListView) findViewById(R.id.drawer_listview);
if (savedInstanceState == null) {
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main_container,new HomeFragment());
tx.commit();
}
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_open, R.string.drawer_close);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
actionBarDrawerToggle.syncState();
title = new String[]{ Here you use menu title
};
icon = new int[]{ here show menu icon};
mMenuAdapter = new HomeMenuListAdapter(context, title, icon);
mDrawerList.setAdapter(mMenuAdapter);
mMenuAdapter.notifyDataSetChanged();
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
switch (position) {
case 0:
}
ft.commit();
mDrawerList.setItemChecked(position, true);
drawerLayout.closeDrawer(mDrawer);
}
});
}
答案 2 :(得分:0)
您的MainActivity应该实现接口OnFragmentInteractionListener