Android ActivityNotFoundException - android

I'm trying to create a very simple custom intent example. I've searched for this error and none of the forums have answers that work for me. Here are my files:
public class DemoImplicit extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void whenButtonIsClicked(View view) {
Intent intent = new Intent("com.example.action.NEW_ACTION"); //<<<<<<<
intent.addCategory("android.intent.category.DEFAULT"); //<<<<<<<
// Intent intent = new Intent("android.intent.action.VIEW");
// intent.addCategory("com.example.MY_CATEGORY");
startActivity(intent); //<<<<<<<
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demos" android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".SatisfyIntent" android:label="#string/app_name">
<intent-filter>
<!-- action android:name="android.intent.action.VIEW" / -->
<!-- category android:name="com.example.MY_CATEGORY" / -->
<action android:name="com.example.action.NEW_ACTION" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="9" />
</manifest>
These two separate files are in two different Eclipse projects, but I make sure to load the project containing the intent-filter onto the emulator before loading the file containing the startActivity call onto the emulator. In any case, I always get an ActivityNotFoundException.
What am I doing wrong?
P.S. Here's the AndroidManifest.xml file for the project containing DemoImplicit.java:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.demos"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".DemoImplicit"
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>
<uses-sdk android:minSdkVersion="9" />
</manifest>

Fisrtly you shuld ensure that your AndroidManifest.xml file must have defined the DemoImplicit Activity in this.
As like this:<activity android:name=".DemoImplicit"/>
Also in your code you have aspecified the SatisfyIntent as a launcher Activity
<activity android:name=".SatisfyIntent" android:label="#string/app_name">
But here it seems like you have nothing like this in your Java Code.
So the Bottom line is that: Activity which you want to run must have defined in your AndroidManifest.xml file.

Related

Launcher Activity is opening twice in my new SDK

I am recently facing a problem while creating new android application in both eclipse as well as android studio.
I am using same SDK in both eclipse and android studio.
When I create a new application and i simply run it. The launcher activity was getting loaded twice.
Means, I am getting MainActivity loading twice one on top of another.
OnCreate() method of my MainActivity also got invoked twice.
The code goes like follows
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sample"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="21"
android:targetSdkVersion="22" />
<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>
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.println("OnCreate Invoked");
}
}
Output
OnCreate Invoked
OnCreate Invoked
Can you please help me in resolving this issue.
Thanks in advance.
It seems you are getting multiple instance of your first activity.
Use this in manifest of 1st activity:
android:launchMode="singleTop"
If I set orientation programatically in onCreate - I can reproduce this issue.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
If needed, set activity orientation in the manifest file like this.
<activity
android:name=".MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

How does an android program understands which class or/and activity to call first?

As i am new to android programming, I don't know how to call a certain class and/or certain activity first.
For example i have two classes viz. 1) Login.java 2) Create.java and two xml files associated with them are activity_main.xml and create_new.xml respectively.
So how can i make Login.java run first with activity_main.xml as screen?
in login java use this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);}
in create java us this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.create_new);
in androidmanifest
<activity
android:name=".Login"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
and another activities
<activity android:name=".MainActivity" ></activity>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.your.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.example.your.package.activity.Login"
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.your.package.activity.Create"
android:label="#string/app_name" >
</activity>
</application>
</manifest>
In this case, the intent-filter of LOGIN activity specifies that the intent with action of MAIN and category of LAUNCHER will be caught by it, aka it is where the application starts.
Afterwards,
public class Login extends Activity
{
//honestly I'd name this class LoginActivity and same in the XML
#Override
public void onCreate(Bundle saveInstanceState)
{
super.onCreate(saveInstanceState);
setContentView(R.layout.activity_main);
}
....
}
Also look at this example to learn how to use Fragments:
NullPointerException accessing views in onCreate()
from the intent filters you decalre in the android manifest file of your project;see the image
You declare an intent filter in the activity tag something like shown in the image for making it the first activity of your application.and if there are other activities in your app having intent filters than you have to just change the capital letter MAIN to DEFAULT in the intent filter tag
You will find AndroidManifest file like in the following image in your project

