0

How do I make a new Activity?

I've tried this code but my app crashes when I start it...

@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 errors:

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)
Sam
  • 86,580
  • 20
  • 181
  • 179
Raul Petrescu
  • 69
  • 1
  • 7

4 Answers4

0

Is your Newfolder extends Activity?
Did you declared Newfolder activity in the AndroidManifest.xml?(check here how to do it)

Post your stacktrace!

Community
  • 1
  • 1
EvZ
  • 11,889
  • 4
  • 38
  • 76
  • I'm not sure how this qualifies as an answer. If you would like to ask the author a question use a comment. At best these are wild guesses. – Sam Feb 04 '13 at 16:31
  • By asking him this 2 questions I described the most possible problems. – EvZ Feb 04 '13 at 16:34
  • What about the answers? It's nice to have answers post in the answer section. (Also there are many more possible problems...) – Sam Feb 04 '13 at 16:40
0

Logcat would be welcome here...But I guess you forgot to add your newFolder activity in your manifest.

psykhi
  • 2,981
  • 2
  • 16
  • 22
0

Try this, First create menu folder and then write menu.xml file. you must give for id each item.

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>

Write this code in your java code.

  @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);
    }

}
MuraliGanesan
  • 3,233
  • 2
  • 16
  • 22
-1

Most common mistake in android is in using this keyword. startActivity(new Intent(this, Newfolder.class)); in this line I think you should use startActivity(new Intent(YourActivityName.this, Newfolder.class)); because this here is most probably an event listener object.

MckyONeil
  • 94
  • 3
  • for this case may be you're right, but if the owner of 'onOptionsItemSelected' is a 'Context' inherited object, it's the case. – MckyONeil Feb 04 '13 at 16:30
  • Here my log.... https://dl-web.dropbox.com/get/log.txt?w=AADckJY-9-B5cObS2RfpJNtTCxzxbnrS0nh7Cz-2Idcolw – Raul Petrescu Feb 04 '13 at 16:52