I got this error, when I was developing my app. When I start app on my phone, it crashes.
Something is wrong in onResume. I can't get it, what's the problem?
Android Studio is showing line with this:
My Run Log:
04-15 21:49:17.624 12927-12927/com.developerfromjokela.edison E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.developerfromjokela.edison, PID: 12927
java.lang.RuntimeException: Unable to resume activity {com.developerfromjokela.edison/com.developerfromjokela.edison.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3582)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6499)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.hashCode()' on a null object reference
at com.developerfromjokela.edison.MainActivity.onResume(MainActivity.java:132)
at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)
at android.app.Activity.performResume(Activity.java:7107)
at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3557)
at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3622)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2863)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1590)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6499)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
My MainActivity:
package com.developerfromjokela.edison;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
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.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;
import android.widget.Toast;
import java.io.File;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private WebView mWebView;
String newUA;
String oldUA;
public MainActivity() {
oldUA = System.getProperty("http.agent");
newUA = "Mozilla/7.0 (X11; U; Linux i686; en-US; rv:1.9.0.4)
Gecko/20170101 Firefox/7.0";
}
#SuppressLint({"ClickableViewAccessibility", "SetJavaScriptEnabled"})
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
if (savedInstanceState != null) {
((WebView)
findViewById(R.id.webview)).restoreState(savedInstanceState);
}
setSupportActionBar(toolbar);
if (AppStatus.getInstance(getApplicationContext()).isOnline()) {
mWebView = findViewById(R.id.webview);
mWebView.loadUrl("https://edison.fi");
mWebView.setVerticalScrollBarEnabled(true);
mWebView.setHorizontalScrollBarEnabled(true);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient(){
ProgressBar progressbar = (ProgressBar) findViewById(R.id.progressBar2);
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressbar.setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressbar.setVisibility(View.GONE);
}
});
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
} else {
Toast.makeText(getApplicationContext(), R.string.nointernetmessage , Toast.LENGTH_LONG).show();
finish();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
mWebView.saveState(outState);
}
#Override
protected void onRestoreInstanceState(Bundle state) {
super.onRestoreInstanceState(state);
}
#Override
public void onResume() {
super.onResume();
Intent intent = getIntent();
String mAction = intent.getStringExtra("action");
switch (mAction) {
case "owilma":
openservwilma();
break;
case "odocs":
opengdocs();
break;
case "odrive":
opengdrive();
break;
case "oclassroom":
opengclassroom();
break;
} {
}
}
#Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
finish();
}
}
public static void clearWebViewCachesCustom(Context context) throws PackageManager.NameNotFoundException {
String dataDir = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).applicationInfo.dataDir;
new File(dataDir + "/app_webview/").delete();
}
public void openservwilma() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://app.edison.fi/sso/wilma/login/3");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void navtoback() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://edison.fi");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengdocs() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://docs.google.com/document/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengslides() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://docs.google.com/presentation/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengdrive() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://drive.google.com/drive/u/1/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengclassroom() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://classroom.google.com/u/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void opengcalendar() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://calendar.google.com/calendar/b/0/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openghangouts() {
Intent intent = getPackageManager().getLaunchIntentForPackage("com.google.android.talk");
if (intent != null) {
// We found the activity now start the activity
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
} else {
// Bring user to the market or let them choose an app?
intent = new Intent(Intent.ACTION_VIEW);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse("market://details?id=" + "com.google.android.talk"));
startActivity(intent);
}
}
public void openmoffice() {
mWebView.getSettings().setUserAgentString(oldUA);
mWebView.getSettings().setBuiltInZoomControls(false);
mWebView.loadUrl("https://www.office.com/?auth=2&home=1");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openmword() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://office.live.com/start/Word.aspx?auth=2");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openmoutlook() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://outlook.office365.com/owa/");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
public void openaboutscreen() {
Intent OpenAbout = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(OpenAbout);
}
public void openssanopmapro() {
mWebView.getSettings().setUserAgentString(newUA);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadUrl("https://oppimisymparisto.sanomapro.fi/d2l/lp/auth/saml/login?target=https://oppimisymparisto.sanomapro.fi/d2l/home/36515");
ProgressBar progressbar = findViewById(R.id.progressBar2);
progressbar.setVisibility(View.VISIBLE);
}
#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;
} else if (id == R.id.backtodesktop){
navtoback();
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.opendocs) {
opengdocs();
} else if (id == R.id.openslides) {
opengslides();
} else if (id == R.id.opendrive) {
opengdrive();
} else if (id == R.id.openclassroom) {
opengclassroom();
} else if (id == R.id.opencalendar) {
opengcalendar();
} else if (id == R.id.openhangouts) {
openghangouts();
} else if (id == R.id.openoffice) {
openmoffice();
} else if (id == R.id.openword) {
openmword();
} else if (id == R.id.openoutlook) {
openmoutlook();
} else if (id == R.id.openwilma) {
openservwilma();
} else if (id == R.id.about_app) {
openaboutscreen();
} else if (id == R.id.opensanomapro) {
openssanopmapro();
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
May be your intent is null or intent doesn't have that string.
Before calling String mAction = intent.getStringExtra("action"); check if(intent != null && intent.hasString("action")
It will check where your intent is not null and intent has that string.
change the code at onResume() method portion:
#Override
public void onResume() {
super.onResume();
if(getIntent()!=null){
Intent intent = getIntent();
String mAction = intent.getStringExtra("action");
switch (mAction) {
case "owilma":
openservwilma();
break;
case "odocs":
opengdocs();
break;
case "odrive":
opengdrive();
break;
case "oclassroom":
opengclassroom();
break;
}
}
}
Related
I'm working on this bit of code that should reference my Firebase Cloud Database. The idea that if there is no uID in the database, (from a user that has successfully authenticated), that it should send the person to a SetupActivity class. Right now, as this codes stands, it doesn't hit the database and goes straight to my MainActivity. Permissions on the database allows read/write. Anyone have a clue that might be the issue?
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.MenuItem;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import com.firebase.ui.auth.AuthUI;
import com.firebase.ui.auth.IdpResponse;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseAuthException;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FirebaseAuth mAuth;
private DatabaseReference UsersRef;
String currentUserID;
List<AuthUI.IdpConfig> providers = Arrays.asList(
new AuthUI.IdpConfig.EmailBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build());
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mAuth = FirebaseAuth.getInstance();
UsersRef = FirebaseDatabase.getInstance().getReference().child("Users");
//Navigation Drawer
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle("Home");
DrawerLayout drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
navigationView.setNavigationItemSelectedListener(this);
}
#Override
protected void onStart()
{
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser == null)
{
SendUserToLoginActivity();
}
else
{
CheckUserExistence();
}
}
private void CheckUserExistence()
{
final String current_user_id = mAuth.getCurrentUser().getUid();
UsersRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
if(!dataSnapshot.hasChild(current_user_id))
{
SendUserToSetupActivity();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
private void SendUserToSetupActivity()
{
Intent setupIntent = new Intent(MainActivity.this, SetupActivity.class);
setupIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(setupIntent);
finish();
}
private void SendUserToLoginActivity()
{
Intent loginIntent = new Intent(MainActivity.this, LoginActivity.class);
loginIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
startActivity(loginIntent);
finish();
}
public void onBackPressed() {
DrawerLayout drawer = 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);
}
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
Fragment fragment;
if (id == R.id.nav_leader) {
} else if (id == R.id.nav_mypicks) {
} else if (id == R.id.nav_tribe) {
} else if (id == R.id.nav_results) {
} else if (id == R.id.nav_chat) {
} else if (id == R.id.nav_settings) {
} else if (id == R.id.nav_about) {
} else if (id == R.id.nav_donate) {
} else if (id == R.id.nav_logout) {
mAuth.signOut();
SendUserToLoginActivity();
} else if (id == R.id.nav_feedback) {
} else if (id == R.id.nav_profile) {
} else if (id == R.id.nav_home) {
}
DrawerLayout drawer = findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
you are doing wrong,
here current_user_id is a uId which is available in Authentication of user
and you are trying to match it with the database key, which is not the same in any way, you must do a comparison with email or phone number which you are going to authenticate a user.
do something like this,
private void CheckUserExistence()
{
final String current_user_id = mAuth.getCurrentUser().getEmail();
UsersRef.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot)
{
for(DataSnapshot dataSnapshot1 : dataSnapshot.getChildren()){
User user = dataSnapshot1.getValue(User.class);
if(user.getEmail().equels(current_user_id))
{
SendUserToSetupActivity();
break;
}
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
I want to Implement Onclick Item in recyclerView To open new activity I can pass data from one to another activity but in the code cannot figure out where to Implement on click onclicklistener if anyone is free to help me edit and give intent to Details.class
as I am getting data from firebase its first time to work on firebase any help would be appreciated.
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
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.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;
import android.widget.TextView;
import com.dekhou.provissionstore.model.Movie;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.squareup.picasso.Picasso;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private FloatingActionButton fab;
ScaleAnimation shrinkAnim;
private RecyclerView mRecyclerView;
private StaggeredGridLayoutManager mLayoutManager;
private TextView tvNoMovies,tvNoMovies1;
private Context context;
//Getting reference to Firebase Database
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference mDatabaseReference = database.getReference();
private static final String userId = "01";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
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);
//Initializing our Recyclerview
mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
tvNoMovies = (TextView) findViewById(R.id.tv_no_movies);
//tvNoMovies1 = (TextView) findViewById(R.id.addresstxt);
//scale animation to shrink floating actionbar
shrinkAnim = new ScaleAnimation(1.15f, 0f, 1.15f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
if (mRecyclerView != null) {
//to enable optimization of recyclerview
mRecyclerView.setHasFixedSize(true);
}
//using staggered grid pattern in recyclerview
mLayoutManager = new StaggeredGridLayoutManager(1, StaggeredGridLayoutManager.VERTICAL);
mRecyclerView.setLayoutManager(mLayoutManager);
//Say Hello to our new FirebaseUI android Element, i.e., FirebaseRecyclerAdapter
FirebaseRecyclerAdapter<Movie,MovieViewHolder> adapter = new FirebaseRecyclerAdapter<Movie, MovieViewHolder>(
Movie.class,
R.layout.movie_board_item,
MovieViewHolder.class,
//referencing the node where we want the database to store the data from our Object
mDatabaseReference.child("users").child(userId).child("shops").getRef()
) {
#Override
protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.address.setText(model.getAddress());
viewHolder.phone.setText(model.getPhone());
viewHolder.lit.setText(model.getLit());
viewHolder.lon.setText(model.getLon());
Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
// all your stuff
}
};
mRecyclerView.setAdapter(adapter);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getSupportFragmentManager().beginTransaction()
.replace(R.id.frame_container, new AddMovieFragment())
.addToBackStack(null)
.commit();
//animation being used to make floating actionbar disappear
shrinkAnim.setDuration(400);
fab.setAnimation(shrinkAnim);
shrinkAnim.start();
shrinkAnim.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
//changing floating actionbar visibility to gone on animation end
fab.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
}
});
}
#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.menu_map) {
Intent intent = new Intent(this, MapsActivity.class);
this.startActivity(intent);
}
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_slideshow) {
Intent cinemaIntent = new Intent(this, MapsActivity.class);
startActivity(cinemaIntent);
} else if (id == R.id.nav_manage) {
Intent cinemaIntent = new Intent(this, Get_Data.class);
startActivity(cinemaIntent);
} else if (id == R.id.nav_share) {
try {
//try to open page in facebook native app.
String uri = "fb://page/" + "293069792077"; //Cutsom URL
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}catch (ActivityNotFoundException ex){
//facebook native app isn't available, use browser.
String uri = "http://touch.facebook.com/pages/x/" + "293069792077"; //Normal URL
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
} else if (id == R.id.nav_send) {
Intent cinemaIntent = new Intent(this, About_Us.class);
startActivity(cinemaIntent);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
//ViewHolder for our Firebase UI
public static class MovieViewHolder extends RecyclerView.ViewHolder{
TextView tvMovieName,address,phone,lit,lon;
ImageView ivMoviePoster;
public MovieViewHolder(View v) {
super(v);
tvMovieName = (TextView) v.findViewById(R.id.tv_name);
address = (TextView) v.findViewById(R.id.address1);
phone = (TextView) v.findViewById(R.id.tv_phone);
lit = (TextView) v.findViewById(R.id.geo);
lon = (TextView) v.findViewById(R.id.geo2);
ivMoviePoster = (ImageView) v.findViewById(R.id.iv_movie_poster);
}
}
}
Add onClickListener to the itemView for your ViewHolder in populateViewHolder and start the Activity. like the below example.
#Override
protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.address.setText(model.getAddress());
viewHolder.phone.setText(model.getPhone());
viewHolder.lit.setText(model.getLit());
viewHolder.lon.setText(model.getLon());
Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
//intent.putExtra("model", model);
intent.putExtra("latitude", model.getLit());
intent.putExtra("longitude", model.getLon());
startActivity(intent);
}
});
// all your stuff
}
try this add onClickListener in your populateViewHolderlike this
#Override
protected void populateViewHolder(MovieViewHolder viewHolder, final Movie model, final int position) {
if(tvNoMovies.getVisibility()== View.VISIBLE){
tvNoMovies.setVisibility(View.GONE);
}
viewHolder.tvMovieName.setText(model.getMovieName());
viewHolder.address.setText(model.getAddress());
viewHolder.phone.setText(model.getPhone());
viewHolder.lit.setText(model.getLit());
viewHolder.lon.setText(model.getLon());
Picasso.with(MainActivity.this).load(model.getMoviePoster()).into(viewHolder.ivMoviePoster);
viewHolder.tvMovieName.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, DetailActivity.class);
startActivity(intent);
}
});
}
I converted a website into an app with WebView in Android Studio. Everything is fine but the youtube videos that was in my website does not go in full Screen. I am a beginner, so please help me in solving this.
Main Activity Code ---
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.KeyEvent;
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.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
import java.net.URI;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
//initializing WebView
private WebView mwebView;
private FrameLayout customViewContainer;
private WebChromeClient.CustomViewCallback customViewCallback;
private View mCustomView;
private MyWebviewClient mWebChromeClient;
#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);
//WebView
mwebView = (WebView) findViewById(R.id.myWebView);
WebSettings webSettings = mwebView.getSettings();
webSettings.setJavaScriptEnabled(true);
//improve webView performance
mwebView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
mwebView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
mwebView.getSettings().setAppCacheEnabled(true);
mwebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mwebView.setWebChromeClient(new MyChromeBrowser());
webSettings.setDomStorageEnabled(true);
webSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setUseWideViewPort(true);
webSettings.setSavePassword(true);
webSettings.setSaveFormData(true);
webSettings.setEnableSmoothTransition(true);
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
mwebView.loadUrl("https://laughonlyblog.wordpress.com");
//force links open in webview only
}
private class MyChromeBrowser extends WebChromeClient {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
}
}
#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
mwebView.loadUrl("http://www.newsweek.com/us");
} else if (id == R.id.nav_slideshow) {
mwebView.loadUrl("http://www.newsweek.com/world");
} else if (id == R.id.nav_manage) {
mwebView.loadUrl("http://www.newsweek.com/tech-science");
} else if (id == R.id.nav_gallery) {
mwebView.loadUrl("http://www.newsweek.com/sports");
} else if (id == R.id.nav_share) {
mwebView.loadUrl("http://www.newsweek.com/about-newsweek");
} else if (id == R.id.nav_send) {
mwebView.loadUrl("http://www.newsweek.com/contact");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private class MyWebviewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
/* if (Uri.parse(url).getHost().equals("www.laughonlyblog.wordpress.com/about/")) {
//open url contents in webview
return false;
} else {
//here open external links in external browser or app
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
} */
view.loadUrl(url);
return true;
}
//ProgressDialogue
ProgressDialog pd = null;
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
pd=new ProgressDialog(MainActivity.this);
pd.setTitle("Please Wait..");
pd.setMessage("Website is Loading..");
pd.show();
super.onPageStarted(view, url, favicon);
}
#Override
public void onPageFinished(WebView view, String url) {
pd.dismiss();
super.onPageFinished(view, url);
}
}
//goto previous page when pressing back button
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_BACK:
if (mwebView.canGoBack()) {
mwebView.goBack();
} else {
finish();
}
return true;
}
}
return super.onKeyDown(keyCode, event);
}
}
To achieve this follows these steps :
1) Set WebChromeClient to your webview.
Exmaple : WebView.setWebChromeClient(new MyChromeBrowser());
2) Implement method onShowCustomView() and onHideCustomView() inside your WebChromeClient.
Example : `
private class MyChromeBrowser extends WebChromeClient {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
}
#Override
public void onHideCustomView() {
super.onHideCustomView();
}
}`
3) Give android:hardwareAccelerated="true" in manifest file for your activity.
Example : <activity android:name=".MainActivity"
android:hardwareAccelerated="true"
/>
Update :
To fix this try these :
change these lines : webView.setWebChromeClient(new MyWebChromeClient()); to
mWebChromeClient = new myWebChromeClient();
webView.setWebChromeClient(mWebChromeClient);
inside initWebView() method.
To let the system decide the best orientation in full-screen mode add this line to onShowCustomView(View view, CustomViewCallback callback) method:
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
UPDATE : 09-07-2017
Or you can use a VideoEnabledWebView library for full screen viedo player in webview and much more .
Link : GitHub
Someone who can help me to fix this problem. (NoSuchMethodError) I really don't understand this error.
I have found similar problems on the web, but none of the posted solutions appear to work. Someone who can help me.
EDIT: I AM already working on Android Studio.
My screenshot
Other
package com.example.android.xxxxxxxxxxy;
import android.content.Intent;
import android.content.res.Configuration;
import android.media.MediaPlayer;
import android.net.Uri;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
public class MainActivity extends AppCompatActivity {
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
AdView adView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.setRequestAgent("android_studio:ad_template").build();
// The firstbutton (song).
assert adView != null;
adView.loadAd(adRequest);
final MediaPlayer happy_birthday_to_me = MediaPlayer.create(this, R.raw.happy_birthday_to_me);
Button playsongmeow = (Button) this.findViewById(R.id.play_song_meow);
assert playsongmeow != null;
playsongmeow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
happy_birthday_to_me.start();
}
});
// The secondbutton (song).
assert adView != null;
adView.loadAd(adRequest);
final MediaPlayer birthday = MediaPlayer.create(this, R.raw.birthday);
Button playsongmeoww = (Button) this.findViewById(R.id.play_song_meoww);
assert playsongmeoww != null;
playsongmeoww.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
birthday.start();
}
});
// The thirdbutton (song).
assert adView != null;
adView.loadAd(adRequest);
final MediaPlayer happy = MediaPlayer.create(this, R.raw.happy);
Button playsongmeowww = (Button) this.findViewById(R.id.play_song_meowww);
assert playsongmeowww != null;
playsongmeowww.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
happy.start();
}
});
// The fourthbutton (song).
assert adView != null;
adView.loadAd(adRequest);
final MediaPlayer original = MediaPlayer.create(this, R.raw.original);
Button playsongmeowwww = (Button) this.findViewById(R.id.play_song_meowwww);
assert playsongmeowwww != null;
playsongmeowwww.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
original.start();
}
});
}
private void addDrawerItems() {
String[] osArray = {"Songs", "Photos", "Videos", "About"};
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent = new Intent(MainActivity.this, MainActivity.class);
startActivity(intent);
switch (position) {
case 0: //Log History
Intent MainActivity = new Intent(MainActivity.this, MainActivity.class);
startActivity(MainActivity);
break;
case 1: //Log History
Intent Photos = new Intent(MainActivity.this, Photos.class);
startActivity(Photos);
break;
case 2: //Log History
Intent Videos = new Intent(MainActivity.this, Videos.class);
startActivity(Videos);
break;
case 3: //Log History
Intent About = new Intent(MainActivity.this, About.class);
startActivity(About);
default:
}
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("List Menu");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#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) {
// Inflate your main_menu into the menu.
int id = item.getItemId();
switch (id){
case R.id.about:
Intent About = new Intent(MainActivity.this, About.class);
startActivity(About);
break;
}
switch (id) {
case R.id.rate:
// Open this Uri.
Uri uri = Uri.parse("https://play.google.com/store/apps/details?id=com.exafmpflle.android.happybirthdayyyy&hl=en"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
}
switch (id){
case R.id.privacy:
// Open this Uri.
Uri uri = Uri.parse("https://docs.google.com/document/d/1DEBh3j3LG5WaF9nT9Gl2hpswa3yD-36MfPhVo3S_p08/edit"); // missing 'http://' will cause crashed
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
break;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The error seems to be saying that one of the methods that you are overriding is nonexistent. Try double checking the spellings of the methods that you are overriding.
If this helps, please consider marking this post as an answer.
I am doing an app for making a call directly from an image button inside my app. I want the app to be worked with both API 22 and 22. But while making the minSDK as 22, I am getting an error.
How to make it work??
MainActivity.java
import android.Manifest;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
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.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
public Context mContext;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext=this.mContext;
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);
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;
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
if (mContext.checkSelfPermission(Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + 4636));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
}
break;
}
}
}
The error is under checkSelfPermission
I had give the permission in Manifest...
You have to asked the runtime permission while compiling agaisnt 6.0
public static final int REQUEST_READ_PERMISSION =111;
// before on create as global variable
public void onClick(View v) {
switch (v.getId()) {
case R.id.imageButton:
f (CheckPermission(this, Manifest.permission.CALL_PHONE)) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + 4636));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
} else {
RequestPermission(YourActivityName.this, Manifest.permission.CALL_PHONE, REQUEST_READ_PERMISSION );
}
break;
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
#Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_READ_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:" + 4636));
callIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(callIntent);
} else {
ShowToast(getString(R.string.permission_needed_sms));
}
return;
}
}
}