Displaying another Activity after clicking a menu item - android

I am writing my first Android app. I have three Activities: BaseballCardList, BaseballCardDetails, and FilterBaseballCards. BaseballCardList loads as the main Activity. It has a menu with two options to display one of the other two activities. BaseballCardDetails loads just fine. However, when I try to load FilterBaseballCards, I get a "Unfortunately, BBCT for Android has stopped." error message. I am running my app in the Android emulator. Here is the relavant code:
res/layout/card_list.xml:
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
res/layout/filter_cards.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:text="#string/filter_by_label" />
<RadioGroup android:id="#+id/filter_by">
<RadioButton android:id="#+id/year"
android:text="#string/year_radio_button"
/>
<RadioButton android:id="#+id/number"
android:text="#string/number_radio_button"
/>
<RadioButton android:id="#+id/year_and_number"
android:text="#string/year_and_number_radio_button"
/>
<RadioButton android:id="#+id/player_name"
android:text="#string/player_name_radio_button"
/>
</RadioGroup>
<Button android:id="#+id/ok"
android:text="#string/ok_button"
/>
<Button android:id="#+id/cancel"
android:text="#string/cancel_button"
/>
</LinearLayout>
BaseballCardList.java:
package bbct.android;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
public class BaseballCardList extends Activity {
private static final String DEBUG_TAG = "BaseballCardList";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_list);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
new MenuInflater(this).inflate(R.menu.option, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.add:
Log.d(DEBUG_TAG, "Starting BaseballCardDetails");
this.startActivity(new Intent(this, BaseballCardDetails.class));
return true;
case R.id.filter:
Log.d(DEBUG_TAG, "Starting FilterBaseballCards");
this.startActivity(new Intent(this, FilterBaseballCards.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
FilterBaseballCards.java:
package bbct.android;
import android.app.Activity;
import android.os.Bundle;
public class FilterBaseballCards extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.filter_cards);
}
}
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="bbct.android"
android:versionCode="1"
android:versionName="1.0">
<application android:label="#string/app_name" android:icon="#drawable/ic_launcher">
<activity android:name=".BaseballCardList"
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=".BaseballCardDetails" />
<activity android:name=".FilterBaseballCards" />
</application>
</manifest>
BaseballCardDetails.java is nearly identical to FilterBaseballCards.java, so I won't post it.
Why am I getting this error message? More importantly, how do I go about tracking down the problem myself? I am using the Android command-line tools, not Eclipse. I started adding some logging messages, but I don't know how to view the logging output that might help me trace the source of the problem.
Update:
I changed the setContentView() in FilterBaseballCards to setContentView(R.layout.card_details); and the card_details view loads just fine. This narrows down the problem to my filter_cards.xml file.
Another Update:
I simplified filter_cards.xml down to:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView android:text="#string/filter_by_label" />
</LinearLayout>
and continue to get the same error as before.

use
adb logcat
to view logging from command prompt
probably you have not added entries to manifest file for your new activities, for more on starting one activity from another look here:
http://developer.android.com/training/basics/firstapp/starting-activity.html

Ok I think your view is right mostly. Except I didn't like this line
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" //Here wrap_content? Shouldn't it be fill_parent?
android:orientation="vertical"
>
Other than that your resources might be messed up (not accessing the right ones / names are wrong) #string/name Or #+id/name
EDIT:
I also noticed you don't assign an ID to the TextView probably should do that ;)

Sounds like you forgot to add the activies you are starting to the manifest.
More on Intent Filters: http://developer.android.com/guide/components/intents-filters.html

As #Nate noted, and I verified, the problem was in the view for my FilterBaseballCards activity. In particular, I modified the filter_cards.xml layout to
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/filter_by_label"
/>
</LinearLayout>
Note that I added android:layout_width and android:layout_height attributes to the TextView tag. After doing this, the app compiles and runs correctly in the Android emulator.

Related

Pass data between two activities in android

