Cannot be cast the activity - android

I've got a little problem casting an activity and when I click the item in my menu to open the activity the app crashes and the logcat says that it can be cast the activity. The problem is in this line:
_Sapp = (SecondActivityApp)getApplicationContext();
I think it's the getApplicationContext but I'm not sure. How can I resolve it?

If you are trying to start a new Activity from the menu, you'll have to use an Intent just like you would, if you wanted to start an activity from any other place.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_testdone:
Intent intent = new Intent(getApplicationContext(), SecondActivityApp.class);
startActivity(intent);
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}

Related

Android oncreateOption menu for reload a activity

I have Activity with Action Which have a refresh icon in OnCreateOption Menu.I want to reload my current Activity On click this Icon Click.But I don't know to use here click event.How to resolve this.
Override onOptionsItemSelected method like this way:
#Override
public boolean onOptionsItemSelected(final MenuItem item) {
Intent intent = new Intent();
switch (item.getItemId()) {
case R.id.mnuRefresh: // your menu item id
reCreate();
break;
}
return false;
}
There is a good example here :
https://stackoverflow.com/a/7480103/2724418

Work with android action bar items with layout and without java class

I'm trying to open a dialog (Layout-xml)(without java class !!) with one action bar called about.so, this is my onOptionsItemSelected codes :
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (item.getItemId()) {
case R.id.action_settings:
Toast.makeText(this, "Settings selected", Toast.LENGTH_SHORT).show();
return true;
case R.id.action_about:
LocationFound();
return true;
default:
return super.onOptionsItemSelected(item);
}
}
private void LocationFound() {
Intent i = new Intent(MainActivity.this, aboutdialog);
startActivity(i);
}
then when i want to add the dialog (layout) file in :
Intent i = new Intent(MainActivity.this, aboutdialog);
not allowed to do this.also, i tried this before Intent :
startActivity(new Intent(MainActivity.this, aboutdialog));
the problem is the app cannot find/accept the aboutdialog.
So the question is,
How we can do this, when i clicked to About button in action bar, it show us the aboutdialog ?
Thanks in advance.
Intent it's for moving from one Activity to another Activity.
about dialog
It is not Activity, so this is why it's not working for you.
You should use this method instead
Dialog.show()
It is supposed to be like that:
aboutdialog.show()
You don't need an intent to display a dialog. Change your LocationFound() method to this
private void LocationFound(View view) {
Dialog dialog = new Dialog(YourActivityName.this);
dialog.setContentView(R.layout.dialog_layout);
dialog.show();
}

How can I get Android ActionBar to goto Home when I click upper left?

I am seeing the up caret but nothing happens when I click it. Where do i tell it to goto home and set which activity is Home?
You need the line:
getActionBar().setDisplayHomeAsUpEnabled(true);
put that in onCreate();
That will make it clickable. The you will need to handle the click event. This is done by overriding onOptionsItemSelected()
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
default:
return super.onOptionsItemSelected(item);
}
}
If you want to use NavUtils, (which I have seen used by the ADT plugin when it makes Activities) you can replace change the implementation like so:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
Don't forget to
import android.support.v4.app.NavUtils;
Also, you can add this to your Activity node:
<activity
android:name=".SomeActivity"
android:label="#string/activity_label" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.mypackage.MainActivity" />
</activity>
NavUtils is usually the better approach. For more information, see the official Android Guidelines.
In your onCreate you have:
getActionBar().setDisplayHomeAsUpEnabled(true);
And then use:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
Updated to address comments below, which correctly point out my original answer was not correct for all cases. The "home/up" button have are supposed to take you the Home Activity or the Parent Activity, respectively. From the ActionBar documents --
By default, your application icon appears in the action bar on the
left side. If you'd like, you can enable the icon to behave as an
action item. In response to user action on the icon, your application
should do one of two things:
Go to the application "home" activity, or
Navigate "up" the application's structural hierarchy
Implementing "Home" is straightforward. Launch an intent to the home activity (usually your main launch activity) of your app, clearing the backstack to the existing instance if one exists--
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent intent = new Intent(this, HomeActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
"up" is similar, except you should use
getActionBar().setDisplayHomeAsUpEnabled(true);
in your onCreate to get the icon to show as "up" instead of "home", and launch an instance of the parent activity.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
Intent parentActivityIntent = new Intent(this, MyParentActivity.class);
parentActivityIntent.addFlags(
Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(parentActivityIntent);
finish();
return true;
}
return super.onOptionsItemSelected(item);
}
In both case, for Activities that can be launched from outside your application, you should add FLAG_ACTIVITY_NEW_TASK to launch into a new task instead of the callers task.
"Up" can get more complicated if your Activity can be launched from another app, and want to build a back stack for your application to get back to the root. See for this guide for more info.
My original answer, shown below, is considered bad form in the guide because it it treats the home/up button as 'back'. It will take you to the activity that invoked this activity, which is not necessarily the home or parent. It works well as 'up' for apps that have strict tree hierarchy of activities because an given activity was always launched by its parent.
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return true;
default:
return super.onOptionsItemSelected(item);
}
}

How to open TabActivity

In my app, I want it to go to my main activity when user pushes hardware menu button options. But my main activity is a TabActivity. You will understand it better if you see the code.
The problem is I made a menu item which should start the main activity when user a clicks it. But I get an error when I click on it and forces to close. Can anyone help?
Here is my code:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch (item.getItemId()) {
case R.id.menuhome:
Intent i = new Intent("com.eran.exampleapp.MYMAINACTIVITY");
startActivity(i);
break;
}
return false;
}
and my main activity:
public class MyMainActivity extends TabActivity
First of all... why are you using upper case like this: MYMAINACTIVITY? Isn't your activity called MyMainActivity? Does it make sense for you?
Try this:
Intent i = new Intent(this, MyMainActivity.class);
startActivity(i);
return true;

Remove multiple Activities to go Back to Dashboard from Options menu

I have a Optionsmenu in my Android App, in which is a Button to Go Back to the Apps Dashboad and remove all Activities laying upon that, also removing the History. How is this possible?
thx
Got the Answer:
That's what does the Trick:
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.dashboard:
Intent myIntent = new Intent(this, DashBoard.class);
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(myIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
That's what does the Trick:
myIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

Categories

Resources