Starting a new activity
I'm relatively new to programming in general so be gentle =| I'm trying to start a new activity from a basic one that displays a couple of text inputs, a checkbox, and a button. When the button is pressed I want it to switch to the new activity. The code compiles but when I press the button in Android it simply crashes. Any help would be greatly appreciated.
Here is the code sample:
public class Something extends Activity implements OnClickListener
{
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button login = (Button)findViewById(R.id.login);
login.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
startActivity(new Intent().setClass(Something.this, That.class));
}
}
Error Log:
I/ActivityManager( 240): Starting activity: Intent { cmp=jano.huerta.sfgc/.HOME } D/AndroidRuntime(13612): Shutting down VM W/dalvikvm(13612): threadid=1: thread exiting with uncaught exception (group=0x4 0025a08) E/AndroidRuntime(13612): FATAL EXCEPTION: main E/AndroidRuntime(13612): java.lang.RuntimeException: Unable to start activity Co mponentInfo{jano.huerta.sfgc/jano.huerta.sfgc.HOME}: android.content.ActivityNot FoundException: Unable to find explicit activity class {jano.huerta.sfgc/jano.hu erta.sfgc.SUMMARY}; have you declared this activity in your AndroidManifest.xml?
E/AndroidRuntime(13612): at android.app.ActivityThread.performLaunchActiv ity(ActivityThread.java:2787) E/AndroidRuntime(13612): 开发者_JAVA技巧 at android.app.ActivityThread.handleLaunchActivi ty(ActivityThread.java:2803) E/AndroidRuntime(13612): at android.app.ActivityThread.access$2300(Activi tyThread.java:135) E/AndroidRuntime(13612): at android.app.ActivityThread$H.handleMessage(Ac tivityThread.java:2136) E/AndroidRuntime(13612): at android.os.Handler.dispatchMessage(Handler.ja va:99) E/AndroidRuntime(13612): at android.os.Looper.loop(Looper.java:144) E/AndroidRuntime(13612):
at android.app.ActivityThread.main(ActivityThrea d.java:4937) E/AndroidRuntime(13612): at java.lang.reflect.Method.invokeNative(Native Method) E/AndroidRuntime(13612): at java.lang.reflect.Method.invoke(Method.java:5 21) E/AndroidRuntime(13612): at com.android.internal.os.ZygoteInit$MethodAndA rgsCaller.run(ZygoteInit.java:868) E/AndroidRuntime(13612): at com.android.internal.os.ZygoteInit.main(Zygot eInit.java:626) E/AndroidRuntime(13612): at dalvik.system.NativeStart.main(Native Method)E/AndroidRuntime(13612): Caused by: android.content.ActivityNotFoundException: U nable to find explicit activity class {jano.huerta.sfgc/jano.huerta.sfgc.SUMMARY }; have you declared this activity in your AndroidManifest.xml? E/AndroidRuntime(13612): at android.app.Instrumentation.checkStartActivit yResult(Instrumentation.java:1563) E/AndroidRuntime(13612): at android.app.ActivityThread.resolveActivityInf o(ActivityThread.java:2597) E/AndroidRuntime(13612): at android.app.LocalActivityManager.startActivit y(LocalActivityManager.java:277) E/AndroidRuntime(13612): at android.widget.TabHost$IntentContentStrategy. getContentView(TabHost.java:651) E/AndroidRuntime(13612): at android.widget.TabHost.setCurrentTab(TabHost. java:323) E/AndroidRuntime(13612): at android.widget.TabHost.addTab(TabHost.java:21 3) E/AndroidRuntime(13612): at jano.huerta.sfgc.HOME.onCreate(HOME.java:23) E/AndroidRuntime(13612): at android.app.Instrumentation.callActivityOnCre ate(Instrumentation.java:1069) E/AndroidRuntime(13612): at android.app.ActivityThread.performLaunchActiv ity(ActivityThread.java:2751) E/AndroidRuntime(13612): ... 11 more W/ActivityManager( 240): Force finishing activity jano.huerta.sfgc/.HOME W/ActivityManager( 240): Force finishing activity jano.huerta.sfgc/.SFGC W/ActivityManager( 240): Activity pause timeout for HistoryRecord{466bbec0 jano .huerta.sfgc/.HOME} D/dalvikvm( 406): GC_EXPLICIT freed 418 objects / 26008 bytes in 90ms I/Process (13612): Sending signal. PID: 13612 SIG: 9 I/ActivityManager( 240): Process jano.huerta.sfgc (pid 13612) has died. I/WindowManager( 240): WIN DEATH: Window{466cb800 jano.huerta.sfgc/jano.huerta. sfgc.SFGC paused=true} W/ActivityManager( 240): Activity destroy timeout for HistoryRecord{46654450 ja no.huerta.sfgc/.SFGC}
The most common mistake here is forgetting to register the 'That' activity in your AndroidManifest.xml. Have you done that?
Also, it would be much easier to help you if you pasted the stack trace from your crash. You can see crash info, amongst other logging, by running adb logcat
.
Never forget to add the <activity>
tag in the Manifest.xml.
All activities MUST be added in the Manifest File. Android Activity is an essential Android Component which should be registered.
Why?
The Activity is started and destroyed by Android OS. So when you call,
Intent i = new Intent(this, TargetActivity.class);
startActicvity(i);
Then the OS looks for the Activity in the Manifest.xml
file. If it does not find the matching activity then you will get following error:
android.content.ActivityNot FoundException: Unable to find explicit activity
How to add activity in Manifest file?
Add this in the Manifest file.
<activity android:name=".TargetActivity"/>
It is the most basic form. You can explore more HERE
精彩评论