I am trying to transfer an int value between two activities using intents, but my app keeps crashing. When I comment out the transfer of any data and simply use an intent, everything seems to work. I cannot tell what is wrong.
Activity 1 (HeartRateActivity):
//Imports
public class HeartRateActivity extends Activity {
/** Called when the activity is first created. */
Button nextActivity;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
nextActivity = (Button)findViewById(R.id.nextActivity);
nextActivity.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View v) {
Intent intent = new Intent(HeartRateActivity.this, NextActivity.class);
intent.putExtra("age", 2);
startActivity(intent);
}
});
}
}
My NextActivity.java
package com.heartRate;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class NextActivity extends Activity {
TextView display;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.next);
int age = getIntent().getIntExtra("age", 0);
display = (TextView) findViewById(R.id.display);
display.setText(age);
}
}
My AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.heartRate" android:versionCode="1" android:versionName="1.0">
<uses-sdk android:minSdkVersion="7" />
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name=".HeartRateActivity"
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=".NextActivity"
android:label="#string/app_name" />
</application>
</manifest>
My main.xml (used by HeartRateActivity)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<Button android:layout_width="wrap_content" android:layout_height="wrap_content"
android:id="#+id/nextActivity" android:text="nextActivity"></Button>
</LinearLayout>
My next.xml(used by NextActivity) is similar and i dont think thats the issue...:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:text="TextView" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/display"></TextView>
</LinearLayout>
I would appreciate help in solving this issue! Thank you
Replace
display.setText(age);
with
display.setText(Integer.toString(age));
If you provide an int as a parameter, it uses it as a resource ID, which, in this case, obviously doesn't exist.
just try do like this.
display = (TextView) findViewById(R.id.display);
display.setText(Integer.toString(age));
and surely it will work
Make sure that your Id and Intent Destination all must be Ok.
then just put between intent from initialize ans start.
with put
intent.putExtra("age", Double);
from get this.
double d = getIntent().getStringExtra("age");
Make sure that R.id.display is a valid TextView in your next layout.
Just so we're clear, you're saying when you include the ...
intent.putExtra("age", 2);
in the sending Activity, and the ...
int age = getIntent().getIntExtra("age", 0);
in the receiving Activity, the application crashes or expereinces problems? And, by excluding those 2 statements the application performs normally? I'm just curious what does LogCat show? Even though you application is crashing LogCat will have several entries as to what was happening just prior to the close.
Related
I'm new in Android studio and I'm trying to make two activities in my application but it don't works.
I don't know what I'm doing wrong, I think it's about the first options I put in my intent.
I'm new and I would like a simple solution or if you can explain me what I have to do it will be fine :)
This is my first activity (MainActivity):
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button mPasserelle = null;
public final static String AGE = "com.myapplis.multiactivite.AGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPasserelle = (Button) findViewById(R.id.passerelle);
mPasserelle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent secondeActivite = new Intent(MainActivity.this,IntentExample.class);
secondeActivite.putExtra(AGE,24);
startActivity(secondeActivite);
}
});
}
}
The second activity(IntentExample) :
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class IntentExample extends Activity{
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.seconde_activite);
Intent i = getIntent();
int age = i.getIntExtra(MainActivity.AGE,0);
TextView resultat= (TextView)findViewById(R.id.resultat);
resultat.setText("Le résultat est : "+age);
}
}
This is my first layout (activity_main), just a simple button to push to get the second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.dunomade.multiactivite.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/passerelle"/>
</LinearLayout>
And now this is my second layout(seconde_activite), just a simple text to show me I'm on the second activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/resultat"
android:text="" />
</LinearLayout>
And to finish, this is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapplis.multiactivite">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".IntentExample"
android:label="seconde acivite">
</activity>
</application>
</manifest>
When I'm running the app, MainActivity is good, but when I try to click on the button the AVD show me "Have you declared this activity in your AndroidManifest ?". I declared it on my manifest but I think the problem is in my MainActivity.
Please help me, I think for you it's really simple but I can't solve it, I tryed many way but I already got the same error.
Thank's for read and for answer =)
I advise that you use the name of package before the name of your activity, like this (some devices need this, like Moto G) :
<activity android:name="your.package.IntentExample "></activity>
And instead use:
public class IntentExample extends Activity
use:
public class IntentExample extends AppCompatActivity
I am writing a basic application which contains two activities. Both contain a TextView showing the title and the first one contains an EditText in which the user types a message and clicks on a button on its side, the second activity is launched which shows the message the user types.
It has the following problem:
1.When I click on the button, the app stops saying "Unfortunately Write n Display and stopped.", rather than launching the second activity at all.
The Logcat can be found here: enter link description here since adding it to the question exceeded the limit.
CODE OF FIRST ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
//import android.view.Menu;
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
EditText editText=(EditText) findViewById(R.id.EditText1_MainActivity);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
LAYOUT OF FIRST ACTIVITY: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/TextView1_MainActivity"
android:layout_toRightOf="#id/EditText1_MainActivity"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
</RelativeLayout>
CODE OF SECOND ACTIVITY: -
package com.practice.myfirstapp1;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
class SecondActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
Intent intent= getIntent();
String intent_value= intent.getStringExtra(MainActivity.key_name);
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
}
}
LAYOUT OF SECOND ACTIVITY: -
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
tools:context=".SecondActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
STRINGS RESOURCE FILE:-
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Write n Display</string>
<string name="action_settings">Settings</string>
<string name="title_MainActivity">WRITE</string>
<string name="EditText_MainActivity">Your Message here</string>
<string name="Button_MainActivity">Send</string>
<string name="title_SecondActivity">DISPLAY</string>
</resources>
ANDROID MANIFEST FILE: -
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.practice.myfirstapp1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme"
android:debuggable="true" >
<activity
android:name="com.practice.myfirstapp1.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.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
</application>
</manifest>
To solve your first issue: put below code to your textview of both xml files.
Reason: You had not put the android:layout_centerHorizontal="true" in your TextView
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="#string/title_MainActivity"
To solve your second issue:Change LAYOUT OF FIRST ACTIVITY:
Reason: android:layout_width is "0dp". it should be wrap_content or fill_parent or match_parent
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To:
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content" //you can put, fill_parent or match_parent
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1_MainActivity"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
To solve your third issue: change your CODE OF FIRST ACTIVITY: -
Reason: You had put editText=(EditText) findViewById(R.id.EditText1_MainActivity); in sendMessage(View view). send message contains a View so whenever you initiate any component in this method, it will search that component on this particular view which is incorrect. you can initiate your components in any method which does not contain any view parameter.if you want to initiate any component in particular view then you have to initiate it like editText=(EditText)view.findViewById(R.id.EditText1_MainActivity);. But in your case edittext is in main view so you need to remove it from sendMessage(View view).
public class MainActivity extends Activity {
public static final String key_name="com.practice.firstApp.key";
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(EditText) findViewById(R.id.EditText1_MainActivity);
}
public void sendMessage(View view){
Intent intent= new Intent(this, SecondActivity.class);
String key_value= editText.getText().toString();
intent.putExtra(key_name, key_value);
startActivity(intent);
}
}
Change your second Activity
Reason: TextView textView= new TextView(this); is not correct way to initialize any component of your xml view.
TextView textView= new TextView(this);
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
To:
TextView textView;
textView= (TextView) findViewById(R.id.TextView2_SecondActivity);
textView.setText(intent_value);
you can solve your problem like this
1.replace
android:gravity="center_horizontal" by android:layout_gravity="center_horizontal".
2.Give layout_width as wrap_content for edittext of first activity xml
3.Make your sendMessage method public not private
To get the logcat In Eclipse, Goto Window-> Show View -> Other -> Android-> Logcat.
OR
Write LogCat in Quick Access edit box in your eclipse window (top right corner, just before Open Prospective button). And just select LogCat it will open-up the LogCat window in your current prospec
change private void sendMessage(View view) to public void sendMessage(View view) (private to public)
LAYOUT OF FIRST ACTIVITY:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical" >
<TextView
android:id="#+id/TextView1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_MainActivity"
android:textStyle="bold"/>
<EditText
android:id="#+id/EditText1_MainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/EditText_MainActivity"
android:textStyle="italic" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button_MainActivity"
android:onClick="sendMessage"/>
LAYOUT OF SECOND ACTIVITY: -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:gravity="center_vertical"
tools:context=".SecondActivity">
<TextView
android:layout_alignParentTop="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#+string/title_SecondActivity"
android:textStyle="bold"/>
<TextView
android:id="#+id/TextView2_SecondActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Logcat shows the following:
Caused by: android.content.ActivityNotFoundException: Unable to find
explicit activity class
{com.practice.myfirstapp1/com.practice.myfirstapp1.SecondActivity};
have you declared this activity in your AndroidManifest.xml?
You did define SecondActivity in manifest, but the package name is wrong ( android:name="com.practive.myfirstapp1.SecondActivity" should be ndroid:name="com.practice.myfirstapp1.SecondActivity"
).
<activity
android:name="com.practice.myfirstapp1.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.practive.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
It should be
<activity
android:name="com.practice.myfirstapp1.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.practice.myfirstapp1.SecondActivity"
android:label="#string/app_name">
</activity>
I am new to android development. After setting up an android project, I tried to get text input and passe it to another activity (screen).
When I run the project I don't get an error but when I click the application it shows the first screen then when click the button in the screen it gives the error 'application stopped unexpectedly'.
When I try the code without passing data from first screen to second screen , the application works properly.
This is MainActivity.java file:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText et= (EditText) findViewById(R.id.editText1);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//intent class is used for activating another or component or an activity
Intent intent =new Intent(MainActivity.this, Second.class);
intent.putExtra("textval", et.getText().toString());
startActivity(intent);
}
});}}
Here is the code for Second.java file:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Here is the activitymain.xml code:
<?xml version="1.0" encoding="UTF-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText android:id="#+id/editText1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
>
<requestFocus />
</EditText>
<Button android:id="#+id/button1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:layout_below="#+id/editText1" android:layout_marginTop="28dp"
android:text="#string/button" />
</RelativeLayout>
Here is the second xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:orientation="vertical">
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/textview" />
</LinearLayout>
Please help me to find the error. Since I don't get an error notification I am not able to proceed.
Thanks in advance...
This is what you missed on your Second.java:
setContentView(R.layout.second);
put it above of your textview declaration.
In your Second java file make changes as follows
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class Second extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
}
Declare Second Activity in android manifest file .
<activity android:name=".Second"/>
Add setContentView(R.layout.second); in second activity.
Add entry for second activity in your Manifest file.
Put first activity as a Launcher and other as a Default.
Here I have two activity MainActivity and Player.
First I Launches MainActivity and then call other activity Player
Note- Activity name should be same as class name, so Keep same in Manifest file as well.
Also you need to specify the layout file for both activity by setContentView in onCreate function.
Here is sample code-
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vt.soc"
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>
<activity
android:name=".Player"
android:label="#string/title_activity_main" >
<intent-filter>
<action android:name="com.vt.soc.PALYER" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
- In your Second Activity you forgot to add the setContentView().
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv= (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("textval"));
}
- Please also do see that you have added this Second Activity in your Manifest.xml file.
I've been playing around with this for some time now and am hoping that someone smarter than I will be able to help. My boss has given me an opportunity to learn to develop Android apps and I'm struggling with this one issue. I need to build several apps with multiple activities but can't seem to get past this point. I'm sure it's a problem in my coding, but with so many different ways of doing things, I'm a little confused at this point.
I am building an app that opens a Main activity with two buttons on the page. One is the close button and works fine. I want the next button to open a mapActivity that is set to my campus' location. I am not a student working on a project I just work for a community college. ;-)
What's really wierd is that the EXACT same google.maps code works fine by itself. I just can't get the button to open the Activity without crashing.
Here is my manifest.xml, main.xml, my main.java, Map.java and map.xml. would you please comment and point me in the right direction?
Thanks in advance,
Dave
here are two errors:
11-21 14:50:58.968: W/dalvikvm(437): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
11-21 14:50:58.978: E/AndroidRuntime(437): android.content.ActivityNotFoundException: No Activity found to handle Intent { act=edu.mtsac.mapproject.MAP }
This says the Activity was not found but I can see it in my source files, and in the manifest here:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.mtsac.mapproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="3"
android:targetSdkVersion="8" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<uses-library android:name="com.google.android.maps" />
<activity
android:label="#string/app_name"
android:name=".Main" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- map activity -->
<activity
android:label="Map"
android:name=".Map" >
<intent-filter >
<action android:name="edu.mtsac.mapproject.MAP" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
My Main.xml and Map.xml files are simple:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="#+id/mapBtn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Map" />
</RelativeLayout>
Map.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.google.android.maps.MapView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="0jevUfyLD_b1Eikgpm_mo7KVDspzhPJdRDDaxEw"
android:clickable="true"
android:enabled="true" />
</LinearLayout>
the Main.java
package edu.mtsac.mapproject;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mapBtn = (Button) findViewById(R.id.mapBtn);
mapBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Map.class));
}
});
Button closeBtn = (Button) findViewById(R.id.closeBtn);
closeBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
};// end onCreate()
}
and lastly the Map.java
package edu.mtsac.mapproject;
import android.os.Bundle;
import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapActivity;
import com.google.android.maps.MapController;
import com.google.android.maps.MapView;
public class Map extends MapActivity {
MapController mc;
GeoPoint p;
MapView mapview;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.map);
mapview = (MapView) findViewById(R.id.mapView);
mapview.displayZoomControls(true);
mapview.setBuiltInZoomControls(true);
mapview.setSatellite(true);
mc = mapview.getController();
String coord[] = { "34.047517", "-117.847050" };
double lat = Double.parseDouble(coord[0]);
double lng = Double.parseDouble(coord[1]);
p = new GeoPoint((int) (lat * 1E6), (int) (lng * 1E6));
mc.animateTo(p);
mc.setZoom(17);
mapview.invalidate();
}
#Override
protected boolean isRouteDisplayed() {
return false;
}
}
I don't see any activity in your manifest registered to handle this action edu.mtsac.mapproject.MAP. I assume that exception is happening when you call:
startActivity(new Intent("edu.mtsac.mapproject.MAP"));
It looks like you're probably trying to start your Map Activity for which you don't need to specify an Action. Just do something like:
startActivity(new Intent(this, Map.class));
You probably want to read this doc on intents to understand what's really going on here and how you should be using these classes.
In the "map" button click implementation you create an Intent whose action is "edu.mtsac.mapproject.MAP". Since your manifest's Intent Filter is looking for action "android.intent.action.MAP" it will not resolve to your activity.
I believe you were confused by explicit vs. implicit intents. Explicit intents use the specific name of the activity you want to use but implicit intents only define an action you want to accomplish.
To get this to work change the map button to start the action in your manifest.
In your Main.java, try to use this code instead:
Button mapBtn = (Button) findViewById(R.id.mapBtn);
mapBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent(Main.this, Map.class));
}
});
I have just started studying Android and I am trying out a simple application to just display a button which when clicked, to show a popup.
My modules below
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Click me!</string>
<string name="button1">Click me!</string>
<string name="popup_text">This is a text</string>
<string name="popup_title">Hi...</string>
</resources>
colors.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="background">#045FB4</color>
</resources>
popup.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/popup_text"
/>
</ScrollView>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#color/background"
android:padding="30dip">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<Button android:text="#string/button1" android:id="#+id/clickme_button"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.googlecode.cowbullgame" android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="8" />
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:debuggable="true">
<activity android:name=".ButtonTest" 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=".Popup" android:label="#string/popup_title"
android:theme="#android:style/Theme.Dialog">
</activity>
</application>
</manifest>
ButtonTest.java
package com.googlecode.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class ButtonTest extends Activity implements OnClickListener{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View clickmeButton = findViewById(R.id.clickme_button);
clickmeButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.clickme_button:
startActivity(new Intent(this, Popup.class));
break;
}
}
}
popup.java
package com.googlecode.test;
import android.app.Activity;
import android.os.Bundle;
public class Popup extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popup);
}
}
While clicking the button, I get an ArrayIndexOutOfBoundsException. I couldn't quite figure out where I am going wrong. Please point it
Thanks.
When getting the button from the xml file, cast the view to Button. Use the following,
Button clickmeButton = (Button) findViewById(R.id.clickme_button);
instead of
View clickmeButton = findViewById(R.id.clickme_button);
And by popup what do you mean. Do you want to show a Dialog? Because your code will just start another activity. to get a popup, use Dialogs. Read about AlertDialog here: http://developer.android.com/guide/topics/ui/dialogs.html .
Button clickmeButton = (Button) findViewById(R.id.clickme_button);
no need of theme for PopUP activity.