i tried integrating google signing in my app, I've done all I am supposes to do but when i run it on andoid device it telling me failed, please kindly help
this is my main Activity
package com.example.todo;
import static android.content.ContentValues.TAG;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.auth.api.identity.BeginSignInRequest;
import com.google.android.gms.auth.api.identity.SignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.common.api.ApiException;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthCredential;
import com.google.firebase.auth.GoogleAuthProvider;
public class MainActivity extends AppCompatActivity {
private TextView todo;
private ImageView googleBtn;
GoogleSignInOptions gso;
GoogleSignInClient gsc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
todo = findViewById(R.id.todo);
googleBtn = findViewById(R.id.googleBtn);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
gsc = GoogleSignIn.getClient(this,gso);
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct != null){
navitgateToSecondActivity();
}
googleBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
signIn();
}
});
}
private void signIn() {
Intent signInIntent = gsc.getSignInIntent();
startActivityForResult(signInIntent,1000);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1000){
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
task.getResult(ApiException.class);
navitgateToSecondActivity();
} catch (ApiException e) {
Toast.makeText(this, "Google Auth failed", Toast.LENGTH_SHORT).show();
}
}
}
}
void navitgateToSecondActivity(){
finish();
Intent intent = new Intent(this,secondActivity.class);
startActivity(intent);
}
}
this is the second activity
package com.example.todo;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.drawerlayout.widget.DrawerLayout;
import android.content.Intent;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.gms.auth.api.signin.GoogleSignIn;
import com.google.android.gms.auth.api.signin.GoogleSignInAccount;
import com.google.android.gms.auth.api.signin.GoogleSignInClient;
import com.google.android.gms.auth.api.signin.GoogleSignInOptions;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.navigation.NavigationView;
public class secondActivity extends AppCompatActivity {
private Button signOut;
private DrawerLayout drawer;
private NavigationView navigationView;
private MaterialToolbar toolbar;
GoogleSignInOptions gso;
GoogleSignInClient gsc;
private TextView profileName;
ImageView profilePic;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
gsc = GoogleSignIn.getClient(this,gso);
GoogleSignInAccount acct = GoogleSignIn.getLastSignedInAccount(this);
if (acct!=null){
String personName = acct.getDisplayName();
String personEmail = acct.getEmail();
Uri personPhoto = acct.getPhotoUrl();
profileName.setText(personName);
profilePic.setImageIcon(Icon.createWithContentUri(personPhoto));
}
initViews();
setSupportActionBar(toolbar);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,R.string.drawer_open,R.string.drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
signOut.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
logout();
}
});
}
void initViews(){
drawer = findViewById(R.id.drawer);
navigationView = findViewById(R.id.navigationView);
toolbar = findViewById(R.id.toolbar);
signOut = findViewById(R.id.signOut);
}
void logout(){
gsc.signOut().addOnCompleteListener(new OnCompleteListener<Void>() {
#Override
public void onComplete(Task<Void> task) {
finish();
startActivity(new Intent(secondActivity.this,
MainActivity.class));
}
});
}
}
i tried integrating google signing in my app, I've done all I am supposes to do but when i run it on andoid device it telling me failed, please kindly help, i tried all that expecting to get a sign in but it keeps telling me failed
Related
So I've linked my android app to back4app parse server. According to their documentation when I run the following code a class is supposed to be created in the database. However Android studio shows the following errors:
cannot resolve symbol SaveCallback
Method does not override method from its superclass
package com.example.android.beastt;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import com.parse.ParseObject;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;
import com.parse.Parse;
import com.parse.ParseException;
import com.parse.ParseUser;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ParseObject soccerPlayers = new ParseObject("SoccerPlayers");
// Store an object
soccerPlayers.put("playerName", "A. Wed");
soccerPlayers.put("yearOfBirth", 1997);
soccerPlayers.put("emailContact", "a.wed#email.io");
soccerPlayers.addAllUnique("attributes", Arrays.asList("fast", "good conditioning"));
// Saving object
soccerPlayers.saveInBackground(new SaveCallback() {
#Override
public void done(ParseException e) {
if (e == null) {
// Success
} else {
// Error
}
}
});
}
}
I'm trying my first exercise on Cucumber with Java. Getting error NullPointer and com.sun.proxy.$Proxy16 while executing.
Tried upgrading, downgrading various dependencies.
package com.cucu.runner;
import org.junit.runner.RunWith;
//import cucumber.api.CucumberOptions;
import io.cucumber.junit.Cucumber;
import io.cucumber.junit.CucumberOptions;
#RunWith(Cucumber.class)
#CucumberOptions
(
features= {".//src/main/java/com/cucu/feature/"},
glue="com.cucu.stepdef",
dryRun=false,
monochrome=true,
plugin= {"pretty","html:test-output"}
)
public class TestRunner_Cucumber {
}
Feature: LoginPage
Scenario: Successful Login with Valid Credentials
Given User launch the URL
When Page title should be "Welcome, please sign in!"
When User enters valid Username "admin#yourstore.com" and valid Password "admin"
When User clicks on LogIn button
Then User lands on the homepage and verifies the text
package com.cucu.stepdef;
import org.junit.Assert;
import org.openqa.selenium.WebDriver;
import com.cucu.configprop.TestBase;
import com.cucu.pages.LoginPage;
import cucumber.api.java.Before;
import cucumber.api.java.en.Given;
import cucumber.api.java.en.Then;
import cucumber.api.java.en.When;
public class StepDefinitions extends TestBase{
LoginPage loginPage = new LoginPage(driver);
#Before
public static void Start()
{
initialization();
}
#Given("User launch the URL")
public void user_launch_the_URL() {
launchURL();
}
#When("Page title should be \"([^\"]*)\"")
public void Page_title_should_be(String Title) {
String ActTitle = loginPage.titleValidation();
System.out.println(ActTitle);
Assert.assertSame(Title, ActTitle);
}
#When("User enters valid Username {string} and valid Password {string}")
public void user_enters_valid_Username_and_valid_Password(String username, String password) {
loginPage.enterEmail(username);
loginPage.enterPassword(password);
}
#When("User clicks on LogIn button")
public void user_clicks_on_LogIn_button() {
loginPage.clickLogIn();
}
#Then("User lands on the homepage and verifies the text")
public void user_lands_on_the_homepage_and_verifies_the_text() {
String DisplayText = loginPage.HeaderValidation();
Assert.assertSame("Incorrect Text", "Dashboard", DisplayText);
}
}
package com.cucu.configprop;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
import org.openqa.selenium.WebDriver;
import org.openqa.selenium.chrome.ChromeDriver;
import org.openqa.selenium.support.ui.WebDriverWait;
import com.cucu.pages.CreateOrdersPage;
import com.cucu.pages.CreateUsersPage;
import com.cucu.pages.HomePage;
import com.cucu.pages.LoginPage;
public class TestBase {
public static WebDriver driver;
public static LoginPage loginPage;
public static HomePage homePage;
public static CreateUsersPage createUsersPage;
public static CreateOrdersPage createOrdersPage;
public static WebDriverWait waitTime;
public static Properties prop;
public TestBase()
{
try {
prop = new Properties();
FileInputStream inp = new FileInputStream("F:\\Azar\\Cucumber_Practice\\"
+ "src\\main\\java\\com\\cucu\\configprop\\config.properties");
prop.load(inp);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
public static void initialization(){
String browserName = prop.getProperty("browser");
if(browserName.equals("chrome")){
System.setProperty("webdriver.chrome.driver","F:\\Installation\\chromedriver.exe");
driver = new ChromeDriver();
driver.manage().window().maximize();
driver.manage().deleteAllCookies();
}
}
public static void launchURL()
{
driver.get(prop.getProperty("url"));
}
public static void waitDriver()
{
waitTime = new WebDriverWait(driver, 2000);
}
}
When Page title should be "Welcome, please sign in!" # StepDefinitions.Page_title_should_be(String)
java.lang.NullPointerException
at org.openqa.selenium.support.pagefactory.DefaultElementLocator.findElement(DefaultElementLocator.java:69)
at org.openqa.selenium.support.pagefactory.internal.LocatingElementHandler.invoke(LocatingElementHandler.java:38)
at com.sun.proxy.$Proxy16.getText(Unknown Source)
at com.cucu.pages.LoginPage.titleValidation(LoginPage.java:39)
at com.cucu.stepdef.StepDefinitions.Page_title_should_be(StepDefinitions.java:31)
I run into a problem, that I really don't know how to probably create a functional buttons in Java Swing GUI ( I guess this is how I should call it). I create a print statement to check whether if my buttons work or not,and it doesn't work.Here is my code.
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.awt.*;
import java.awt.event.*;
/**
* Create a JFrame to hold our beautiful drawings.
*/
public class Jan1UI implements ActionListener
{
/**
* Creates a JFrame and adds our drawings
*
* #param args not used
*/
static JFrame frame = new JFrame();
static JButton nextBut = new JButton("NEXT");
static NextDayComponents nextDaycomponent = new NextDayComponents();
public static void main(String[] args)
{
//Set up the JFrame
nextBut.setBounds(860, 540, 100, 40);
/*nextBut.setOpaque(false);
nextBut.setContentAreaFilled(false);
nextBut.setBorderPainted(false);
*/
frame.setSize(1920, 1080);
frame.setTitle("Jan1UI demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
frame.add(nextBut);
frame.add(nextDaycomponent);
}
public void actionPerformed(ActionEvent e)
{
JButton b = (JButton)e.getSource();
if (b == nextBut)
{
System.out.println("ok");
}
}
}
/*static class Butt implements ActionListener
{
}*/
You need to add an action listener to the button, but cant do that in main as it is a static method. Instead, create a constructor to do thework similar to this:
public class Jan1UI implements ActionListener
{
public static void main(String[] args)
{
Jan1UI ui = new Jan1UI();
}
public Jan1UI ()
{
JFrame frame = new JFrame();
JButton nextBut = new JButton("NEXT");
nextBut.setBounds(860, 540, 100, 40);
nextBut.addActionListener(this);
frame.setSize(1920, 1080);
frame.setTitle("Jan1UI demo");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setBackground(Color.WHITE);
frame.setVisible(true);
frame.add(nextBut);
}
public void actionPerformed(ActionEvent e)
{
System.out.println("ok");
}
}
You must bound an ActionListener to the button:
nextBut.setBounds(860, 540, 100, 40);
nextBut.addActionListener(new Jan1UI());
I am new to android and i am getting this error.Maths1 code:
package com.materialdesign.geniusmathsandphysics;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.ConnectivityManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import org.apache.commons.net.ftp.FTPReply;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
public class Maths1 extends Activity{
MainActivity async;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.maths1);
async=new MainActivity();
Button m15,d15;
m15 = (Button) findViewById(R.id.but_m1may15);
d15 = (Button) findViewById(R.id.but_m1dec15);
m15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
File pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/" + "M1_May_15.pdf");
if (pdf.exists()) {
//file= new File(Environment.getExternalStorageDirectory().getAbsolutePath()+"/pdf/Area.pdf");
Intent i=new Intent(Maths1.this,PdfViewer.class);
i.putExtra("FileName",pdf);
startActivity(i);
} else {
if (isNetworkConnected() == true) {
Toast.makeText(Maths1.this, "Starting Download....please wait.", Toast.LENGTH_LONG).show();
async.new Connect().execute("166.62.10.29", "M1_May_15.pdf");
} else {
Toast.makeText(Maths1.this, "No internet connection \n Please try again", Toast.LENGTH_SHORT).show();
}
}
}
});
d15.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
File pdf = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/" + "M1_Dec_15.pdf");
if (pdf.exists()) {
ViewPDF();
} else {
if (isNetworkConnected() == true) {
async.new Connect().execute("166.62.10.29", "M1_Dec_15.pdf");
Toast.makeText(Maths1.this, "Starting Download....please wait.", Toast.LENGTH_LONG).show();
ViewPDF();
} else {
Toast.makeText(Maths1.this, "No internet connection \n Please try again", Toast.LENGTH_SHORT).show();
}
}
}
});
}
private boolean isNetworkConnected() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
return cm.getActiveNetworkInfo() != null;
}
public void ViewPDF()
{
Intent i=new Intent(Maths1.this,PdfViewer.class);
SavePreferences("name", "M1_Dec_15.pdf");
startActivity(i);
}
private void SavePreferences(String key, String value) {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString(key, value);
editor.commit();
}
}
pdf viewer code:
package com.materialdesign.geniusmathsandphysics;
import android.content.SharedPreferences;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import java.io.File;
public class PdfViewer extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.pdfviewer);
com.joanzapata.pdfview.PDFView Pdf = (com.joanzapata.pdfview.PDFView) findViewById(R.id.pdfview1);
//String filepath=getIntent().getExtras().getString("name");
String file=showPreferences("name");
File fd = new File(file);
//Log.i("hhhhdsdada",filepath);
Pdf.fromFile(fd).defaultPage(1).swipeVertical(true).load();
}
private String showPreferences(String key){
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String savedPref = sharedPreferences.getString(key, "");
File files= new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/GeniusPhysicsandMaths/"+savedPref);
String file=files.toString();
return file;
}
}
this is my logcat:
FATAL EXCEPTION: AsyncTask #3 Process: com.materialdesign.geniusmathsandphysics, PID: 18871
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.RuntimeException: PDF file is corrupted
at org.vudroid.pdfdroid.codec.PdfDocument.open(Native Method)
at org.vudroid.pdfdroid.codec.PdfDocument.openDocument(PdfDocument.java:28)
at org.vudroid.pdfdroid.codec.PdfContext.openDocument(PdfContext.java:18)
at org.vudroid.core.DecodeServiceBase.open(DecodeServiceBase.java:59)
at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:52)
at com.joanzapata.pdfview.DecodingAsyncTask.doInBackground(DecodingAsyncTask.java:31)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
i am using joanzapata library to view pdf files.for that i am trenter code hereying to pass that file name from maths 1 class on button click listner. The library takes file as input so i had to convert string to file previously using intent i was getting null pointer exception but now with shared preferences i am getting this error. Please help me to solve it. Thanks in advance.
No errors in code according to Eclipse IDE. But when I'm trying to run this one in device it gives an error message.
package com.android.mypackage;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import org.apache.http.entity.BufferedHttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.HttpEntity;
import android.widget.TextView;
public class MyActivity extends Activity
{
TextView textview;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View v)
{
try
{
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://www.google.com");
HttpResponse httpresponse = httpclient.execute(httpget);
HttpEntity httpentity = httpresponse.getEntity();
if (httpentity != null)
{
httpentity = new BufferedHttpEntity(httpentity);
}
}
catch (Exception e)
{
System.out.println("Exception e");
textview.setText(e.getMessage());
}
};
});
}
}
Try this: add this line
httpget.addHeader("User-Agent", "Android");
Before you execute httpget.