Genadinik
Genadinik

Reputation: 18649

Android - how to structure new Activity class?

I have a home page activity that I got from an example, which works fine. Now I am trying to make it have a button that makes the app go to the next screen.

So I added a second activity to the application manifest:

<activity
    android:name=".AddProblemActivity"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />

        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>        

But I wonder if it is supposed to have these two lines:

    <action android:name="android.intent.action.MAIN" />

    <category android:name="android.intent.category.LAUNCHER" />

It seems that MAIN is saying that there should be a main() function. But isn't that only supposed to exist on the home page?

Here is the full stack trace:

02-20 10:17:04.408: D/gralloc_goldfish(646): Emulator without GPU emulation detected.
02-20 10:18:58.655: D/AndroidRuntime(646): Shutting down VM
02-20 10:18:58.665: W/dalvikvm(646): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
02-20 10:18:58.745: E/AndroidRuntime(646): FATAL EXCEPTION: main
02-20 10:18:58.745: E/AndroidRuntime(646): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.problemio/com.problemio.AddProblemActivity}: java.lang.NullPointerException
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.os.Looper.loop(Looper.java:137)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 10:18:58.745: E/AndroidRuntime(646):  at java.lang.reflect.Method.invokeNative(Native Method)
02-20 10:18:58.745: E/AndroidRuntime(646):  at java.lang.reflect.Method.invoke(Method.java:511)
02-20 10:18:58.745: E/AndroidRuntime(646):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 10:18:58.745: E/AndroidRuntime(646):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 10:18:58.745: E/AndroidRuntime(646):  at dalvik.system.NativeStart.main(Native Method)
02-20 10:18:58.745: E/AndroidRuntime(646): Caused by: java.lang.NullPointerException
02-20 10:18:58.745: E/AndroidRuntime(646):  at com.problemio.AddProblemActivity.onCreate(AddProblemActivity.java:28)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.Activity.performCreate(Activity.java:4465)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 10:18:58.745: E/AndroidRuntime(646):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-20 10:18:58.745: E/AndroidRuntime(646):  ... 11 more

This is the part of my onCreate from AddProblemActivity causes the error it seems:

    final EditText problemName = (EditText) findViewById(R.id.problem_name);  
    String name = problemName.getText().toString(); // THIS LINE  

Here is my xml for is problem_name from the Layout xml:

    <EditText  
        android:id="@+id/problem_name"  
        android:layout_height="wrap_content"  
        android:hint="@string/problem_name"  
        android:inputType="textPersonName"  
        android:layout_width="fill_parent">  
    </EditText>  

Thanks, Alex

Upvotes: 0

Views: 508

Answers (2)

sachy
sachy

Reputation: 789

  1. Change the category tag of the new activity to android.intent.category.DEFAULT instead of android.intent.category.LAUNCHER.
  2. Change the name of the action to some other value e.g. android.intent.action.AddProblemActivity

Upvotes: 1

Sebastian Olsson
Sebastian Olsson

Reputation: 836

The variable problemName is probably null. Check if it is and make sure that you have done setContentView with a layout that contains such an id. If you do you could also try to clean the build environment if you use an IDE such as Eclipse or IntelliJ IDEA since they sometimes keep old generated R files.

You should not have this intent filter unless you want the second activity to show up on the home screen.

<activity
android:name=".AddProblemActivity"
android:label="@string/app_name" />

will do if you just want to be able to start the second activity (AddProblemActivity) from the first.

Have a look at http://developer.android.com/guide/topics/intents/intents-filters.html to learn about Intents and Intent filters.

Upvotes: 1

Related Questions