How to use an intent to call up another class?

I am having an issue when trying to go from the main screen of my app to display the data that i have collected in my SQLiteDatabase. Here is my Application manifest...
I have tried switching the Main.java file from LAUNCHER to DEFAULT and the opposite for the database java file to make it open when it is run and i can get it to display that class that way but not using the button that I have in the Main.java file that is for changing to see the data.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.innovativesolutions.gpsareafinder"
android:versionCode="2"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="14" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.innovativesolutions.gpsareafinder.Main"
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.innovativesolutions.gpsareafinder.Locationdbview"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
Here is the section in my Main.java file that has to do with the Intent where I am trying to call the Location database java file.
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bcalculate:
area();
break;
case R.id.bclear:
clear();
break;
case R.id.bloc:
Intent in = new
Intent("com.innovativesolutions.gpsareafinder.Locationdbview");
startActivity(in);
break;
I think the error is something simple in my manifest or where I try to use the Intent to switch views...If anyone has any ideas please help. Thanks!
Change your second activity in the manifest to:
<activity
android:name="com.innovativesolutions.gpsareafinder.Locationdbview"
android:label="#string/app_name" >
</activity>
You don't need any filters there.
Change the call in your firstActivity to:
startActivity(new Intent(this, Locationdbview.class));
You can also try this in Main.java
Class myclass =Class.forName("com.innovativesolutions.gpsareafinder.Locationdbview");
Intent in=new Intent(Main.this,myclass);
startActivity(in);
In Manifest.xml :
<activity android:name=".Locationdbview" ></activity>

Android: I added another button but now the app crashes

I added another button to my app but now the app crashes on start up. Here is the code that I added in, when I block it out the app runs fine.
btnnext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("com.com.com.addtask"));
}
});
}
I think it might be a problem with the manifest so here is the manifest too (and this is only a playing around app so dont hassle me about the package being com.com.com)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.com.com"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET"/>
<application android:icon="#drawable/ic_launcher" android:label="#string/app_name" android:permission="android.permission.INTERNET">
<activity android:name=".HelloWorldActivity"
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=".addtask"
android:label="#string/app_name">
<intent-filter>
<action android:name="com.com.com.addtask" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
And this might be helpful too:
05-16 21:21:55.446: E/AndroidRuntime(581): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.com.com/com.com.com.HelloWorldActivity}: java.lang.NullPointerException
Have you declared the new button that you have added within the activity?
Button btnnext = (Button)findViewById(R.id.myNewButton);
If you don't do this and set a listener it will throw the nullpointer error.
First identify the button from xml to activity using
Button b = (Button) findViewById(R.id.btnNext);

Sample AlarmController application Service registeration in AndroidManifest

I am new to Android and trying to implement the sample AlarmController application. Everything is fine but its Alarm Service is not working. I was wondering that do I need to register these services in the AndroidManifest.xml file as the other activities a are registered and if yes. How do I do that, I guess the alarm controller code is Available on Android's website therefore I am just putting the AndroidManifest.xml code of my application.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AlarmController"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AlarmActivity"
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>
</manifest>
You need a <service> element inside your <application> element, indicating the class that implements the service.
Here is a sample project from one of my books demonstrating the use of a Service.
Here is a sample project demonstrating WakefulIntentService, an open source component I wrote to simplify the use of AlarmManager when you want the alarms to wake up the phone.
I just found it as the "CommonsWare" said in the other answer there needs to be a service tag in AndroidManifest.xml
?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.AlarmController"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".AlarmActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<service android:name="AlarmService_service" />
</application>
</manifest>
So whatever service, reciever, activity and etc you are using, you need to register it in your AndroidManifest.xml.

Categories

Resources