java - how to start new activity in android -
i want start new activity called counter
when button clicked, got error activity not found...so wrong in code:
t = new thread(){ public void run(){ try{ sleep(5000); } catch (interruptedexception e){ e.printstacktrace(); } finally{ intent counter = new intent("com.example.test.counter"); startactivity(counter); } } }; test.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { t.run(); } });
this manifest file :
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.test" android:versioncode="1" android:versionname="1.0" > <uses-sdk android:minsdkversion="8" android:targetsdkversion="17" /> <application android:allowbackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name="com.example.test.mainactivity" 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="com.example.test.counter" android:label="@string/title_activity_counter" > </activity> </application> </manifest>
change this
intent counter = new intent("com.example.test.counter"); startactivity(counter);
this called implicit intent , needs intent filter
to
intent counter = new intent(mainactivity.this,counter.class); startactivity(counter);
this called explicit intent , there no need intent filter
you should use explicit intent coz have
<activity android:name="com.example.test.counter" android:label="@string/title_activity_counter" > </activity>
quoting docs
explicit intents specify component start name (the fully-qualified class name). you'll typically use explicit intent start component in own app, because know class name of activity or service want start. example, start new activity in response user action or start service download file in background.
note: explicit intent delivered target, regardless of intent filters component declares.
edit:
you should call start()
on thread not run
Comments
Post a Comment