Android - View has no ID - why? - java

I have a class that is supposed to call another activity depending on which button has been clicked. This is done by checking the available IDs.
My problem is: The view that is passed to my class has no id, or rather, its value is NO_ID. What puzzles me is that the view, which is a button, does have an ID.
public class StrengthOrganizer extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_strength_organizer);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_strength_organizer, menu);
return true;
}
public void notifyActivity(View view) {
Intent intent = null;
int id = view.getId();
switch(id){
case R.id.button_log_book_launcher:
intent = new Intent(this, LogBook.class);
break;
case R.id.button_programming_launcher:
intent = new Intent(this, Programming.class);
break;
case R.id.button_visualizer_launcher:
intent = new Intent(this, Visualizer.class);
break;
default:
return;
}
startActivity(intent);
}
}
The corresponding XML file looks like this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button android:name="#+id/button_log_book_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_log_book_launcher"
android:onClick="notifyActivity" />
<Button android:name="#+id/button_programming_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_programming_launcher"
android:onClick="notifyActivity" />
<Button android:name="#+id/button_visualizer_launcher"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:text="#string/button_visualizer_launcher"
android:onClick="notifyActivity" />
</LinearLayout>
Why does the view object have no ID if it's been give in the XML file?

You used android:name instead of android:id in your XML to assign the ids. Use the latter instead.

Related

NavigationView Menu not respond on Click Listener [duplicate]

I'm developing an application (as my first one) and In this app I've declared my navigationView like this:
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/navigation_drawer"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:menu="#menu/drawer_menu"
app:headerLayout="#layout/drawer_header">
In my mainactivity.java I have:
public class MainActivity extends AppCompatActivity implements
NavigationView.OnNavigationItemSelectedListener{
...
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
Log.d("Navigation","Item selected : "+ item.getItemId());
int id = item.getItemId();
switch (id){
case R.id.about:
Toast.makeText(getApplicationContext(),"About Us
elected",Toast.LENGTH_SHORT).show();
break;
case R.id.help:
Toast.makeText(getApplicationContext(),
"Help",Toast.LENGTH_SHORT).show();
break;
case R.id.setting:
Toast.makeText(getApplicationContext(),
"Settings",Toast.LENGTH_SHORT).show();
break;
case R.id.EditProfile:{
Intent intent= new Intent(this , EditProfile.class);
startActivity(intent);
}
default:
break;
}
DrawerLayout drawerLayout= (DrawerLayout)
findViewById(R.id.drawer_layout);
drawerLayout.closeDrawer(GravityCompat.START);
return false;
}
And according to this I've extended ActionBarDrawerToggle and in it's onDrawerOpened function I've don
drawerView.bringToFront();
drawerView.getParent().requestLayout();
But still when I click on navigation view Items onNavigationItemSelected function doesn't fire.
Is it because of header view I have added??
This method worked before I add header view to navigationView( I didn't test it just before adding header to navigationView.
And also I think it's useful to note that I have some buttons and TextViews in navigationView header.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:id="#+id/drawer_header"
android:background="#drawable/drawer_bg"
android:layout_width="match_parent"
android:padding="4dp"
android:layout_height="200dp">
<RelativeLayout
android:gravity="center_vertical"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_marginTop="50dp"
android:layout_gravity="center"
android:layout_height="wrap_content">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/avatar"
android:id="#+id/AvatarImageView"
android:layout_centerHorizontal="true" />
<Button
android:text="Signup"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentRight="true"
android:id="#+id/header_signup"
android:textSize="12sp"
android:background="#drawable/signup_button"
android:textStyle="normal|bold" />
</RelativeLayout>
<TextView
android:textColor="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_height="wrap_content"
android:id="#+id/header_username"
android:textAlignment="center" />
<TextView
android:text="Logout"
android:textColor="#color/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:layout_height="wrap_content"
android:id="#+id/header_logout"
/>
</LinearLayout>
You should add following line of code in onCreate for click listener for menu item,
NavigationView navigationView = (NavigationView) findViewById(R.id. navigation_drawer);
navigationView.setNavigationItemSelectedListener(this);
You can set click listener to the header of the NavigationView like this:
View headerLayout = navigationView.getHeaderView(0);
headerLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent= new Intent(this , EditProfile.class);
startActivity(intent);
});
and for the elements inside the header
CircleImageView userPicture = (CircleImageView) headerLayout.findViewById(R.id.AvatarImageView);
Edit
navigationView.setNavigationItemSelectedListener(this);
This is the solution:
path = Environment.getExternalStorageDirectory().getAbsolutePath()+"/WhatsApp/Media/.Statuses/";
File f= new File(path);

ActionBar Item Click listner is not working

