android intent filter? - android

I'm feeling stupid.This is very clear but I can not solve my problem.So excuse me for my question.
My problem is in about intenfilter.This is application tag of my manifest file:
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:label="#string/app_name"
android:name=".AlakyTestActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="reza"
android:name=".A2" >
<intent-filter >
<action android:name="MAIN" />
<category android:name="LAUNCHER" />
</intent-filter>
</activity>
</application>
And this is my button click listener:
b1 = (Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent2 = new Intent();
intent2.setAction("MAIN");
intent2.addCategory("LAUNCHER");
startActivity(intent2);
}
});
I think that all things is good but when I run my code and click on b1,I get this erroe:
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=MAIN cat=[LAUNCHER] }
Edit:
This is A2:
public class A2 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main2);
}
}
Please help me.

You should set the android:name of the second activity to the (package name).(the class)
for example, lets say the second activity class is 'com.my.app.reza' you should add you the manifest:
<activity
android:label="#string/app_name"
android:name=".reza" >
<intent-filter >
<action android:name="com.my.app.REZA" />
<category android:name="android.intent.category.DEFUALT" />
</intent-filter>
</activity>
and you should start the activity like that:
Intent intent = new Intent("com.my.app.REZA");
startActivity(intent);
NOTE that it isn't the best way to do it, you shouldn't mess to much with package name I'd recommend you to do it the following way:
<activity
android:label="#string/app_name"
android:name=".reza" />
and start it like that:
startActivity(new Intent( this.getContext() , reza.class );

Please use like that:
Intent intent2 = new Intent(context,A2.class);
startActivity(intent2);
android:name=".A2" ,you must have "A2" activity class implement!
Modify to android:name=".A2", not android:name="A2"!

You don't need to specify category if you just need to call A2 inside your app. And you should set an unique action name, for example it can be a hash string:
...
Intent intent2 = new Intent("a202bfa923069ee8e119205e3468ee131ceafda37");
startActivity(intent2);
Note that action name uses same rule as package name.

<intent-filter>
<action android:name="com.blacky.basictutorial.TutorialTwo" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
Try to use this in your second activity and call by the following code:
startActivity(new Intent("com.blacky.basictutorial.TutorialTwo"));
Hope this will work for you.

Related

Intent Not working in MenuInflater and with Buttons

So I was trying to work on graphics and wanted to use intent to switch between activities and its not the first time I am doing it but I really cannot find the error. So I am posting the code for my mainactivity and manifest here. Please tell me the cause of the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//fullscreen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent p = new Intent("com.example.menuinflater.GFX");
startActivity(p);
}
});
}
and Manifest looks something like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".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=".About"
android:label="#string/title_activity_about"
android:theme="#android:style/Theme.Dialog" >
<intent-filter>
<action android:name="android.intent.action.About" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".Prefs"
android:label="#string/title_activity_about" >
<intent-filter>
<action android:name="android.intent.action.Prefs" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity
android:name=".GFX"
android:label="#string/title_activity_gfx" >
</activity>
</application>
I hope that makes clear where am I going wrong and I would want an answer with explanation. I would highly appreciate any solution.
Thanks in advance!
I suggest reading the basic training pages on developer.android.com, they contain alot of basic and useful information.
About your specific problem, this link ought to help: http://developer.android.com/training/basics/firstapp/starting-activity.html
The intent should be like this:
Intent p = new Intent(this, GFX.class);

Contact Picker intent returns to the wrong activity

