Synposis: I have 3 activity (LoginActivity, MainActivity and EditProfileActivity). After Facebook login in my LoginActivity I srart the MainActivity. (Till this point the app is working perfectly correct with no error). I have a NavigationDrawer for my navigations, When I click on Edit Profile link from the NavigationDrawer it starts the EditProfileActivity.
Problem: When I click on the LeftArrow in the Top blue bar, the app crashes.
Please see the images:
MainActivity
Navigation Drawer
EditProfileActivity
App Crash on clicking the top left arrow
Here is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.example">
<uses-permission android:name="android.permission.INTERNET" />
<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">
<meta-data
android:name="com.facebook.sdk.ApplicationId"
android:value="#string/facebook_app_id" />
<activity
android:name="com.facebook.FacebookActivity"
android:configChanges="keyboard|keyboardHidden|screenLayout|screenSize|orientation"
android:label="#string/app_name" />
<activity
android:name="com.facebook.CustomTabActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="#string/fb_login_protocol_scheme" />
</intent-filter>
</activity>
<activity android:name=".LoginActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<provider
android:name="com.facebook.FacebookContentProvider"
android:authorities="com.facebook.app.FacebookContentProvider"
android:exported="true" />
<activity
android:name=".MainActivity"
android:label="#string/title_activity_main"
android:theme="#style/AppTheme.NoActionBar">
</activity>
<activity android:name=".EditProfileActivity"
android:parentActivityName=".MainActivity" >
<!-- The meta-data tag is required if you support API level 15 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
Here is my MainActivity.java
package com.example.example;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log;
import android.view.View;
import android.support.design.widget.NavigationView;
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.widget.ImageView;
import android.widget.TextView;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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.main, menu);
Bundle inBundle = getIntent().getExtras();
String first_name = inBundle.get("first_name").toString();
String last_name = inBundle.get("last_name").toString();
String imageUrl = inBundle.get("imageUrl").toString();
TextView nameView = (TextView)findViewById(R.id.nameView);
nameView.setText("" + first_name + " " + last_name);
new DownloadImage((ImageView)findViewById(R.id.imageView)).execute(imageUrl);
return true;
}
public class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public DownloadImage(ImageView bmImage) {
this.bmImage = bmImage;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return mIcon11;
}
protected void onPostExecute(Bitmap result) {
bmImage.setImageBitmap(result);
}
}
#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);
}
#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) {
} else if(id == R.id.nav_user_edit){
Intent intent = new Intent(this, EditProfileActivity.class);
startActivity(intent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
You have to define your activities parent in the manifest file like this
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="fully qualified activity name" />
Make sure that you keep this meta data of your activity containing back button. Other wise its just like you are not telling you back button where to go !!
You have to add these two lines in your MainActivity.java file:
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Add this in your onOptionsItemSelected(MenuItem item) method:
if (id==android.R.id.home)
{
NavUtils.navigateUpFromSameTask(this);
}
In your toolbar.xml set an imageview. Do like this i your toolbar.xml
<ImageView
android:id="#+id/tv_header_title2"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:src="#drawable/backbutton"
android:layout_marginLeft="15dp"/>
In MainActivity
ImageView img= (ImageView) findViewById(R.id.tv_header_title2);
and
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
Related
I am trying to get weather information using gps location, but the problem is getting value for my location 0,0 ("latitude , longitude"). When I go to setting for my phone -> " permission " found no permissions allowed, and I have to activate the permission manually. Then app start working fine .
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.ionicframework.myapp574503">
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-feature android:name="android.hardware.location.gps"/>
<uses-permission android:name="android.permission.INTERNET" />
<application android:name="android.support.multidex.MultiDexApplication"
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>
</activity>
</application>
</manifest>
MainActivity
package com.ionicframework.myapp574503;
import android.location.LocationManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.RequiresApi;
import android.support.design.widget.NavigationView;
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.widget.TextView;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
RequestQueue rq;
TextView timeDesc, windspeedDesc;
String wind;
GPSTracker gps;
LocationManager locationManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
windspeedDesc = (TextView) findViewById(R.id.textView1);
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);
// create class object
gps = new GPSTracker(MainActivity.this);
// check if GPS enabled
if(gps.canGetLocation()){
double latitude = gps.getLatitude();
double longitude = gps.getLongitude();
String url = "http://api.wunderground.com/api/xxxxxxxxxxx/conditions/hourly/forecast10day/geolookup/q/"+latitude+","+longitude+".json";
Toast.makeText(getApplicationContext(), "Your Location is - \nLat: " + latitude + "\nLong: " + longitude, Toast.LENGTH_LONG).show();
rq = Volley.newRequestQueue(this);
sendjsonrequest(url);
// \n is for new line
}else{
// can't get location
// GPS or Network is not enabled
// Ask user to enable GPS/network in settings
gps.showSettingsAlert();
}
}
public void sendjsonrequest(String url) {
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
#RequiresApi(api = Build.VERSION_CODES.N)
#Override
public void onResponse(JSONObject response) {
try {
JSONObject stationsJO = response.getJSONObject("location");
JSONArray array = stationsJO.optJSONArray("days");
wind= stationsJO.getString("city");
windspeedDesc.setText(wind);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
rq.add(jsonObjectRequest);
}
#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.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);
}
#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;
}
}
I tested on version 4.4.2 and it worked fine and permission auto allowed but on version 7.0 I have to activate permission manually from setting
any ideas ?
Try this code for Runtime Permission in Android Version 6.0 or above
private int PERMISSIONS_REQUEST_LOCATION = 100;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(MainFragmentActivity.this, ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(MainFragmentActivity.this,ACCESS_FINE_LOCATION)) {
} else {
ActivityCompat.requestPermissions(MainFragmentActivity.this, new String[]{ACCESS_FINE_LOCATION}, PERMISSIONS_REQUEST_LOCATION);
}
}
}
}
You need to request the runtime permission.
You do that using ActivityCompat.requestPermissions
There is a tutorial on doing this here
**Hey guys, i know this might question might have been asked before, I really need some help. I keep getting this 'App_Measurement_Receiver' not registered/enabled, 'App_Measurement_Service' not enabled. I cant seem to login to my App. what might be the problem guys and how may i go about it? **
08-19 21:13:50.826 3019-3255/? E/FA: 'AppMeasurementReceiver' not registered/enabled
08-19 21:13:50.827 3019-3255/? E/FA: 'AppMeasurementService' not registered/enabled
08-19 21:13:50.827 3019-3255/? E/FA: Uploading is not possible. App
measurement disabled
** Android manifest file**
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.hushtagstudios.towme">
<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true">
<activity
android:name=".SplashActivity"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/title_activity_login"
android:parentActivityName=".SplashActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hushtagstudios.towme.SplashActivity" />
</activity>
<activity
android:name=".Register"
android:label="#string/Register"
android:parentActivityName=".LoginActivity"
android:theme="#style/AppTheme">
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.hushtagstudios.towme.LoginActivity" />
</activity>
<activity
android:name=".MainActivity"
android:label="#string/Let_us_help_activity_title"
android:theme="#style/MyAppStyleTheme" />
<activity
android:name=".PaymentNav"
android:label="#string/Payment"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".HistoryNav"
android:label="#string/menu_history"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".NotificationNav"
android:label="#string/menu_Notifications"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".ShareNav"
android:label="#string/share"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".HelpNav"
android:label="#string/how_can_we_assist_you"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".SettingsNav"
android:label="#string/menu_Settings"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".AboutNav"
android:label="#string/menu_about"
android:parentActivityName=".MainActivity"
android:theme="#style/AppTheme" />
<activity
android:name=".AddPaymentMethod"
android:label="#string/add_payment_method"
android:parentActivityName=".PaymentNav"
android:theme="#style/AppTheme" />
<activity
android:name=".CreditCardNav"
android:label="#string/add_payment_method"
android:parentActivityName=".AddPaymentMethod"
android:theme="#style/AppTheme" />
<activity
android:name=".TripHelp"
android:label="#string/select_trip"
android:parentActivityName=".HelpNav"
android:theme="#style/AppTheme" />
<activity
android:name=".AccountPaymentOptions"
android:label="#string/Account_and_payments"
android:parentActivityName=".HelpNav"
android:theme="#style/AppTheme" />
<activity
android:name=".HowToUseTowMe"
android:label="#string/How_to_use_tow_me"
android:parentActivityName=".HelpNav"
android:theme="#style/AppTheme" />
<activity
android:name=".SigningUpHelp"
android:label="#string/signing_up_to_tow_me"
android:parentActivityName=".HelpNav"
android:theme="#style/AppTheme" />
<activity
android:name=".MoreHelp"
android:label="#string/More"
android:parentActivityName=".HelpNav"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignIn"
android:label="#string/menu_user_accountandpayment_issue1"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".CantUpdateMobile"
android:label="#string/menu_user_accountandpayment_issue2"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".PaymentOptions"
android:label="#string/menu_user_accountandpayment_issue3"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".AccountPaymentSettingsHelp"
android:label="#string/menu_user_accountandpayment_issue4"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".PromosCreditsHelp"
android:label="#string/menu_user_accountandpayment_issue5"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".UknownChargeHelp"
android:label="#string/menu_user_accountandpayment_issue6"
android:parentActivityName=".AccountPaymentOptions"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignInIssue1"
android:label="#string/menu_user_cant_sign_in_issue1"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignInIssue2"
android:label="#string/menu_user_cant_sign_in_issue2"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignIssue4"
android:label="#string/mennu_user_cant_sign_in_issue4"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignInIssue3"
android:label="#string/menu_user_cant_sign_in_issue3"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignInIssue5"
android:label="#string/mennu_user_cant_sign_in_issue5"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
<activity
android:name=".CantSignInIssue6"
android:label="#string/mennu_user_cant_sign_in_issue6"
android:parentActivityName=".CantSignIn"
android:theme="#style/AppTheme" />
</application>
</manifest>
MainActivity.class file
package com.hushtagstudios.towme;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import at.markushi.ui.CircleButton;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private NavigationView navigationView = null;
private CircleButton cbBattery;
private CircleButton cbAcc;
private CircleButton cbFlatTire;
private CircleButton cbFuel;
private CircleButton cbMechanical;
private CircleButton cbInsurance;
private CircleButton cbLocked;
private CircleButton cbStuckInDitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar)findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.mDrawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
cbBattery = (CircleButton)findViewById(R.id.cbBattery);
cbBattery.setOnClickListener(this);
cbAcc = (CircleButton)findViewById(R.id.cbAcc);
cbAcc.setOnClickListener(this);
cbFlatTire = (CircleButton)findViewById(R.id.cbFlatTire);
cbFlatTire.setOnClickListener(this);
cbFuel = (CircleButton)findViewById(R.id.cbFuel);
cbFuel.setOnClickListener(this);
cbInsurance =(CircleButton)findViewById(R.id.cbInsurance);
cbInsurance.setOnClickListener(this);
cbLocked = (CircleButton)findViewById(R.id.cbLocked);
cbLocked.setOnClickListener(this);
cbMechanical = (CircleButton)findViewById(R.id.cbMechanical);
cbMechanical.setOnClickListener(this);
cbStuckInDitch = (CircleButton)findViewById(R.id.cbStuckInDitch);
cbStuckInDitch.setOnClickListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.mDrawerLayout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementsWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_payment) {
startActivity(new Intent(MainActivity.this, PaymentNav.class));
} else if (id == R.id.nav_history) {
startActivity(new Intent(MainActivity.this, HistoryNav.class));
} else if (id == R.id.nav_notifications) {
startActivity(new Intent(MainActivity.this, NotificationNav.class));
} else if (id == R.id.nav_share) {
startActivity(new Intent(MainActivity.this, ShareNav.class));
} else if (id == R.id.nav_help) {
startActivity(new Intent(MainActivity.this, HelpNav.class));
} else if (id == R.id.nav_settings) {
startActivity(new Intent(MainActivity.this, SettingsNav.class));
} else if (id == R.id.nav_about) {
startActivity(new Intent(MainActivity.this, AboutNav.class));
} else if (id == R.id.nav_logout) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.mDrawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cbBattery:
break;
case R.id.cbAcc:
break;
case R.id.cbFlatTire:
break;
case R.id.cbFuel:
break;
case R.id.cbInsurance:
break;
case R.id.cbLocked:
break;
case R.id.cbMechanical:
break;
case R.id.cbStuckInDitch:
break;
}
}
}
** LoginActivity.class file **
package com.hushtagstudios.towme;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.NavigationView;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.view.View;
import at.markushi.ui.CircleButton;
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener, View.OnClickListener {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
private NavigationView navigationView = null;
private CircleButton cbBattery;
private CircleButton cbAcc;
private CircleButton cbFlatTire;
private CircleButton cbFuel;
private CircleButton cbMechanical;
private CircleButton cbInsurance;
private CircleButton cbLocked;
private CircleButton cbStuckInDitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mToolbar = (Toolbar)findViewById(R.id.nav_action);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.mDrawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
mDrawerLayout.setDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
navigationView = (NavigationView)findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
cbBattery = (CircleButton)findViewById(R.id.cbBattery);
cbBattery.setOnClickListener(this);
cbAcc = (CircleButton)findViewById(R.id.cbAcc);
cbAcc.setOnClickListener(this);
cbFlatTire = (CircleButton)findViewById(R.id.cbFlatTire);
cbFlatTire.setOnClickListener(this);
cbFuel = (CircleButton)findViewById(R.id.cbFuel);
cbFuel.setOnClickListener(this);
cbInsurance =(CircleButton)findViewById(R.id.cbInsurance);
cbInsurance.setOnClickListener(this);
cbLocked = (CircleButton)findViewById(R.id.cbLocked);
cbLocked.setOnClickListener(this);
cbMechanical = (CircleButton)findViewById(R.id.cbMechanical);
cbMechanical.setOnClickListener(this);
cbStuckInDitch = (CircleButton)findViewById(R.id.cbStuckInDitch);
cbStuckInDitch.setOnClickListener(this);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.mDrawerLayout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item){
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementsWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.nav_payment) {
startActivity(new Intent(MainActivity.this, PaymentNav.class));
} else if (id == R.id.nav_history) {
startActivity(new Intent(MainActivity.this, HistoryNav.class));
} else if (id == R.id.nav_notifications) {
startActivity(new Intent(MainActivity.this, NotificationNav.class));
} else if (id == R.id.nav_share) {
startActivity(new Intent(MainActivity.this, ShareNav.class));
} else if (id == R.id.nav_help) {
startActivity(new Intent(MainActivity.this, HelpNav.class));
} else if (id == R.id.nav_settings) {
startActivity(new Intent(MainActivity.this, SettingsNav.class));
} else if (id == R.id.nav_about) {
startActivity(new Intent(MainActivity.this, AboutNav.class));
} else if (id == R.id.nav_logout) {
startActivity(new Intent(MainActivity.this, LoginActivity.class));
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.mDrawerLayout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.cbBattery:
break;
case R.id.cbAcc:
break;
case R.id.cbFlatTire:
break;
case R.id.cbFuel:
break;
case R.id.cbInsurance:
break;
case R.id.cbLocked:
break;
case R.id.cbMechanical:
break;
case R.id.cbStuckInDitch:
break;
}
}
}
Disable Firebase .
check this dropdown :
Also additionally
in my case that was simple. I had BroadcastReciever class declared within a java file. It wasnt envolved into the code, like a garbage test code. The problem started show that "AppMeasurementReceiver not registered/enabled" right after I have removed useless reciever node in manifest. So finally I removed that class switched processes in second (dropdown) window from left side of Android Monitor and it started work as usually.
I made an app with navigation drawer whose default activity at startup is HomeActivity. I want it to the UserActivity to start by default at launch. I tried doing it with AndroidManifest.xml but it didn't work. Can u tell me another way of doing it like using intent or something in the HomeActivity such that it loads to UserActivity as soon as the app opens?
HomeActivity.java
package thenerdimite.nuttybuddies;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.LayoutInflater;
import android.view.View;
import android.support.design.widget.NavigationView;
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.ViewGroup;
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
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, 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);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
android.app.FragmentManager fragmentmanager = getFragmentManager();
if (id == R.id.nav_activity_user) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new UserActivity())
.commit ();
} else if (id == R.id.nav_activity_blog) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new BlogActivity())
.commit ();
} else if (id == R.id.nav_activity_Chat) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new ChatActivity())
.commit ();
} else if (id == R.id.nav_activity_fbgroup) {
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new FBGroupActivity())
.commit ();
} 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;
}
}
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="thenerdimite.nuttybuddies">
<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=".HomeActivity"
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>
</activity>
<activity android:name=".BlogActivity" />
<activity android:name=".ChatActivity" />
<activity android:name=".FBGroupActivity" />
<activity android:name=".UserActivity"></activity>
</application>
</manifest>
UserActivity.java
package thenerdimite.nuttybuddies;
import android.app.Fragment;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class UserActivity extends Fragment {
View myView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.activity_user, container, false);
return myView;
}
}
activity_home_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_activity_user"
android:icon="#drawable/ic_menu_camera"
android:title="Home" />
<item
android:id="#+id/nav_activity_blog"
android:icon="#drawable/ic_menu_gallery"
android:title="Blog" />
<item
android:id="#+id/nav_activity_Chat"
android:icon="#drawable/ic_menu_slideshow"
android:title="Chat Room" />
<item
android:id="#+id/nav_activity_fbgroup"
android:icon="#drawable/ic_menu_manage"
android:title="Facebook Group" />
</group>
<item android:title="Communicate">
<menu>
<item
android:id="#+id/nav_share"
android:icon="#drawable/ic_menu_share"
android:title="About Us" />
<item
android:id="#+id/nav_send"
android:icon="#drawable/ic_menu_send"
android:title="Click Here to Report App related Issues" />
</menu>
</item>
</menu>
Any help would be great. Thank You!
Add onNavigationItemSelected in OnCreate of HomeActivity.
public class HomeActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
......
......
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
if (savedInstanceState == null) {
MenuItem item = navigationView.getMenu().getItem(0);
onNavigationItemSelected(item);
}
}
}
And remove this line From AndroidManifest.xml
<activity android:name=".UserActivity"></activity>
In AndroidManifest delete this
<intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter>
And add it to the activity you want to show at start
Hello #Bhavesh I thought you are using fragment so you doesn't need to declare in manifest just put last in on create method you will be in UserActivity
getSupportFragmentManager().beginTransaction()
.replace(R.id.content_frame, new UserActivity())
.commit();
I have been recently trying to make an Android app that simply calls a number . I came up with the following code .main activity:
package com.example.irdeesmughal.callapp;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
public void call()
{
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:03335975321"));
try{
startActivity(callIntent);
}
catch (android.content.ActivityNotFoundException ex){
Toast.makeText(getApplicationContext(),"yourActivity is not founded", Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
});
}
#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);
}
}
problem is when I run this app on my Galaxy Note 5 and click the call button, the app crashes.
android manifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.irdeesmughal.callapp">
<uses-permission android:name="android.permission.CALL_PHONE" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<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"
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>
</activity>
</application>
</manifest>
putting this in your call method may helps:
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:03335975321"));
try
{
startActivity(callIntent);
}
catch (Exception e)
{
e.printStackTrace();
}
You should request runtime permission on Android 6.0+ Documentation
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CALL_PHONE) ==
PackageManager.PERMISSION_GRANTED){
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + phoneNumber));
startActivity(callIntent);
} else {
snackWithOk(R.string.please_enable_call_permission);
}
I got some errors when I am trying to run my application ... ( I edit manifest a few days ago!)
I installed the lastest SDK files and it seems hasn't problem with SDK version...
errors in logcat:
Click To show my errors picture 1
Click To show my errors picture 2
and here is my manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="ir.homa">
<permission
android:name="android.permission.INTERNET">
</permission>
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="23" />
<application
android:name=".AppController"
android:allowBackup="true"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".HomaActivity"
android:label="#string/app_name"
android:launchMode="singleTop" >
<intent-filter >
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".LoginActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
<activity
android:name=".RegisterActivity"
android:label="#string/app_name"
android:launchMode="singleTop"
android:windowSoftInputMode="adjustPan" >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</activity>
</application>
</manifest>
UPDATE:
my HomaActivity code:
package ir.homa;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.support.design.widget.NavigationView;
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;
public class HomaActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homa);
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.homa, 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);
}
#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;
}
}
The error occurs because you're trying to add a toolbar:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
And it has already been supplied by window decor. It is all written in the error.
You can find your answer here. Which you could've googled out yourself also.