I tried Action Bar like this
I want like when i click on Actionbar's Item it will print a Toast but it is not working tried a lot.
I Created 2 custom layout,one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/noti"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginTop="3dp"
android:layout_marginLeft="15dp"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</RelativeLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:src="#drawable/task"
android:id="#+id/imageView5" />
<TextView
android:id="#+id/cart_badge1"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginLeft="15dp"
android:layout_gravity="right|end|top"
android:background="#drawable/badge_circle"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"
android:layout_alignTop="#+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
navigation.menu
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:apps="http://schemas.android.com/tools">
<item
android:id="#+id/action_refresh"
android:icon="#drawable/noti"
app:showAsAction="always"
android:actionLayout="#layout/custom_action_notification_layout"
android:title="Refresh"/>
<item
android:id="#+id/action_task"
android:orderInCategory="100"
android:actionLayout="#layout/custom_action_task_layout"
android:title="Task"
android:icon="#drawable/task"
app:showAsAction="always" /></menu>
Now in my navigationdrawer.class i put thiscode onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
navigation.class
#Override
public boolean onCreateOptionsMenu(final Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.navigation, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_refresh);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
// RelativeLayout notifCount = (RelativeLayout) MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
final MenuItem itemNotification1 = menu.findItem(R.id.action_task);
MenuItemCompat.setActionView(itemNotification1, R.layout.custom_action_task_layout);
View actionViewNotification1 = MenuItemCompat.getActionView(itemNotification1);
// RelativeLayout notifCount1 = (RelativeLayout) MenuItemCompat.getActionView(itemNotification1);
actionViewNotification1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification1);
}
});
Now finally in onOptionsItemSelected() the code is like
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_refresh: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
Tried in many ways but not working at all please help out from this..
1. Create two custom layout, one for Notification custom item and another for Task custom item.
custom_action_notification_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_notification"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
custom_action_task_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
style="?attr/actionButtonStyle"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:focusable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/icon_task"/>
<TextView
android:id="#+id/cart_badge"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|end|top"
android:layout_marginEnd="-5dp"
android:layout_marginRight="-5dp"
android:layout_marginTop="3dp"
android:background="#drawable/badge_background"
android:gravity="center"
android:padding="3dp"
android:textColor="#android:color/white"
android:text="0"
android:textSize="10sp"/>
</FrameLayout>
2. Create a menu XML containing Notification and Task item. Use attribute app:actionLayout to set custom layout to each item.
custom_menu.xml
<menu
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto" >
<item
android:id="#+id/action_notification"
android:icon="#drawable/icon_notification"
android:title="Notification"
app:actionLayout="#layout/custom_action_notification_layout"
app:showAsAction="always"/>
<item
android:id="#+id/action_task"
android:icon="#drawable/icon_task"
android:title="Task"
app:actionLayout="#layout/custom_action_task_layout"
app:showAsAction="always"/>
</menu>
3. In your Activity onCreateOptionsMenu(), get ActionView of Notification and Task items and Set OnClick listeners to those ActionView.
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.custom_menu, menu);
// Notification
final MenuItem itemNotification = menu.findItem(R.id.action_notification);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
actionViewNotification.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemNotification);
}
});
// Task
final MenuItem itemTask = menu.findItem(R.id.action_task);
View actionViewTask = MenuItemCompat.getActionView(itemTask);
actionViewTask.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onOptionsItemSelected(itemTask);
}
});
return true;
}
4. Finally, In onOptionsItemSelected() do the rest:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_notification: {
// Do something
Toast toast = Toast.makeText(this, "Notification clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
case R.id.action_task: {
// Do something
Toast toast = Toast.makeText(this, "Task clicked", Toast.LENGTH_SHORT);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
return true;
}
}
return super.onOptionsItemSelected(item);
}
FYI, I have checked Notification and Task menu item click event by showing Toast messages.
OUTPUT:
UPDATE:
In your updated code use:
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
Instead of
View actionViewNotification = MenuItemCompat.getActionView(itemNotification);
MenuItemCompat.setActionView(itemNotification, R.layout.custom_action_notification_layout);
Hope this will help~

How to connect between xml file and class?

I tried to call to a function that places in activity file. My xml file, trying to find this method in other activity file. How can I connect between both?
This is my activity file:
public class FirstScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bmi);
//Intent newActivity = new Intent(this, MainActivity.class);
//startActivity(newActivity);
}
public void gotomain(){
Intent newActivity = new Intent(this, MainActivity.class);
startActivity(newActivity);
}
}
This is my xml file (button onClick - search method in other class - what do I do here?):
<?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">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick=""/>
</LinearLayout>
Thanks a lot!
// Try this
Java Code
public void gotomain(View view){
Intent newActivity = new Intent(this, MainActivity.class);
startActivity(newActivity);
}
xml code.
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal"
android:onClick="gotomain"/>

