如何制作新活动?
我已经尝试过这段代码,但是当我启动它时我的应用程序崩溃了......
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.newfolder:
startActivity(new Intent(this, Newfolder.class));
return true;
default:
return super.onOptionsItemSelected(item);
}
}
LogCat错误:
02-04 18:48:07.987: E/AndroidRuntime(839): FATAL EXCEPTION: main
02-04 18:48:07.987: E/AndroidRuntime(839): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.androidexplorer/com.example.androidexplorer.Newfolder}; have you declared this activity in your AndroidManifest.xml?
02-04 18:48:07.987: E/AndroidRuntime(839): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1508)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1384)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.app.Activity.startActivityForResult(Activity.java:3190)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.app.Activity.startActivity(Activity.java:3297)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.example.androidexplorer.MainActivity.onMenuItemSelected(MainActivity.java:105)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:950)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:735)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:149)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.view.menu.ListMenuPresenter.onItemClick(ListMenuPresenter.java:163)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.widget.AdapterView.performItemClick(AdapterView.java:292)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.widget.AbsListView.performItemClick(AbsListView.java:1058)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2514)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.widget.AbsListView$1.run(AbsListView.java:3168)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.os.Handler.handleCallback(Handler.java:605)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.os.Looper.loop(Looper.java:137)
02-04 18:48:07.987: E/AndroidRuntime(839): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-04 18:48:07.987: E/AndroidRuntime(839): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 18:48:07.987: E/AndroidRuntime(839): at java.lang.reflect.Method.invoke(Method.java:511)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-04 18:48:07.987: E/AndroidRuntime(839): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-04 18:48:07.987: E/AndroidRuntime(839): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您的Newfolder
是否延长了活动?
您是否在AndroidManifest.xml中声明了Newfolder
个活动?(查看here如何操作)
发布你的堆栈跟踪!
答案 1 :(得分:0)
这里欢迎使用Logcat ...但我猜您忘了在清单中添加newFolder活动。
答案 2 :(得分:0)
试试这个,首先创建菜单文件夹,然后编写menu.xml文件。你必须给每个项目ID。
menu.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="@+id/newfolder"
android:orderInCategory="100"
android:showAsAction="never"
android:title="@string/un_subscribe">
</item>
将此代码写入您的Java代码。
@Override
public boolean onMenuItemSelected(int featureId, MenuItem item) {
// TODO Auto-generated method stub
super.onMenuItemSelected(featureId, item);
switch (item.getItemId()) {
case R.id.newfolder:
startActivity(new Intent(this, Newfolder.class));
default:
return super.onOptionsItemSelected(item);
}
}
答案 3 :(得分:-1)
android中最常见的错误是使用this
关键字。我想你应该使用startActivity(new Intent(this, Newfolder.class));
startActivity(new Intent(YourActivityName.this, Newfolder.class));
,因为this
这里很可能是一个事件监听器对象。