startactivity crashed on execute - android

I have setup an activity to be executed from a menu button. The activity is started and briefly appears and then crashes. I have added added activity to manifest file. Code poseted below. I have recently switched form Eclipse to Android Studio and still learning the changes.
<?xml version="1.0" encoding="utf-8"?>
<uses-permission android:name="android.permission.BLUETOOTH" />
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<activity
android:name=".Titles_Edit_Activity"
android:label="#string/title_activity_titles__edit_"
android:theme="#style/Theme.AppCompat">
</activity>
</application>
This is the logcat message:
08-13 11:13:45.841 15302-15302/com.example.jerry.els2015 D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002
Menu trigger in MainActivity
public void setup(MenuItem menuItem){
Log.d("TAG", "Setting ");
startActivity(new Intent(this,Titles_Edit_Activity.class));
}
XML for Tiles_Edit_Activity
package com.example.jerry.els2015;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class Titles_Edit_Activity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_titles__edit_);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_titles__edit_, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I found the issue on the activity. I setup menu and back button to finish and exit the system so bluetooth will be killed. I removed these option and was able to execute the new activity.
#Override
protected void onDestroy() {
super.onDestroy();
// finish();
// System.exit(0);
}
#Override
protected void onStop() {
super.onStop();
// finish();
// System.exit(0);
}

try other way for startactivity, ex;
Intent intent = new Intent(MainActivity.this, Titles_Edit_Activity.class);
startActivity(intent);

I found the issue on the activity. I setup menu and back button to finish and exit the system so bluetooth will be killed. I removed these option and was able to execute the new activity.
#Override
protected void onDestroy() {
super.onDestroy();
finish();
System.exit(0);
}
#Override
protected void onStop() {
super.onStop();
// finish();
// System.exit(0);
}

Related

How to go back to previous activity

I have a problem with the lifecycle of the activities on android. I am trying to return back from my second activity to my first one. It actually my code is doing that, not the way it supposed to do it. I have "back button" on the top left corner and when I hit it - it goes through onDestroy() and onCreate() methods of my MainActivity. But if I use the back button on my phone - everything is OK(it goes just trough onResume() method) Can someone explain me - where my code is wrong?
in MainActivity.java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.settings_menu, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
in my SettingsActivity.java:
package com.example.garagedoor;
import android.os.Bundle;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.preference.PreferenceFragmentCompat;
public class SettingsActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_activity);
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.settings, new SettingsFragment())
.commit();
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayHomeAsUpEnabled(true);
}
}
public static class SettingsFragment extends PreferenceFragmentCompat {
#Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
setPreferencesFromResource(R.xml.root_preferences, rootKey);
}
}
}
in AndroidManifest.xml:
<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=".SettingsActivity"
android:label="#string/title_activity_settings"
android:parentActivityName=".MainActivity">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity"/>
</activity>
You need to add
android:launchMode="singleTop"
to the manifest entry for MainActivity. See the following questions:
ActionBar up navigation recreates parent activity instead of onResume
How can I return to a parent activity correctly?
Your problem is probably about the task stack. I am not sure what actionbar button do but i think you should either use
#Override public void onBackPressed()
in SettingsActivity or start the SettingsActivity with Intent flags like here:
https://developer.android.com/guide/components/activities/tasks-and-back-stack
Hope this helps.

Search dialog in android is not showing

