Intent not working (Showing errors) - android

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

Related

app unfortunately stopped when use "Intent intent = new ...." method to change screen

I have created a simple app.
one screen of my app has following code and I use a Button (id "sendManual") to change the app view to another one.
please look at the end of this code, you can seen the button id "sendManual".
I have setup it to change screen view to App2Activity.java
(I have put the Code of App2Activity java below this code, then you can check it also)
I installed this app on my android phone, But when I click Button(id"sendManual") app suddenly Stopped and says
Unfortunately,XXXXX app has stopped
public class showPermission implements View.OnClickListener {
Dialog dialog;
Activity activity;
public void showDialog(final Activity activity){
dialog = new Dialog(activity);
dialog.setCancelable(false);
this.activity=activity;
dialog.setTitle("XXXXX");
dialog.setContentView(R.layout.permission);
Button Sendcancel = (Button) dialog.findViewById(R.id.sendCancel);
Button SendNow = (Button) dialog.findViewById(R.id.sendNow);
Button sendManual = (Button) dialog.findViewById(R.id.sendManual);
sendManual.setOnClickListener(this);
SendNow.setOnClickListener(this);
Sendcancel.setOnClickListener(this);
dialog.show();
}
#Override
public void onClick(View view) {
int id=view.getId();
if(id==R.id.sendCancel){
dialog.dismiss();
}
else if(id==R.id.sendNow){
sendSMS();
}
else if(id==R.id.sendManual){
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
}
}
Here is the code of App2Activity.java
public class App2Activity extends Activity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
findViewById(R.id.start).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
sendSMS2();
}
});
}
............ etc
Here is manifest file.
<uses-permission android:name="android.permission.SEND_SMS"/>
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<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>
</application>
You are not properly set context in Intent.Use this
Intent intent = new Intent(getApplicationContext(), App2Activity.class);
startActivity(intent);
instead of
Intent intent = new Intent(activity, App2Activity.class);
activity.startActivity(intent);
This all is happening because you didn't declare App2Activity in Manifest.xml. That is compulsory to declare all the activities in manifest file.
Replace your manifest.xml with this one:
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="16" />
<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=".App2Activity"/>
</application>

How to open an activity in a package subfolder from another activity?

I have problem with my manifest. When I try to open the second Activity, I get the error android.content.ActivityNotFoundException: No Activity found to handle Intent { act=com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION }
Find bellow my Manifest .xml contain
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.xxxxx.monapplication" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="15" android:targetSdkVersion="23" />
<application android:allowBackup="true" android:icon="#mipmap/ic_launcher" android:label="#string/app_name" android:supportsRtl="true" android:theme="#style/AppTheme" >
<activity android:name="com.xxxxx.monapplication.application.Accueil" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.xxxxx.monapplication.application.OwnerRegistration" >
<intent-filter>
<action android:name="android.intent.action.APPLICATION.OWNERREGISTRATION" />
<category android:name="android.intent.category.APPLICATION.DEFAULT" />
</intent-filter>
</activity>
An the part of my code to open second activity
btnMel.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent("com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION");
startActivity(i);
}
}
Please help me to find the error.
You can call activity by its class name, then put your custom intent to setAction.
private String YOUR_ACTION = "com.xxxxx.monapplication.APPLICATION.OWNERREGISTRATION"
//uou can use this instead.
//Intent i = new Intent(this /* caller context */, OwnerRegistration.class);
Intent i = new Intent();
i.setAction(YOUR_ACTION);
startActivity(i);
try this code..
Intent myIntent = new Intent(YourActivity.this, OWNERREGISTRATION.class); startActivity(myIntent);

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

android intent filter?

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.

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.

Categories

Resources