I made an android app which passes data between two activities but an error occurs in my code that I can't discover.
First "activity_main.xml" executes, and when the user presses a button, a second activity "activity_main1.xml" starts. But when the button is pressed, an error occurs and app stops.
This is the MainActivity.java:
package com.example.measureregistertool;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void sendMessage(View view) {
Intent intent = new Intent(MainActivity.this, MainActivity1.class);
startActivity(intent);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
This is the activity_main.xml:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="90dp"
android:onClick="sendMessage"
android:text="Take Photo" />
</RelativeLayout>
This is the activity_main1.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" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Activity" />
</LinearLayout>
This is the manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.measureregistertool"
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.example.measureregistertool.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>
My code error
Your second activity is missing in the manifest file. You probably created it just by creating new java class, not by creator from right click.
Add it to the manifest below your activity tag
<activity
android:name="com.example.measureregistertool.MainActivity1">
</activity>
You have two activities in your package,But have only declared one in manifest.
Declare the other Activity class.
You need to add the second Activity (MainActivity1) in your AndroidManifest.xml
<activity
android:name="com.example.measureregistertool.MainActivity1"
android:label="#string/app_name" >
</activity>
Always create activity by File->new->Activity -> (Select Category).
This process performs three operations
1. Create Activity java file.
2. Create Layout xml file.
3. Register your Activity class in Manifest file.
So you can be safe from such problems.

What is causing my app to crash on button press?

My manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.rufflez.pagertabstriplistview.pagertabstriplistview" >
<application
android:allowBackup="true"
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.rufflez.pagertabstriplistview.pagertabstriplistview.FirstScreen"
android:label="#string/app_name"
android:theme="#style/AppTheme.Base">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
On the Launch screen (FirstScreen):
<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:background="#drawable/bg">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_logo"
android:src="#drawable/logo2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:visibility="invisible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img_title"
android:layout_centerVertical="false"
android:layout_centerHorizontal="true"
android:layout_marginTop="230dp"
/>
<TextView
android:visibility="visible"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/desc"
android:id="#+id/description"
android:textColor="#ffffffff"
android:textSize="24dp"
android:typeface="sans"
android:gravity="center"
android:layout_marginTop="375dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:textStyle="italic" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_start"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/start"
android:background="#null"
android:layout_marginBottom="16dp" />
</RelativeLayout>
My FirstScreen.java:
package com.rufflez.pagertabstriplistview.pagertabstriplistview;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
public class FirstScreen extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_screen);
setupMessageButton();
}
private void setupMessageButton() {
//1. Get reference to button
ImageButton messageButton = (ImageButton) findViewById(R.id.btn_start);
//2. Set the click listener to run code.
messageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("DemoButtonApp", "Clicked Begin");
startActivity(new Intent(FirstScreen.this, MainActivity.class));
finish();
}
});
}
}
When I tap the ImageButton (btn_start) It crashes and says "Unfortunately, Video Game Historium has stopped" It doesn't seem to want to load the next screen. I looked in Logcat (which keeps going and going and never stopping) it's spitting out a few red errors:
EnterpriseContainerManager﹕ ContainerPolicy Service is not yet ready!!!
E/SMD﹕ DCD ON
CameraController﹕ handleMessage signal: 0
CameraController﹕ handleMessage(0) before let go!{ when=-506ms what=0 target=com.sec.android.smartface.CameraController$EventHandler }
just to name a few. Any ideas? It was working until I took the screen from one project and imported it into this one. I imported the xml, java and drawables.
Edit: Added Crash Report:
11-18 10:04:47.683 9690-9690/com.rufflez.pagertabstriplistview.pagertabstriplistview
E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rufflez.pagertabstriplistview.pagertabstriplistview, PID: 9690
java.lang.RuntimeException: Unable to start activity
ComponentInfo{com.rufflez.pagertabstriplistview.pagertabstriplistview/com.rufflez.pagertabstriplistview.pagertabstriplistview.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to false in your theme
to use a Toolbar instead
This is the important part here. So, check your themes.xml and set `
<item name="windowActionBar">false</item>
Alternatively, you should be able to use as parent
parent="Theme.AppCompat.Light.NoActionBar"
and it should work.
Solved my issue. I had to change the order of my themes.xml around a little and add
<item name="windowActionBar">false</item>

Android layout xml file not running

Below is a code for layout main.xml file
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button" />
</LinearLayout>
Here is the activity file
package alvisoft.helloworld;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
but when I run it on eclipse with android SDK it shows blank screen instead of showing button and textview.I am absolute beginner and don't know what's the problem Kindly Help me!
I also make mistakes like you. You can fix this error as follows:
- On the tab Graphic_layout you drag and drop layout LinearLayout(on the left) into the interfaces.
- Then on the tab main.xml You created newly code into this LinearLayout. This error has been fixed.
i think you forgot to add your activity in AndroidManifest.xml.
update your manifest file like this.
<?xml version="1.0" encoding="utf-8"?>
<manifest package="app.sample" android:versionCode="1"
android:versionName="1.0" xmlns:android="http://schemas.android.com/apk/res/android">
<application android:icon="#drawable/icon" android:label="#string/app_name"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen">
<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>
</manifest>
i tried your code. it is working fine. but the R.menu.main file was missing. i removed the options menu and tried

Starting activities with intents in Android - why does this work?

Previously I've been looking at existing code to display a map in a tab, and this time I've tried to write it myself to see if I understood it.
It runs fine without errors (screenshot), but I don't completely understand why, and whether I'm doing this in the best way.
The part that I don't quite understand is in the manifest.
I would have thought that android:label should be maptabview_name, but that gives me an error saying no matching resource was found.
Why does it run when using app_name for that activity?
Why can't it find the maptabview_name resource?
Also, in MapTab2, is this the best way to start an intent?
What exactly is this telling the system? "I intend to start an activity from this class"?
Here's my code
(It's based on this):
MapTab2.java
package com.test.maptab2;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;
public class MapTab2 extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i = new Intent(this,MapTabView.class);
TabHost.TabSpec spec;
spec = getTabHost().newTabSpec("tab1");
spec.setContent(i);
spec.setIndicator("Map");
getTabHost().addTab(spec);
spec = getTabHost().newTabSpec("tab2");
spec.setContent(R.id.detailstub);
spec.setIndicator("Detail");
getTabHost().addTab(spec);
getTabHost().setCurrentTab(0);
}
}
MapTabView.java
package com.test.maptab2;
import android.os.Bundle;
import com.google.android.maps.MapActivity;
public class MapTabView extends MapActivity {
#Override
public void onCreate (Bundle icicle){
super.onCreate(icicle);
setContentView(R.layout.maptabview);
}
#Override
public boolean isRouteDisplayed(){
return false;
}
}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<!-- Tab-switch panel -->
<TabWidget android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<!-- Tab contents -->
<FrameLayout android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Map here -->
<RelativeLayout android:id="#+id/mapstub"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
<!-- Other stuff -->
<TextView android:id="#+id/detailstub"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="detail"/>
</FrameLayout>
</LinearLayout>
</TabHost>
maptabview.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/maptablayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.android.maps.MapView android:id="#+id/mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
android:apiKey="0HRMcD5o6WrBVhmwbWpeyeavZ67PXWOvJeeCx2g"/>
</RelativeLayout>
MapTab2.Manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test.maptab2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3" />
<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" />
<!-- Main -->
<activity android:name=".MapTab2"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<!-- Map -->
<activity android:name=".MapTabView"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.EMBED" />
</intent-filter>
</activity>
</application>
</manifest>
Q1: maptabview_name has to be in a string resource file, usually at res/values/string.xml, in case you have it there and still it's not in the resources generated file (gen/package/R.java), try removing it and let eclipse generate it again.
Q2: AFAIK, TabHost.TabSpec.setContent(Intent intent) it's like starting the Activity within the specified tab. I think that's the way do do it when you deal with tabs. Since Intents are used to do a lot of things, this is a basic usage, in this case just to specify the class.

A new button to Android screen

How can I display a button on screen? I have defined it as
final Button nappi = (Button) findViewById(R.id.soita);
and
<Button
android:layout_width="100px"
android:layout_height="wrap_content"
android:text="#+string/Soita"
android:id="#+id/soita"
/>
You need to create the layout of the view in which you want the button to appear. Even something like
Let's say you're storing the definition in a file called main.xml in your res/layout directory.
<?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"
>
<Button android:text="Button01" android:id="#+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Then within your activity, you need to do the following:
public class Main extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Replace this with the name of your xml file dictating layout
setContentView(R.layout.main);
}
}
See the Hello Views tutorial; very useful. http://developer.android.com/guide/tutorials/views/index.html
If you're asking something different, like programmatically adding a button to an existing view, I'm not sure about that. In that case you might want to have the button start off as hidden and then reveal it when you need it.
I don't see why this displays nothing. I tried to make my own simple user interface.
main:
package com.xyz;
import android.app.Activity;
import android.os.Bundle;
public class NewHomeScreen extends Activity {
/** Called when the activity is first created. */
#Override public void onCreate(Bundle state) {
super.onCreate(state);
setContentView(R.layout.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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello"
>
</TextView>
<Button android:text="Soita"
android:id="#+id/soita"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
</Button>
</LinearLayout>
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.xyz"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="#drawable/icon" android:label="#string/app_name">
<activity android:name="NewHomeScreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.HOME"/>
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Your textview gets all screen place...
try this
XML:
<TextView
android:text="#string/hello"
android:layout_width="match_parent" android:layout_height="291dp">
</TextView>

Categories

Resources