0

如何创建新活动?

我已经尝试过这段代码,但是当我启动它时我的应用程序崩溃了......

@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)
4

4 回答 4

0

Logcat 会在这里受到欢迎......但我猜你忘了在你的清单中添加你的 newFolder 活动。

于 2013-02-04T16:31:20.600 回答
0

试试这个,首先创建菜单文件夹,然后编写 menu.xml 文件。您必须为每个项目提供 id。

菜单.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);
    }

}
于 2013-02-04T16:31:30.253 回答
0

你的Newfolder扩展活动吗?您是否在 AndroidManifest.xml 中
声明了活动?(在此处查看如何操作)Newfolder

发布您的堆栈跟踪!

于 2013-02-04T16:26:26.107 回答
-1

android中最常见的错误是使用this关键字。startActivity(new Intent(this, Newfolder.class));在这一行中,我认为您应该使用startActivity(new Intent(YourActivityName.this, Newfolder.class));,因为this这很可能是一个事件侦听器对象。

于 2013-02-04T16:26:13.393 回答