I am working on an Android application where I want to implement a search dialog.
Despite following all the steps in http://developer.android.com/guide/topics/search/search-dialog.html
On clicking the search icon no search dialog appears. The code is as follows:
Android manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a1405232.rateit2">
<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=".HomePage"
android:label="#string/title_activity_home_page"
android:theme="#style/AppTheme.NoActionBar"></activity>
<activity android:name=".SearchableActivity" >
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</application>
HomePage.java
package com.example.a1405232.rateit2;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
public class HomePage extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
Intent i1 = getIntent();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.home_page, 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();
//noinspection SimplifiableIfStatement
switch(id)
{
case R.id.action_settings:
return true;
case R.id.action_search:
onSearchRequested();
// Toast.makeText(this, "This is my Toast message!",
// Toast.LENGTH_SHORT).show();
return true;
default:
// If we got here, the user's action was not recognized.
// Invoke the superclass to handle it.
return super.onOptionsItemSelected(item);
}
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
SearchableActivity.java
package com.example.a1405232.rateit2;
import android.app.ListActivity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
public class SearchableActivity extends ListActivity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent(getIntent());
}
public void onNewIntent(Intent intent) {
setIntent(intent);
handleIntent(intent);
}
public void onListItemClick(ListView l,View v, int position, long id) {
// call detail activity for clicked entry
}
private void handleIntent(Intent intent) {
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query =intent.getStringExtra(SearchManager.QUERY);
doSearch(query);
}
}
private void doSearch(String queryStr) {
// get a Cursor, prepare the ListAdapter and set it
}
}
There's a searchable.xml in res/xml
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
What am I missing out on?
I have not yet implemented query yet. Just want the SearchableActivity to
open so that I can see the search dialog.
I saw this answer and added an external meta tag but no response Search in Android
This is how the screen looks. No search box appears on clicking the search icon.
following is the basic code for a working search dialog example.
First of all u have to define a searchable.xml file in the xml resource folder:
<?xml version="1.0" encoding="utf-8"?>
<searchable xmlns:android="http://schemas.android.com/apk/res/android"
android:label="#string/app_name"
android:hint="#string/search_hint" >
</searchable>
Please be careful, for the label and hint values are mandatory they point to string resources, this is a point that should be highlighted more in the android documentation and still is the source of so many issues.
Second, you have to change a bit your AndroidManifest.xml, let's suppose for sake of simplicity that you just have one activity MainActivity, your manifest at the end should be like this:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.paolomoschini.searchdialogtest">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
<!-- this is the searchable activity; it performs searches.
Notice that this provides the search dialog by default -->
<intent-filter>
<action android:name="android.intent.action.SEARCH" />
</intent-filter>
<meta-data android:name="android.app.searchable"
android:resource="#xml/searchable"/>
</activity>
</application>
</manifest>
The last step is to change the MainActivity.class for show the search dialog and receive the search query.
Let's suppose you have a menu item with the following items declared:
<menu 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"
xmlns:appcompat="http://schemas.android.com/apk/res-auto"
tools:context="com.paolomoschini.searchdialogtest.MainActivity">
<item
android:id="#+id/action_search"
android:icon="#drawable/ic_search"
android:title="Search"
appcompat:showAsAction="always"/>
<item
android:id="#+id/action_settings"
android:orderInCategory="100"
android:title="#string/action_settings"
app:showAsAction="never"/>
</menu>
The MainActivity.class should trigger the following when user press on the search icon
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
if (id == R.id.action_search) {
//this is the method that displays the search dialog
onSearchRequested();
}
return super.onOptionsItemSelected(item);
}
MainActivity.class receive the search text throught an intent, in your onCreate method add the following:
// Get the intent, verify the action and get the query
Intent intent = getIntent();
if (Intent.ACTION_SEARCH.equals(intent.getAction())) {
String query = intent.getStringExtra(SearchManager.QUERY);
Log.v("QUERY", query);
}

my app not works error comes on intent

i am new in this field
sorry for my bad english
i want to make an app that perform to open second layout and in 2nd layout i want to open first layout
but an error come on my intent kindly guide me
package com.example.ahmed.calling;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button= (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.my_layout2);
Intent intent = new Intent(getApplicationContext(),second.class);
startActivity(intent);
finish();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
my second class is
public class second extends MainActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_layout2);
Button btn1 =(Button)findViewById(R.id.button2);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),MainActivity.class);
startActivity(intent);
finish();
}
});
}
}
error come on clicking the button it appers on first activity
on line 25 i-e startactivity(intent) kindly guide me
my logcat error
10-15 00:32:14.968 26752-26752/? E/ConnectionService﹕ Failed to connect to GoogleApiClient: ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
10-15 00:32:16.296 26939-26939/com.example.ahmed.calling E/AndroidRuntime﹕ FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.ahmed.calling/com.example.ahmed.calling.second}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1541)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1416)
at android.app.Activity.startActivityForResult(Activity.java:3389)
at android.app.Activity.startActivityForResult(Activity.java:3350)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:748)
at android.app.Activity.startActivity(Activity.java:3562)
at android.app.Activity.startActivity(Activity.java:3528)
at com.example.ahmed.calling.MainActivity$1.onClick(MainActivity.java:25)
at android.view.View.performClick(View.java:4106)
at android.view.View$PerformClick.run(View.java:17052)
at android.os.Handler.handleCallback(Handler.java:615)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5059)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
manifest file is
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ahmed.calling" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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've spotted three logical errors/issues in your codes, however for being certain outputs of logcat are needed.
In your MainActivity class you don't need to change view hierarchy in that onClick callback. i.e. Remove setContentView(R.layout.my_layout2); in that method.
You shouldn't use getApplicationContext() inside an Activity to start another Activity. You should use the reference to current Activity instead.
Intent intent = new Intent(MainActivity.this, second.class);
startActivity(intent);
Also, there's no need to finish your Activities while you're starting another ones. They will be handled by the Android itself.
UPDATE #1:
Your logcat says:
Unable to find explicit activity class {com.example.ahmed.calling/com.example.ahmed.calling.second}; have you declared this activity in your AndroidManifest.xml?
This means that you should first declare your activities in the AndroidManifest.xml, then you can start them.
UPDATE #2:
Your final manifest file should look like this:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<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>
<!-- ADD THE FOLLOWING LINE -->
<activity android:name=".second"/>
</application>
As I suspected, your second Activity is not declared in the manifest. You need to add:
<activity android:name=".second"/>

