Android中不同包路徑下Activity跳轉的實現(解決ActivityNotFoundException) 。
初學者在嘗試位於不同Java包目錄下Activity之間的跳轉時,經常會在運行時得到ActivityNotFoundException,下面就來看看不同包目錄下Activity跳轉的實現。
首先,我們創建兩個Activity:JumpTestActivity和JumpTestActivity2,JumpTestActivity中有個按鈕,當用戶按下時會跳轉到JumpTestActivity2,功能很簡單。
首先實現JumpTestActivity的代碼,它位於hust.iprai.asce1885包中:
- 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();
- }
-
- });
- }
- }