I'm calling a Contact Picker from an activity named AddFriendActivity. Once a contact is picked, it always returns to the MainActivity rather than the calling activity.
I have checked the android manifest and I can't see any tags that would cause it to prefer MainActivity on resumption. I've extracted (what I think is) the relevant code below.
AndroidManifest.xml
<activity
android:name="x.MainActivity"
android:label="#string/activity_main_title" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="x.start.StartActivity"
android:label="#string/start_activity_title"
android:noHistory="true" >
</activity>
<activity
android:name="x.policies.PoliciesActivity"
android:label="#string/activity_policies_title" >
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="x" />
<activity
android:name="x.AddFriendActivity"
android:label="#string/title_activity_add_friend"
android:parentActivityName="x.friendster.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="MainActivity" />
</activity>
Code for starting the contact picker from AddFriendActivity
btnContacts.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent contactpicker = new Intent(Intent.ACTION_PICK, Contacts.CONTENT_URI);
startActivityForResult(contactpicker, 1);
Log.e("contactpicker", "meh start");
}
});
Not sure if this is relevant, but here's the code that calls the AddFriendActivity from a fragment ChatPanel in MainActivity
btnAddFriend.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
Intent intent = new Intent(getActivity(), AddFriendActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
Any idea why this is happening? I've probably missed something simple...
remove line intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
from your OnClickListener and see if it works.
Cheers

Intent not working (Showing errors)

while starting intent on image button click "Intent intent = new Intent(this,Settings.class);" showing error in eclipse. Any thing wrong on my code? Here's my code.. Help appreciated..
imageButton =(ImageButton)findViewById(R.id.imageButton4);
imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(this,Settings.class);
startActivity(intent);
}
});
You should use getApplicationContext() instead of this
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(), Settings.class);
startActivity(intent);
}
});
You have to do two things
First
start your activity like this
Intent intent = new Intent(getBaseContext, Setting.class);
startActivity(intent);
Second thing Mention this class into Android manifest.xml
inside application tag
below the by default activitytag
I think this way you can resolve your problem.
do this
imageButton =(ImageButton)findViewById(R.id.imageButton4);
imageButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(YourCurrentCLass.this,Settings.class);
startActivity(intent);
}
});
and check your manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.sampledatabase"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
if you only see <category android:name="android.intent.category.LAUNCHER" />
and no <category android:name="android.intent.category.DEFAULT" />
create a new activity like this
<activity
android:name=".Settings"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="name.of.your.package.Settings" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
remember to put it inside your <application tag same as your .MainActivity
try below code.
public void onClick(View v) {
Intent intent = new Intent(yourclassName.this,Settings.class);
startActivity(intent);
}
});
Intent intent = new Intent(this,Settings.class);
use this line of code
Intent intent = new Intent(yourclassName.this,Settings.class);
Because you using anonymus class for OnClickListener.
So you can provide the reference with class name. this
Add your Settings activity in AndroidManifest.xml

Android app crash: Unable to instantiate activity ComponentInfo

07-18 04:48:22.465: E/AndroidRuntime(19105): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.liamwli.fa.yearbook/com.liamwli.fa.yearbook.Home}: java.lang.NullPointerException
That is the error I get.
I have defined the Home class in the manifest:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.liamwli.fa.yearbook"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Home"
android:label="#string/app_name"
android:theme="#android:style/Theme.Holo" >
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And it only started doing this when I added the putExtras line in my main activity:
enter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
myname = name.getText().toString();
Intent i = new Intent("com.liamwli.fa.yearbook.HOME");
i.putExtra("myname", myname);
startActivity(i);
}
});
So, can anyone please explain what is happening?
use this:
Intent i = new Intent(context,otherActivity.class);
context of that activity from where you want to start activity.otherActivity is the name of activity which you want to start
Try this once
Intent i = new Intent(MainActivity.this,Home.class);
i.putExtra("myname", myname);
startActivity(i);
And I don't think this is required in Manifest
<intent-filter>
<action android:name="com.liamwli.fa.yearbook.HOME" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
change like this
Intent i = new Intent(MainActivity.this, Home.class);
i.putExtra("myname", myname);
startActivity(i);
Try this...
Intent i = new Intent( YourActivityName.this , otherActivity.class);
Ok, I looked and it looked like I was setting a variable contents outside of the onCreate method. So that is why it wasn't working.

Start an Activity not from an Intent

How can I start an Activity without using an Intent? The only rule I have got is
if( var == true ) startActivity();
but startActivity(); needs an Intent as a parameter.
Just create a new intent for the activity you want to start. depending on where you are you will need the app context thought.
Intent i = new Intent(getApplicationContext(), YourActivity.class);
startActivity(i);
Here's how to navigate to a second Activity (another page) using an Intent.
public void onClick(View v)
{
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
Also, don't forget to adjust the AndroidManifest.xml for each Activity.
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name="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="SecondActivity"
android:label="#string/second_label">
<intent-filter>
<action android:name="android.intent.action.SECOND" /> //should be namespace of your company I guess
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>

Categories

Resources