android eclipse如何使用intent在默认导航抽屉中显示第二个窗口
Posted
技术标签:
【中文标题】android eclipse如何使用intent在默认导航抽屉中显示第二个窗口【英文标题】:android eclipse how to use intent to show the second window in default navigation drawer 【发布时间】:2015-11-18 15:20:40 【问题描述】:所以我是新的android编程中的新人,我也没有Java的背景。
我的问题:我的代码可行吗?
这是默认的导航抽屉项目代码,我添加了代码意图,但是当我运行应用程序时,我得到“不幸的是应用程序已停止”,它位于 switch case 1 上。我试图做的是当用户单击第 1 部分时将显示第 1 部分的窗口。
在这里对不起新手。
package com.example.example;
public class MainActivity extends Activity implements NavigationDrawerFragment.NavigationDrawerCallbacks
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* @link #restoreActionBar().
*/
private CharSequence mTitle;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment) getFragmentManager().findFragmentById(
R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer, (DrawerLayout) findViewById(R.id.drawer_layout));
@Override
public void onNavigationDrawerItemSelected(int position)
// update the main content by replacing fragments
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
public void onSectionAttached(int number)
switch (number)
case 1:
mTitle = getString(R.string.title_section1);
Intent intent = new Intent(MainActivity.this, NavigationDrawerFragment.class);
startActivity(intent);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
public void restoreActionBar()
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
@Override
public boolean onCreateOptionsMenu(Menu menu)
if (!mNavigationDrawerFragment.isDrawerOpen())
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
return super.onCreateOptionsMenu(menu);
@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();
if (id == R.id.action_settings)
return true;
return super.onOptionsItemSelected(item);
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section number.
*/
public static PlaceholderFragment newInstance(int sectionNumber)
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
public PlaceholderFragment()
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
@Override
public void onAttach(Activity activity)
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt(ARG_SECTION_NUMBER));
【问题讨论】:
您是否在Android清单文件中提到了您的活动? span> @ArslanAhmed 我没有。怎么办? @Heyyou 是 .java 也是一个 .class 吗?它在主要活动所在的 src 中。 【参考方案1】:开始第二个活动时,您需要在意图中使用该活动的类名。
Intent intent = new Intent(MainActivity.this, NavigationDrawerFragment.class);
我看不到你创建了一个NavigationDrawerFragment
类。
这就是它没有响应的原因。
【讨论】:
我在 MainActivity 的 src 中有 NavigationDrawerFragment.java,.java 也是 .class 吗? 非常感谢您的帮助,我真的很感激。有些事情困扰着我,我们可以将片段制作/整合到空白活动中吗?谢谢!以上是关于android eclipse如何使用intent在默认导航抽屉中显示第二个窗口的主要内容,如果未能解决你的问题,请参考以下文章
用于 LibGDX 项目的 Eclipse Intent 插件