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