Android Multiple Intents - One Form

Good afternoon,
I am trying to create a basic menu in my andorid application which contains 5 buttons each bringing you to another form. I am trying to create the java to carry out this action but appear to be running into the following error with each of my buttons
"EXAMPLE cannot be resolved as a variable"
Please help me in a solution to my code or if there is a simpler way to allow me to execute this menu with 5 buttons each going to a different form
<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="com.techblogon.loginexample.MainMenu" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/pic" />
<Button
android:id="#+id/btnFootball"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="btnFootball" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="btnHockey" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="btnLacrosse" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="btnCurling" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="btnLogout" />
</RelativeLayout>
Here is the Java:
package com.techblogon.loginexample;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.content.Context;
import android.view.Menu;
import android.view.MenuItem;
public class MainMenu extends Activity {
public void ButtonOnClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
startActivity(Football);
break;
case R.id.btnHockey:
startActivity(Hockey);
break;
case R.id.btnLacrosse:
startActivity(Lacrosse);
break;
case R.id.btnCurling:
startActivity(Curling);
break;
case R.id.btnLogout:
startActivity(HomeActivity);
break;
}
}
}
#Meryl2
When u are using
""android:onClick="btnLogout"""
Then you should have corresponding method
public void btnLogout(View view{
//your code here
}
Same applies for all buttons in your code
Your Mistakes ->
You have not used setcontentView() nor you overridden onCreate() method. One more thing, you are not sending intent correctly.
Here is how you can send intent
Intent intent = new Intent(getApplicationContext(), DestinationActivity.class);
startActivity(intent);
I have modified the class and the layout. You need to create the other classes for each action.
public class MainActivity extends Activity implements Button.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
int id = v.getId();
Intent intent = null;
if(id == R.id.btnHockey) {
intent = new Intent(MainActivity.this, Hockey.class);
} else if(id == R.id.btnCurling) {
intent = new Intent(MainActivity.this, Curling.class);
} else if(id == R.id.btnFootball) {
intent = new Intent(MainActivity.this, Football.class);
} else if(id == R.id.btnLogout) {
intent = new Intent(MainActivity.this, Logout.class);
} else if(id == R.id.btnLacrosse) {
intent = new Intent(MainActivity.this, Lacrosse.class);
}
startActivity(intent);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<Button
android:id="#+id/btnFootball"
android:layout_below="#id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Football"
android:onClick="onClick" />
<Button
android:id="#+id/btnHockey"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnFootball"
android:layout_marginTop="25dp"
android:text="Hockey"
android:onClick="onClick" />
<Button
android:id="#+id/btnLacrosse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnHockey"
android:layout_marginTop="25dp"
android:text="Lacrosse"
android:onClick="onClick" />
<Button
android:id="#+id/btnCurling"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnLacrosse"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Curling"
android:onClick="onClick" />
<Button
android:id="#+id/btnLogout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btnCurling"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="Logout"
android:onClick="onClick" />
I think i got your problem. In each Button you set the name of the method that will be used when you click on it :
android:onClick="btnCurling"
android:onClick="btnHockey"
...
But you only have one method here which is :
public void ButtonOnClick(View v) {
...
}
One solution is to define each method like so :
public class MainMenu extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
}
// it will get there when you click on the "#+id/btnHockey" Button
public void btnHockey(View v) {
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
}
// then you add the other ones btnCurling, ...
}
Another solution is to set the Listener of each button programmatically :
public class MainMenu extends Activity implements View.OnClickListener {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Here you tell your activity what layout he will display
setContentView(R.layout.TheNameOfYourLayout);
// This will find your button in the layout you just set
// and then set this Class as your Listener
findViewById(R.id.btnFootball).setOnClickListener(this);
// Then set the listener of all your other buttons
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnFootball:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
case R.id.btnHockey:
Intent intent = new Intent(this, NameOfTheActivityToLaunch.class);
startActivity(intent);
break;
// ...
}
}
}
You also have to remove all the android:onClick="btnCurling" line in your XML Layout file for this solution to work.
Hope it helps cheers :)
Try this code;
public class MainMenu extends Activity implements OnClickListener {
Button btn_football,btn_hokey,btn_something; // add another button here
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
btn_football = (Button)findViewById(R.id.btnFootball);
// add another button here same way
btn_football.setOnClickListener(this);
// add another button here same way
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.btnFootball:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
case R.id.another button:
Intent intent = new Intent(MainMenu.this,(nextclass_addhere).class);
startActivity(intent);
break;
//.......add here another button with same way
}
}
}

Why do I get an error in this ListView creation?

