How to use an intent to call up another class? - android

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>

Related

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

Android Broadcast receiver works on emulator but not on device

So here is the setup.. I have two apps AppReceiver.apk and AppClient.apk. AppReceiver project has just one receiver and no other activity and AppClient just sends the broadcast message. This works in my emulator with Android 4.0 api level 14 on my mac but not in windows or on actual device.
Code for AppReceiver.apk
*MyReceiver.java*
public class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
Log.d("MyReceiver", "Got Message");
}
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appreceiver"
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" >
<receiver android:name="MyReceiver">
<intent-filter>
<action android:name="com.appreceiver.CUSTOM_INTENT"></action>
</intent-filter>
</receiver>
</application>
</manifest>
Code for AppClient
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sendButton = (Button)findViewById(R.id.sendbroadcastButton);
sendButton.setOnClickListener(new View.OnClickListener(){
public void onClick(View v)
{
Intent intent = new Intent();
intent.setAction("com.appreceiver.CUSTOM_INTENT");
sendBroadcast(intent);
}
});
}
Manifest file
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.appclient"
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.appclient.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>
</manifest>
So here is what is happening when I click on the sendbroadcast button in my emulator that is running in Mac then I see the log message Log.d("MyReceiver", "Got Message"); in my logcat meaning it is working as expected. I can click more than once and it works as expected.
However when I installed it on the device I do not see the message in logcat (I made sure I am reading the logcat from device) Also I have another windows laptop and on that one it won't show this message on the emulator either. So I am not sure what is going on? Any pointers?
So I confirmed that I will have to start AppReceiver.apk at least once after install and this is a new requirement or safety thing after Android 3.1 as they want user to explicitly start an app in order to user its broadcast receiver.
Since I did not wanted any GUI thing, the way I did is just wrote an activity which is not calling any view and defined the theme as android:theme="#android:style/Theme.NoDisplay" in manifest so the user won't notice after install that the app has already started and the receiver is also available. Thanks #Nitin Sethi for pointing me in the right direction.
public class MyActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
}
Here is how the new manifest for AppReceiver.apk looks
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.appreceiver.MyActivity" android:theme="#android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<receiver android:name="com.appreceiver.MyReceiver">
<intent-filter>
<action android:name="com.appreceiver.CUSTOM_INTENT"></action>
</intent-filter>
</receiver>
</application>

android content activitynotfoundexception no activity found to handle intent - when trying to go to url

I am trying to write an app where you can type in an address and then you get redirected to google maps. (I suppose this is called implicit intent)
-I have created an intent to launch the main activity, which is the only activity in my app.
The Main activity consists of some text, an editfield and a button.
AndroidManifest.xml:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.where_do_you_live"
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>
<uses-permission android:name="android.permission.INTERNET" />
</manifest>
this is the code for the button:
public void Button1Click(View view)
{
try
{
addressField=(EditText)findViewById(R.id.address);
String address=addressField.getText().toString();
address=address.replace(' ','+');
Intent geoIntent=new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse("geo:0,0?q=" + address));
startActivity(geoIntent);
}
catch(Exception e)
{
TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(e.toString());
//finding stuff
}
}
If you are testing this in emulator, things are different than in a device.
When you are creating your Android Virtual Device, you should select Google APIs as your target. If you do not have them installed, you can use SDK Manager to download it.
Have a look at this.

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

Android ActivityNotFoundException

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.

Categories

Resources