When beginners try to jump between activities in different Java package directories, they often get ActivityNotFoundException at runtime. Next let's take a look at the implementation of Activity jump under different package directories.
First, we create two activities: JumpTestActivity and JumpTestActivity2. JumpTestActivity has a button. When you press this button, the page jumps to JumpTestActivity2. The function is very simple.
First, implement the JumpTestActivity code, which is located in the hust. iprai. asce1885 package:
[Java]
Public class JumpTestActivity extends Activity {
Private Button mButton;
/** Called when the activity is first created .*/
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
MButton = (Button) findViewById (R. id. button );
MButton. setOnClickListener (new View. OnClickListener (){
@ Override
Public void onClick (View v ){
Intent mIntent = new Intent ();
MIntent. setClass (JumpTestActivity. this, JumpTestActivity2.class );
MIntent. setFlags (Intent. FLAG_ACTIVITY_CLEAR_TOP );
StartActivity (mIntent );
JumpTestActivity. this. finish ();
}
});
}
}
The code for implementing JumpTestActivity2 is located in the hust. iprai. asce1885.tmp package:
[Java]
Public class JumpTestActivity2 extends Activity {
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
TextView mTextView = new TextView (this );
MTextView. setText ("ASCE1885 ");
MTextView. setTextColor (Color. RED );
SetContentView (mTextView );
}
}
The key to redirection is in the AndroidManifest. xml file. The file content is as follows:
[Html]
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "hust. iprai. asce1885"
Android: versionCode = "1"
Android: versionName = "1.0" type = "codeph" text = "/codeph">
<Uses-sdk android: minSdkVersion = "8"/>
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". JumpTestActivity"
Android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
<Activity android: name = ". tmp. JumpTestActivity2"> </activity>
</Application>
</Manifest>
We can see that JumpTestActivity2 must be named. tmp. jumpTestActivity2 relative package name, named in any other form, such as the complete package name. hust. iprai. asce1885.tmp. jumpTestActivity2 and so on will get ActivityNotFoundException at runtime. The reason is very simple. The attribute package = "hust. iprai. asce1885 "has clearly pointed out that the operations in <manifest> are based on hust. iprai. asce1885. it indicates that it is relative to hust. iprai. asce1885.
Someone may ask, when the packages of the two activities do not have a inclusion relationship, for example, JumpTestActivity is in hust. iprai. asce1885, while JumpTestActivity2 is in hust. iprai. in wen1885, how can I set the manifest file? It's easy. Since you cannot use relative paths, use absolute paths:
[Html]
<Activity android: name = "hust. iprai. wen1885.JumpTestActivity2"/>
From ASCE1885's column