Activity in android not starting as intended

The scenario I have consists of 3 activities:
1- Homepage
2-Login
3-Register
The thing is now Home page has 2 buttons one login and one for homepage , and the login activity should be called from either this button or from inside register after I am done registering ,also both login and register activities inflate 2 different fragments.
The problem : Login activity doesnt start , instead it sometimes just blinks with the layout it should inflate for a split second and then backs to the previous activity whichever one it is , also when I replaced the login activity with another activity it started normally with no problems
Any idea what that might be.
NOTE: I'm not getting any errors or so
Signin Activity code:
package engezni.Activties;
import android.annotation.TargetApi;
import android.app.ActionBar;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.FrameLayout;
import android.widget.TextView;
import Fragments.SigninFragment;
import engezni.Activties.R;
public class SignInScreen extends Activity {
FrameLayout frameLayout;
SigninFragment signinFragment;
TextView signinScreen;
#TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR2)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_in_screen);
this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
getActionBar().setCustomView(R.layout.custom_action_bar);
getActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
/*Setting text to the textview in the xml actionbar layout */
signinScreen = (TextView) findViewById(R.id.actionbarTitleOrderScreen);
signinScreen.setText("Sign In");
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.actionbar));
getActionBar().setHomeAsUpIndicator(getResources().getDrawable(R.drawable.backlogo));
frameLayout=(FrameLayout)findViewById(R.id.signin_framelayout);
signinFragment=new SigninFragment();
if (findViewById(R.id.signin_framelayout) != null) {
if (savedInstanceState != null) {
return;
}
signinFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction().add(R.id.signin_framelayout, signinFragment).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sign_in_screen, menu);
onBackPressed();
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);
}
}
And then here is the manifest
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="#string/app_name"
android:screenOrientation="portrait"
android:theme="#android:style/Theme.Light.NoTitleBar.Fullscreen" >
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".HomeScreen"
android:label="#string/title_activity_home_screen"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".SignInScreen"
android:label="#string/title_activity_sign_in_screen"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".RegisterScreen"
android:label="#string/title_activity_register_screen"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".MyActivity"
android:label="#string/title_activity_my" >
</activity>
</application>
Problem:
getMenuInflater().inflate(R.menu.sign_in_screen, menu);
onBackPressed();
When the onCreateOptionsMenu is called that is where you go back to the last activity because you called onBackPressed which will destroy your current activity.
solution:
remove the onBackPressed

Launching an activity from an activity other than the main

I am new to android app development. I am practicing some basics with a simple app. I was able to launch an activity from my app's home screen, however, when I try to launch a third
activity from activity2 using the same method, the app fails to work.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fish);
Button anthiasButton = (Button)findViewById(R.id.anthiasButton);
anthiasButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(FishActivity.this, AnthiasActivity.class);
startActivity(intent);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kdc.reeffishguide"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.kdc.reeffishguide.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.kdc.reeffishguide.FishActivity"
android:label="#string/title_activity_fish" >
</activity>
<activity
android:name="com.kdc.reeffishguide.CoralActivity"
android:label="#string/title_activity_coral" >
</activity>
<activity
android:name="com.kdc.reeffishguide.InvertsActivity"
android:label="#string/title_activity_inverts" >
</activity>
<activity
android:name="com.kdc.reeffishguide.AnthiasActivity"
android:label="#string/title_activity_anthias" >
</activity>
</application>
</manifest>
I am trying to open AnthiasActivity from FishActivity. No errors or warnings are given in eclipse, but when I run the app and click on FishActivity the app closes. When I delete the portion of code in fish activity that is responsible for launching anthias Activity, fish activity opens, but obviously I cannot get to Anthias Activity
package com.kdc.reeffishguide;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class AnthiasActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_anthias);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.anthias, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_anthias,
container, false);
return rootView;
}
}
}
here are the logcat errors.
04-12 19:24:32.427: E/QcrilMsgTunnelSocket(22517): IOExceptionjava.io.IOException: No such file or directoryReason: No such file or directory
04-12 19:24:36.421: E/QcrilMsgTunnelSocket(22517): IOExceptionjava.io.IOException: No such file or directoryReason: No such file or directory
04-12 19:24:40.425: E/QcrilMsgTunnelSocket(22517): IOExceptionjava.io.IOException: No such file or directoryReason: No such file or directory

Categories

Resources