I'm trying to develop an ListView in Android that contains on Customer Name & Address and this list view is in a TabWidget. However, whenever I click that tab to see customer list info, application breaks. Following is my code:
The exception would be:
exception IllegalStateException (id=830019169944)
cause IllegalStateException (id=830019169944) detailMessage "Did
you forget to call 'public void setup(LocalActivityManager
activityGroup)'?" (id=830019169976) stackState (id=830019170216)
stackTrace null suppressedExceptions ArrayList (id=830019170192)
XML page
<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="fill_parent">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:tag="tab0"
android:text="MainMenu"
android:background="#android:drawable/btn_star_big_on"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
<TextView
android:tag="tab1"
android:text="Customers"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
/>
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
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" >
<TextView
android:id="#+id/lblmainmenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Menu"
android:textSize="20sp" />
<ImageButton
android:id="#+id/btnStock"
android:layout_marginTop="20dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_alignLeft="#+id/lblmainmenu"
android:layout_below="#+id/lblmainmenu"
android:onClick="btn_clickstock"
android:scaleType="fitCenter"
android:src="#drawable/dailystock" />
<ImageButton
android:id="#+id/btnDailySummury"
android:layout_marginTop="20dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_marginLeft="40dp"
android:layout_below="#+id/lblmainmenu"
android:layout_toRightOf="#+id/btnStock"
android:onClick="btn_clickDailySummury"
android:scaleType="fitCenter"
android:src="#drawable/dailystock" />
</RelativeLayout>
<ListView
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" >
</ListView>
</FrameLayout>
</LinearLayout>
</TabHost>
JAVA Main Activity code
public class MainMenuActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
Intent intent = new Intent(this, Customer_List_Activity.class);
TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost);
tabHost.setup();
final TabWidget tabWidget = tabHost.getTabWidget();
final FrameLayout tabContent = tabHost.getTabContentView();
// Get the original tab textviews and remove them from the viewgroup.
TextView[] originalTextViews = new TextView[tabWidget.getTabCount()];
for (int index = 0; index < tabWidget.getTabCount(); index++) {
originalTextViews[index] = (TextView) tabWidget.getChildTabViewAt(index);
}
tabWidget.removeAllViews();
// Ensure that all tab content childs are not visible at startup.
for (int index = 0; index < tabContent.getChildCount(); index++) {
tabContent.getChildAt(index).setVisibility(View.GONE);
}
// Create the tabspec based on the textview childs in the xml file.
// Or create simple tabspec instances in any other way...
for (int index = 0; index < originalTextViews.length; index++) {
final TextView tabWidgetTextView = originalTextViews[index];
final View tabContentView = tabContent.getChildAt(index);
TabSpec tabSpec = tabHost.newTabSpec((String) tabWidgetTextView.getTag());
if (index == 0)
{
tabSpec.setContent(new TabContentFactory() {
#Override
public View createTabContent(String tag) {
return tabContentView;
}
});
}
else
tabSpec.setContent(intent);
if (tabWidgetTextView.getBackground() == null) {
tabSpec.setIndicator(tabWidgetTextView.getText());
} else {
tabSpec.setIndicator(tabWidgetTextView.getText(), tabWidgetTextView.getBackground());
}
tabHost.addTab(tabSpec);
}
}
#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, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
ListView Activity Code XML Page
<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:id="#+id/text1"
android:textSize="14dp"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#BFFF00"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
ListView Activity Code Java
public class Customer_List_Activity extends ListActivity {
private SimpleAdapter sa;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
HashMap<String,String> item;
for(int i=0;i<HardwareShops.length;i++){
item = new HashMap<String,String>();
item.put( "line1", HardwareShops[i][0]);
item.put( "line2", HardwareShops[i][1]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.list_for_customer ,
new String[] { "line1","line2" },
new int[] {android.R.id.text1, android.R.id.text2});
setListAdapter( sa );
}
private String[][] HardwareShops =
{{"Jayasekara","Colombo 03"},
{"Chandana","Colombo 03"},
{"Ruban","Borella"},
{"Safras","Colombo 05"},
{"Harris","Rajagiriya"},
{"HJ","Nawala"},
{"Himali","Nugegoda"},
{"Nilawala","Colombo 13"},
{"Jayantha","Colombo 09"}};
}
What would be the problem? Is there any missing code line?
you need to change MainMenuActivity's base class from Activity to ActivityGroup, as follows:
public class MainMenuActivity extends ActivityGroup {
...
}
ActivityGroup will take care of an instance of LocalActivityManager. So you don't need to create it. After the base class is changed, just call getLocalActivityManager() function defined in the base class to get that instance. Call tabHost's setup function like this:
tabHost.setup(this.getLocalActivityManager());

Categories

Resources