我在打开片段活动时遇到问题,我是Android设备编程的新手,但我觉得它非常有趣,我想为自己制作一些应用程序。
我有一个带滑动菜单的示例,它正在工作的一切,所以我有三个列表项(主页,设置,关于)我想打开一个活动,当我按下其中一个这是我的mainActivity,它会很好,如果有人应该告诉我如何完成此代码才能打开其他活动。
提前谢谢你。
我的主要活动是:
public class MainActivity extends BaseActivity {
FragmentStackManager fm, sliding_menu;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setUpView();
menuToggeleSetUp(savedInstanceState);
}
void setUpView() {
fm = new FragmentStackManager(this);
sliding_menu = new FragmentStackManager(this);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
fm.addFragment(new MainFragment(), R.id.main_frame, false, FragmentTransaction.TRANSIT_NONE, false);
sliding_menu.addFragment(new SlidingMenuFragment(), R.id.slide_fragment, false, FragmentTransaction.TRANSIT_NONE, false);
}
void menuToggeleSetUp(Bundle savedInstanceState) {
mDrawerToggle = new ActionBarDrawerToggle(this, drawer,
R.drawable.ic_drawer, // nav menu toggle icon
R.string.app_name
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
drawer.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
if (id == R.id.action_settings) {
System.out.println("CLEAR SETTINGS STARTED");
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
谢谢你的帮助 我想保留这个,因为汉堡包动画所以我发现了一些东西,我完成了我的代码,但在logcat上有一个错误 如果有人应该帮助我,那就太好了 所以我做的是这个:
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
private class DrawerItemClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
updateDisplay(position);
}
}
private void updateDisplay(int position) {
Fragment fragment = null;
switch (position) {
case 0:
System.out.println("ACTIVITY ONE");
//fragment = new Home_Fragment();
break;
case 1:
//fragment = new Settings_Fragment();
System.out.println("ACTIVITY TWO");
// startActivity(new Intent(this, Preferences.class));
break;
//case 2:
// fragment = new TB_Fragment();
//break;
default:
break;
}
if (fragment != null) {
android.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frame_container, fragment).commit();
// update selected item and title, then close the drawer
//setTitle(menutitles[position]);
drawer.closeDrawer(mDrawerList);
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
我的logcat是
java.lang.RuntimeException:无法启动活动ComponentInfo {com.intelligent_screen / com.intelligent.screen.View.MainActivity}:java.lang.NullPointerException:尝试调用虚方法'void android.widget.ListView.setOnItemClickListener (空载对象引用上的(android.widget.AdapterView $ OnItemClickListener))
答案 0 :(得分:0)
对于导航抽屉,请使用此示例 https://developer.android.com/training/implementing-navigation/nav-drawer.html 其中显示了如何打开每个导航项目的片段
并且用于打开新活动使用此
Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
finish();
startActivity(intent);