I am working on new project when I enter to RegisterActivity the logCat printing
11-07 20:12:09.305 26674-26674/com.rosheta D/NSD: curPkgName is not in list
too much and not stop printing it.
what is this? is this is an error? could this lot of logs printing reduce the performance of app?
here is my RegisterActivity
package com.rosheta.activities;
import android.app.Activity;
import android.os.Bundle;
import android.support.v7.widget.AppCompatImageButton;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.rosheta.R;
import com.rosheta.api.Api;
import com.rosheta.api.ApiHelper;
import com.rosheta.api.response.UsernameResponse;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
/**
* Created by Tefa on 07/11/2016.
*/
public class Reg extends Activity implements View.OnClickListener {
EditText fName, lName, username, email, password, confirmPassword, phone;
Button signUp;
TextView login;
AppCompatImageButton checkUsername;
Api api;
private static final String TAG = Reg.class.getSimpleName();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
initializeViewItems();
}
private void initializeViewItems() {
fName = (EditText) findViewById(R.id.act_register_et_firstName);
lName = (EditText) findViewById(R.id.act_register_et_lastName);
username = (EditText) findViewById(R.id.act_register_et_username);
email = (EditText) findViewById(R.id.act_register_et_email);
password = (EditText) findViewById(R.id.act_register_et_password);
confirmPassword = (EditText) findViewById(R.id.act_register_et_confirm_password);
phone = (EditText) findViewById(R.id.act_register_et_phone);
signUp = (Button) findViewById(R.id.act_register_b_signup);
signUp.setOnClickListener(this);
login = (TextView) findViewById(R.id.act_register_tv_login);
login.setOnClickListener(this);
checkUsername = (AppCompatImageButton) findViewById(R.id.act_register_ib_check_username);
checkUsername.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.act_register_b_signup:
attemptLogin();
break;
case R.id.act_register_ib_check_username:
String inputUsername = username.getText().toString();
if (TextUtils.isEmpty(inputUsername)) {
username.setError("Username can not be blank");
return;
} else {
checkUsernameValidity(inputUsername);
}
break;
}
}
private void checkUsernameValidity(String inputUsername) {
Log.d(TAG,"checkUsernameValidity");
try {
ApiHelper.base_url = getString(R.string.base_url);
api = ApiHelper.getClient().create(Api.class);
api.checkUsername(inputUsername).enqueue(new Callback<UsernameResponse>() {
#Override
public void onResponse(Call<UsernameResponse> call, Response<UsernameResponse> response) {
if (response.code() == 200) {
Log.d(TAG, "response = 200");
UsernameResponse uResponse = response.body();
if (uResponse.getCode() == 20) {
checkUsername.setImageResource(R.drawable.ic_accepted_username);
} else if (uResponse.getCode() == 3) {
checkUsername.setImageResource(R.drawable.ic_invalid_username);
username.setError(uResponse.getErrorMessage());
}
} else {
Log.e(TAG, "response != 200");
if (!TextUtils.isEmpty(response.message())) {
Log.e(TAG, response.message());
}
}
}
#Override
public void onFailure(Call<UsernameResponse> call, Throwable t) {
Log.e(TAG, "fail");
t.printStackTrace();
}
});
}catch (Exception e){
Log.e(TAG,"Exceptio");
e.printStackTrace();
}
}
private void attemptLogin() {
String firstName = fName.getText().toString();
String lastName = lName.getText().toString();
String inputUsername = username.getText().toString();
String inputEmail = email.getText().toString();
String inputPass = password.getText().toString();
String inputCPass = confirmPassword.getText().toString();
String inputPhone = phone.getText().toString();
if (TextUtils.isEmpty(firstName)) {
fName.setError("First name can not be blank");
return;
}
if (TextUtils.isEmpty(lastName)) {
lName.setError("Last name can not be blank");
return;
}
if (TextUtils.isEmpty(inputUsername)) {
username.setError("Email can not be blank");
return;
}
if (TextUtils.isEmpty(inputEmail)) {
email.setError("Email can not be blank");
return;
}
if (TextUtils.isEmpty(inputPass)) {
password.setError("Password can not be blank");
return;
}
if (TextUtils.isEmpty(inputCPass)) {
confirmPassword.setError("Please confirm your password");
return;
}
if (inputPass.equals(inputCPass)) {
password.setError("Passwords don't match");
return;
}
//TODO handle phone number
//TODO register request
}
}
and activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_register"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroundColor"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.rosheta.activities.RegisterActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<LinearLayout
android:id="#+id/act_register_main_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/ll_names_et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1">
<EditText
android:id="#+id/act_register_et_firstName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="First name"
android:inputType="text" />
<EditText
android:id="#+id/act_register_et_lastName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:hint="Last name"
android:inputType="text" />
</LinearLayout>
<EditText
android:id="#+id/act_register_et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="E-mail"
android:inputType="textEmailAddress" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_margin="5dp"
>
<EditText
android:id="#+id/act_register_et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Username"
android:inputType="text" />
<android.support.v7.widget.AppCompatImageButton
android:id="#+id/act_register_ib_check_username"
android:layout_width="35dp"
android:layout_height="match_parent"
android:src="#drawable/ic_refresh_username"
/>
</LinearLayout>
<EditText
android:id="#+id/act_register_et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/act_register_et_confirm_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Confirm Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/act_register_et_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:hint="Phone number"
android:inputType="phone" />
<Button
android:id="#+id/act_register_b_signup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="Sign up" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/tv_have_account"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="5dp"
android:text="#string/have_account_text" />
<TextView
android:id="#+id/act_register_tv_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv_have_account"
android:layout_toEndOf="#+id/tv_have_account"
android:layout_toRightOf="#+id/tv_have_account"
android:text="login"
tools:textColor="?android:attr/textColorLink" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
[update]
I found a way to filter this Log from spamming my logcat
in top right corner of logcat select Edit Filter Configuration
1-in filter name write your app name
2-in package name write your app_package_name
3-in (Log message write ^((?!(?:THE_TAG_YOU_NEED_TO_IGNORE)).)$ or Log tag write ^((?!(?:THE_TAG_YOU_NEED_TO_IGNORE)).)$ )
Then it is not bug in your code. Huawei phones shows this log and they have not fixed it. So ignore it.
It seems to me this might be related to the Theme Creator tool nsd.solutions.huaweithemecreator (https://www.apk-s.com/nsd.solutions.huaweithemecreator/), e.g., the UI is changing and the theme creator is checking if a custom theme should be applied given the package name.
Related
I am trying to create an Android app, then has an initial login screen that detects if a user is Admin or User. Once logged in, the user (depending on their role) will see different tabs to swipe between. I have a basic app that starts with a login activity and the login to decipher the users roles (using static values for now). First part is ok. Now when I try to use Fragments after the login screen, I'm getting stuck.
I have a LoginActivity that has an onClikcListener on a button after user enters credentials. Depending on whether the user is Amin or General Users, it will just to a new activity either activity_vote_admin or activity_vote_user. At this point I am stuck. Any ideas if what I am trying to do is valid. Or is there a better way to do this. At the moment I am getting this error.
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.confidencevotefragmentrebuild/com.example.confidencevotefragmentrebuild.activity_vote_admin}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference.
LoginActivity (this is my main activity)
...
package com.example.confidencevotefragmentrebuild;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private static EditText username;
private static EditText password;
public static TextView attempts;
private static Button submit_button;
int attempt_counter = 5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
loginButton();
}
public void loginButton() {
username = (EditText) findViewById(R.id.editText);
password = (EditText) findViewById(R.id.editText2);
attempts = (TextView) findViewById(R.id.textView_Attempts);
//Reference button view
final Button submit_button = findViewById(R.id.submit_button);
// perform click event on button
submit_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (username.getText().toString().equals("admin") &&
password.getText().toString().equals("pass")) {
Toast.makeText(LoginActivity.this, "Welcome " + username.getText(),
Toast.LENGTH_SHORT).show();
Intent intent = new Intent(LoginActivity.this, activity_vote_admin.class);
startActivity(intent);
} else if (username.getText().toString().equals("user") &&
password.getText().toString().equals("pass")) {
Toast.makeText(LoginActivity.this, "Welcome " + username.getText(),
Toast.LENGTH_SHORT).show();
Intent intentUser = new Intent(LoginActivity.this, activity_vote_user.class);
startActivity(intentUser);
} else {
Toast.makeText(LoginActivity.this, "User or Password incorrect",
Toast.LENGTH_SHORT).show();
attempt_counter--;
attempts.setText(Integer.toString(attempt_counter));
if (attempt_counter == 0) {
Toast.makeText(LoginActivity.this, "Too many failed attempts, please close app and try again",
Toast.LENGTH_SHORT).show();
submit_button.setEnabled(false);
}
}
}
}
);
}
}
...
Activity_vote_admin
...
package com.example.confidencevotefragmentrebuild;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.RatingBar;
import android.widget.Spinner;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.viewpager.widget.ViewPager;
public class activity_vote_admin extends AppCompatActivity {
/**
* Field for selecting the number of confidence votes
*/
RatingBar mResults;
private RatingBar rBar;
Button mBtn;
/**
* Spinner field to enter the project name
*/
private Spinner mProjectSpinner;
/**
* Projects. The possible values are:
* stored locally - Needs to get values from a database. TODO
*/
int mProject = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_vote_main_admin);
// Find the view pager that will allow the user to swipe between fragments
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
// Create an adapter that knows which fragment should be shown on each page
AdminFragmentPagerAdapter adapter = new AdminFragmentPagerAdapter(getSupportFragmentManager());
// Set the adapter onto the view pager
viewPager.setAdapter(adapter);
// initiate a rating bar
rBar = findViewById(R.id.rating_bar);
//float vote = rBar.getRating();
// Find all relevant views for user input
mResults = findViewById(R.id.rating_bar);
mProjectSpinner = findViewById(R.id.spinner_project);
// Run getRating method to get rating number from a rating bar
//getRating();
// Initiate button
mBtn = findViewById(R.id.voteButton);
// perform click event on button
mBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// get values and then displayed in a toast
//String totalStars = "Total Stars:: " + RatingBar.getNumStars();
float vote = rBar.getRating();
String rating = "Rating : " + vote;
Toast.makeText(getApplicationContext(), rating + "\n", Toast.LENGTH_LONG).show();
}
});
projectSpinner();
}
// Setup the dropdown spinner that allows the user to select the role.
private void projectSpinner() {
// Create adapter for spinner. The list options are from the String array it will use
// the spinner will use the default layout
ArrayAdapter projectSpinnerAdapter = ArrayAdapter.createFromResource(this,
R.array.projects, android.R.layout.simple_spinner_item);
// Specify dropdown layout style - simple list view with 1 item per line
projectSpinnerAdapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
// Apply the adapter to the project spinner
mProjectSpinner.setAdapter(projectSpinnerAdapter);
// Set the integer mSelected to the constant values
mProjectSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selection = (String) parent.getItemAtPosition(position);
if(mProjectSpinner.getSelectedItem() == "This is Hint Text");
if (!TextUtils.isEmpty(selection)) {
if (selection.equals(getString(R.string.nextGEMS1_5))) {
mProject = 0 ;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_6))) {
mProject = 1;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_7))) {
mProject = 2;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_8))) {
mProject = 3;
return;
} else if (selection.equals(getString(R.string.nextGEMS1_9))) {
mProject = 4;
return;
}
else mProject = 0;
}
}
// Because AdapterView is an abstract class, onNothingSelected must be defined
#Override
public void onNothingSelected(AdapterView<?> parent) {
mProject = 0; // User
}
});
}
}
...
activity_vote_main_admin.xml
...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.viewpager.widget.ViewPager
android:name="com.example.confidencevotefragmentrebuild.testFragment1"
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
...
activity_login.xml
...
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".LoginActivity"
tools:showIn="#layout/activity_login">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="#+id/login_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:text="LOGIN"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#ffffff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp"
android:layout_below="#+id/login_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="450dp"
android:orientation="vertical"
android:paddingTop="30dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp">
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:drawableLeft="#drawable/usericon"
android:hint="User Name"
android:inputType="textEmailAddress"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/editText2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:drawableLeft="#drawable/lock"
android:hint="Password"
android:inputType="textPassword"
android:maxLines="1" />
</com.google.android.material.textfield.TextInputLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:paddingTop="5dp"
android:text="Forgot Password?" />
<Button
android:id="#+id/submit_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#drawable/action_buttom"
android:text="Sign in"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="horizontal"
android:paddingTop="1dp">
<TextView
android:id="#+id/attempts_text"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_gravity="center_horizontal"
android:paddingTop="1dp"
android:text="Number of attempts remaining: " />
<TextView
android:id="#+id/textView_Attempts"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_gravity="center_horizontal"
android:paddingTop="1dp"
android:text="5" />
</LinearLayout>
<TextView
android:id="#+id/lnkRegister"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="1dp"
android:gravity="center"
android:paddingTop="30dp"
android:text="Register here"
android:textColor="#5fb0c9"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
<ImageButton
android:id="#+id/conferencevote_logo"
android:layout_width="210dp"
android:layout_height="180dp"
android:layout_below="#+id/login_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:elevation="4dp"
android:background="#drawable/rounded_button"
android:src="#drawable/confvotenew"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
...
AdminFragemntyPagerAdapter
...
package com.example.confidencevotefragmentrebuild;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
public class AdminFragmentPagerAdapter extends FragmentPagerAdapter {
public AdminFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
if (position == 0) {
return new testFragment1();
} else if (position == 1) {
return new testFragment2();
} else {
return new testFragment2();
}
}
#Override
public int getCount() {
return 3;
}
}
...
aactivity_vote_user.xml
...
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#5fb0c9"
android:orientation="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView
android:id="#+id/vote_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:layout_marginTop="16dp"
android:gravity="center_horizontal"
android:text="USER VOTE"
android:textColor="#fff"
android:textSize="20sp"
android:textStyle="bold"/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="500dp"
android:background="#ffffff"
android:elevation="4dp"
android:orientation="vertical"
android:padding="20dp"
android:layout_below="#+id/vote_title"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="80dp">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="450dp"
android:orientation="vertical"
android:paddingTop="70dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp">
<Spinner
android:id="#+id/spinner_project"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:hint="#string/spinner_hint"
android:maxLines="1"
android:singleLine="true"
android:backgroundTint="#color/attCobalt"
android:popupBackground="#color/myBlue"
android:dropDownSelector="#color/attBlue"
/>
</com.google.android.material.textfield.TextInputLayout
>
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<RatingBar
android:id="#+id/rating_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:stepSize="1.0"
android:layout_marginTop="40dp"
android:theme="#style/rating_bar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="5dp"
android:text="Vote and Submit" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</com.google.android.material.textfield.TextInputLayout>
<Button
android:id="#+id/voteButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="22dp"
android:background="#drawable/action_buttom"
android:text="Submit"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="18sp" />
/>
</LinearLayout>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<ImageButton
android:id="#+id/conferencevote_logo"
android:layout_width="210dp"
android:layout_height="180dp"
android:layout_below="#+id/login_title"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:elevation="4dp"
android:background="#drawable/rounded_button"
android:src="#drawable/confvotenew"
/>
</RelativeLayout>
...
My fragments are as follows.
...
package com.example.confidencevotefragmentrebuild;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
public class testFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.activity_vote_user, container, false);
}
}
...
There are two reason for this issue :
You didn't declare any button on "activity_vote_main_admin.xml", thats why your view not binding with activity and giving null pointer exception.
May be you declare the button on fragment xml and trying to bind it with activity.
I'm having trouble with phone number authentication. Verify_otp generates a unique code for any verified number. But the biggest problem is that it only generates the code if the numbers start with 6599 ..... a number 6598 .... it no longer generates the code.
I can not find an outlet for him to accept any phone number.
this is a part of the RegistrationModel.java file
public static class OTP_Details{
/**
* status : 2
* message : Otp Sent to phone for Verification
* otp : 2017
* auto_otp : 1
*/
private int status;
private String message;
private String otp;
private int auto_otp;
public int getStatus() {
return status;
}
public void setStatus(int status) {
this.status = status;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getOtp() {
return otp;
}
public void setOtp(String otp) {
this.otp = otp;
}
public int getAuto_otp() {
return auto_otp;
}
public void setAuto_otp(int auto_otp) {
this.auto_otp = auto_otp;
}
}
}
This is Vetify_otp
package com.motofacil.passageiro;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.motofacil.passageiro.accounts.RegistrationModel;
import com.motofacil.passageiro.accounts.ResultCheckMessage;
import com.motofacil.passageiro.accounts.ResultChecker;
import com.motofacil.passageiro.manager.ApiManager;
import com.motofacil.passageiro.manager.SessionManager;
import com.motofacil.passageiro.samwork.Config;
import com.motofacil.passageiro.urls.Apis;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.hbb20.CountryCodePicker;
import java.util.HashMap;
public class Verify_OTP extends AppCompatActivity implements ApiManager.APIFETCHER {
ApiManager apiManager ;
GsonBuilder gsonBuilder;
RegistrationModel.OTP_Details otp_details;
private TextView otpError_txt;
private EditText otp_input, edt_enter_phone;
private LinearLayout submit_otp_layout;
SessionManager sessionManager ;
Gson gson;
String code, input_phone_number, otp;
CountryCodePicker codePicker;
private static final int KEY_REGISTER = 110;
Button generate_otp;
LinearLayout submit_otp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
apiManager = new ApiManager(this , this , this );
sessionManager = new SessionManager(Verify_OTP.this);
otp_details = new RegistrationModel.OTP_Details();
setContentView(R.layout.activity_verify__otp);
gsonBuilder = new GsonBuilder();
gson = gsonBuilder.create();
submit_otp = (LinearLayout) findViewById(R.id.otp_submit);
generate_otp = (Button) findViewById(R.id.generate_otp);
edt_enter_phone = (EditText)findViewById(R.id.edt_enter_phone);
otp_input = (EditText)findViewById(R.id.otp_edt);
otpError_txt = (TextView)findViewById(R.id.otp_verifier_txt);
submit_otp_layout = (LinearLayout)findViewById(R.id.otp_submit);
codePicker = (CountryCodePicker)findViewById(R.id.otp_ccp);
submit_otp.setEnabled(false);
generate_otp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
input_phone_number = edt_enter_phone.getText().toString().trim();
Log.e("input_phone_number====", input_phone_number);
code = codePicker.getSelectedCountryCodeWithPlus();
Log.e("COUNTRY_CODE_PICKER===", code);
if (input_phone_number.equals("")){
Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
}else {
getOTP(code+input_phone_number);
}
}
});
submit_otp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (otp_input.getText().toString().equals("")) {
Toast.makeText(Verify_OTP.this, R.string.required_field_missing, Toast.LENGTH_SHORT).show();
} else if (!otp_input.getText().toString().equals(otp)) {
// Toast.makeText(Verify_OTP.this, R.string.invalid_otp, Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.putExtra("phone_number", code + input_phone_number);
setResult(Activity.RESULT_OK, intent);
finish();
} else {
Intent intent = new Intent();
intent.putExtra("phone_number", code + input_phone_number);
setResult(Activity.RESULT_OK, intent);
finish();
}
}
});
}
private void getOTP(String phone) {
HashMap<String , String > bodyparameters = new HashMap<String, String>();
bodyparameters.put("phone" , phone);
bodyparameters.put("flag" ,"1");
apiManager.execution_method_post(Config.ApiKeys.KEY_VERIFY_OTP ,""+ Apis.SEND_OTP, bodyparameters, true,ApiManager.ACTION_SHOW_TOP_BAR);
}
#Override
public void onFetchComplete(Object script, String APINAME) {
if(APINAME.equals(""+Config.ApiKeys.KEY_VERIFY_OTP)){
submit_otp.setEnabled(true);
RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
// finilalizeActivity();
Log.e("**OTP_SCRIPT-----", String.valueOf(otp_response.getMessage() + otp_response.getOtp() + otp_response.getStatus()));
otp = otp_response.getOtp();
Log.d("otp==normal sign up==", otp);
otp_input.setText(""+otp);
otp_input.requestFocus();
if(otp_response.getAuto_otp() == 1){
otp_input.setText(""+otp);
}
}else {
try{ResultChecker rcheck = gson.fromJson("" + script, ResultChecker.class);
Log.e("**OTP_SCRIPT-----", String.valueOf(script));
if(rcheck.getResult() == 1){
Log.e("**RCHHECKK---", String.valueOf(rcheck.getResult()));
RegistrationModel.OTP_Details otp_response = gson.fromJson("" + script, RegistrationModel.OTP_Details.class);
}else {
ResultCheckMessage rr = gson.fromJson("" + script, ResultCheckMessage.class);
Toast.makeText(this, ""+rr.getMessage(), Toast.LENGTH_SHORT).show();
}
}catch (Exception e){}
}
}
#Override
public void onFetchResultZero(String s) {
}
}
This is Activity_forgotpass_verify_otp.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.motofacil.passageiro.ForgotPass_Verify_OTP">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/pure_white"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/otp_back"
android:layout_width="50dp"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="invisible">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/ic_left_sort"
android:tint="#color/icons_8_muted_grey"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<com.motofacil.passageiro.accounts.TypefaceDosisRegular
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/verifyOTP"
android:textColor="#color/icons_8_muted_grey"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="50dp"
android:layout_height="match_parent" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/icons_8_muted_grey" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/login_banner"></ImageView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bf000000"></RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/icons_8_muted_grey" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center|top"
android:layout_marginTop="20dp"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:id="#+id/phone_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="15dp"
android:background="#drawable/shapes_white_transparent"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="horizontal">
<com.hbb20.CountryCodePicker
android:id="#+id/otp_ccp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:hideNameCode="true"
app:keyboardAutoPopOnSearch="false"
app:showFlag="false"
android:layout_marginLeft="5dp"
app:defaultCode="55"
app:textSize="15dp"/>
<EditText
android:id="#+id/edt_enter_phone"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginLeft="10dp"
android:background="#android:color/transparent"
android:ems="10"
android:drawableTint="#color/icons_8_muted_grey"
android:gravity="center|left"
android:hint="Enter Phone Number"
android:inputType="phone"
android:maxLength="10"
android:minLines="1"
android:padding="5dp"
android:textColor="#color/pure_black"
android:textSize="17dp" />
</LinearLayout>
<!--
<View
android:layout_width="match_parent"
android:layout_height="0.85dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="6dp"
android:background="#color/icons_8_muted_yellow" />-->
<Button
android:id="#+id/generate_otp"
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/generateOTP" />
<TextView
android:id="#+id/otp_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="#string/otp_text"
android:textColor="#color/pure_white"
android:textSize="17dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="70dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:gravity="center"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_marginTop="30dp"
android:background="#drawable/shapes_white_transparent"
android:orientation="horizontal">
<EditText
android:id="#+id/otp_edt"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent"
android:drawableTint="#color/icons_8_muted_grey"
android:gravity="center|left"
android:hint="#string/enter_otp"
android:inputType="number"
android:paddingLeft="10dp"
android:textColor="#color/pure_black"
android:textSize="17dp" />
<TextView
android:id="#+id/otp_verifier_txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:gravity="center|right"
android:padding="10dp"
android:text="Email not valid"
android:textColor="#color/icons_8_muted_red"
android:visibility="gone"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/otp_submit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimary"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/submitOTP"
android:textColor="#color/pure_white"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Ready! I've submitted below the files I've listed. After typing the phone and clicking send otp, the code that comes back is always the code 2017, that is the first code I sent, but it only sends this code 2017 if the numbers start like this: 6599 .... if the number is so for example: 6598 .... it does not generate the code 2017 and can not continue the registration.
I'm using an ImageView to fetch the data from a TextView and use it to call the ACTION_DIAL. Whenever the app is run, it is unable to launch the dialer to display the number. It shows that the getPackageManger is null. I've added the code with the XML below. If I remove the else statement below the app will crash.
Code:
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Picasso;
public class PostDetailActivity extends AppCompatActivity {
TextView mTitleTv, mDetailTv, mCategoryTv, mCallTv;
ImageView mImageIv;
private static final String TAG = "PostDetailActivity";
String contact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_detail);
//Action Bar
ActionBar actionBar = getSupportActionBar();
// ActionBar title
actionBar.setTitle("Coupon Details");
// Set Back Button
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
// Initialise
mTitleTv = findViewById(R.id.titleTv);
mDetailTv = findViewById(R.id.descriptionTv);
mImageIv = findViewById(R.id.imageView);
mCategoryTv = findViewById(R.id.categoryTv);
mCallTv = findViewById(R.id.callTv);
// get data from intent
String image = getIntent().getStringExtra("image");
String title = getIntent().getStringExtra("title");
String desc = getIntent().getStringExtra("description");
String cate = getIntent().getStringExtra("category");
String call = getIntent().getStringExtra("contact");
contact = call;
// Set Data to views
mTitleTv.setText(title);
mDetailTv.setText(desc);
mCategoryTv.setText(cate);
mCallTv.setText(call);
Picasso.get().load(image).into(mImageIv);
}
// Handle onBack pressed to go back to the previous activity
#Override
public boolean onSupportNavigateUp() {
onBackPressed();
return true;
}
public void dialNumber(View view) {
TextView textView = (TextView) findViewById(R.id.callTv);
// Use format with "tel:" and phone number to create phoneNumber.
String phoneNumber = String.format("",
textView.getText().toString().trim());
// String phoneNumber = "1234567890";
// Create the intent.
Intent dialIntent = new Intent(Intent.ACTION_DIAL);
// Set the data for the intent as the phone number.
dialIntent.setData(Uri.parse(phoneNumber));
// If package resolves to an app, send intent.
if (dialIntent.resolveActivity(getPackageManager()) != null) {
startActivity(dialIntent);
} else {
Log.e(TAG, "Can't resolve app for ACTION_DIAL Intent.");
}
}
}
The XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#fff"
app:cardCornerRadius="5dp"
app:cardElevation="2dp"
app:cardUseCompatPadding="true"
app:contentPadding="10dp"
tools:context=".PostDetailActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="64dp"
android:layout_height="64dp"
android:background="#color/cardview_light_background" />
<TextView
android:id="#+id/titleTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:ellipsize="end"
android:maxLines="1"
android:text="Title"
android:textColor="#000"
android:textSize="22sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descriptionTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/titleTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="#+id/categoryTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<TextView
android:id="#+id/callTv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/imageView"
android:layout_alignTop="#id/descriptionTv"
android:layout_marginStart="81dp"
android:layout_marginLeft="73dp"
android:layout_marginTop="35dp"
android:text="This will be the description of the post" />
<ImageView
android:id="#+id/imgCall"
android:layout_width="19dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="false"
android:layout_marginStart="83dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="255dp"
android:clickable="true"
android:onClick="dialNumber"
app:srcCompat="#drawable/ic_call_black" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Replace
String phoneNumber = String.format("", textView.getText().toString().trim());
with
String phoneNumber = String.format("tel:%s", textView.getText().toString().trim());
As the first statement will produce an empty string.
I've battling a volley timeout error when attempting to post a registration form to a localhost (WAMP) for testing purposes. I've been looking at the code for sometime and cannot seem to find the issue. Timeout errors from what I understand are generally due to connection issues. I don't seem to be getting any errors via logcat, and the URL to my localhost php file seem to be correct.
I hope it's something small. In my db_function.php file, I did (include/connectiondb.php) but not sure if its correct. I originally had (connectiondb.php) but still no go.
Any assistance is appreciated.
All my code is below.
database details
CREATE TABLE jobseekerusers (
jobSeekerId int,
jobSeekerFirstName VARCHAR,
jobSeekerLastName VARCHAR,
jobSeekerEmaiLAddress VARCHAR,
jobSeekerPasswordVARCHAR,
);
connectdb.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'Discover');
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
?>
db_function.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$jobFirstName = $_POST['jobFirstName'];
$jobLastName = $_POST['jobLastName'];
$jobEmailAddress = $_POST['jobEmailAddress'];
$jobPassword = $_POST['jobPassword'];
require_once('include/connectdb.php');
$sql = "INSERT INTO jobseekerusers(jobSeekerFirstName, jobSeekerLastName,
jobSeekerEmailAddress, jobSeekerPassword)
values ('$jobFirstName', '$jobLastName', '$jobEmailAddress',
'$jobPassword')";
if(mysql_query($con,$sql))
{
echo "Success";
}
else
{
echo "Fail";
}
}
else
{
echo "Something Failed";
}
?>
job_seeker_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#drawable/shape_gradient_orange"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/search_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_full_name"
android:textColor="#FAFAFA"
android:textSize="20sp" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="350dp"
android:layout_height="320dp"
android:paddingBottom="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:background="#drawable/border"
android:id="#+id/cardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:padding="10dp"
android:text="#string/signUp"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/jobFirstName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:singleLine="true"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/rate"
android:drawablePadding="10dp"
android:hint="#string/employerFirstName"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobLastName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:singleLine="true"
android:layout_weight="1"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/employer_icon"
android:drawablePadding="10dp"
android:hint="#string/employerLastName"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobEmailAddress"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:layout_gravity="center"
android:singleLine="true"
android:layout_marginTop="15dp"
android:background="#FAFAFA"
android:layout_weight="1"
android:drawableLeft="#mipmap/email_icon"
android:drawablePadding="10dp"
android:hint="#string/employerEmailAddress"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:singleLine="true"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/password_icon"
android:drawablePadding="10dp"
android:hint="#string/employerPassword"
android:maxLines="1"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="#+id/cardView"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/jobSubmit"
android:layout_width="200dp"
android:layout_marginTop="10dp"
android:textColor="#FAFAFA"
android:layout_height="wrap_content"
android:text="Submit"
android:background="#drawable/shape_gradient_orange"/>
<TextView
android:layout_width="wrap_content"
android:id="#+id/jobHaveAccount"
android:layout_height="wrap_content"
android:textColor="#999"
android:padding="10dp"
android:text="#string/haveAccount"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
jobSeekerPage.java
package com.example.dennis.discover;
import android.app.DownloadManager;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
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.StringRequest;
import com.android.volley.toolbox.Volley;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.Map;
public class JobSeekerSignUp extends AppCompatActivity implements
View.OnClickListener {
private TextView jobHaveAccount;
private TextView jobFirstName;
private TextView jobLastName;
private TextView jobEmailAddress;
private TextView jobPassword;
private Button jobSubmit;
String URL = "http://192.168.0.9/Discover/db_functions.php";
public static final String FIRSTNAME = "jobSeekerFirstName";
public static final String LASTNAME = "jobSeekerLastName";
public static final String EMAILADDRESSID = "jobSeekerEmailAddress";
public static final String PASSWORD = "jobSeekerPassword";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_seeker_sign_up);
jobHaveAccount = (TextView) findViewById(R.id.jobHaveAccount);
jobFirstName = (TextView) findViewById(R.id.jobFirstName);
jobLastName = (TextView) findViewById(R.id.jobLastName);
jobEmailAddress = (TextView) findViewById(R.id.jobEmailAddress);
jobPassword = (TextView) findViewById(R.id.jobPassword);
jobSubmit = (Button) findViewById(R.id.jobSubmit);
jobSubmit.setOnClickListener(this);
jobHaveAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent jobSeekerSignIn = new Intent(JobSeekerSignUp.this,
JobSeekerLogIn.class);
startActivity(jobSeekerSignIn);
}
});
}
#Override
public void onClick(View view) {
final String firstName = jobFirstName.getText().toString().trim();
final String lastName = jobLastName.getText().toString().trim();
final String emailAddress = jobEmailAddress.getText().toString().trim();
final String password = jobPassword.getText().toString().trim();
//Initiate request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//get String Response using URL (variable defined above - currently WAMP)
StringRequest stringrequest = new StringRequest(Request.Method.POST,
URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(JobSeekerSignUp.this, response,
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(JobSeekerSignUp.this, error.toString(),
Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(FIRSTNAME, firstName);
params.put(LASTNAME, lastName);
params.put(EMAILADDRESSID, emailAddress);
params.put(PASSWORD, password);
return super.getParams();
}
};
// Adding the request to the request queue
requestQueue.add(stringrequest);
}
}
Try disabling your firewall if on windows, or disabling Avast shields. It worked for me.
I have created my first simple currency converter app which takes INR as input and which gives amount in USD as Toast. The app works fine and returns the correct result when an amount is given as input and the "Convert" button is pressed, but as soon as no input is given and then the "Convert" button is pressed, the app crashes.
Here is the code for the .xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:background="#color/colorPrimary"
android:fontFamily="monospace"
android:padding="20sp"
android:text="INR to USD converter"
android:textColor="#android:color/background_light"
android:textSize="18sp" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:background="#color/colorPrimary"
android:onClick="convert"
android:text="Convert" />
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
app:srcCompat="#drawable/currency" />
<EditText
android:id="#+id/amount"
style="#style/Widget.AppCompat.Light.AutoCompleteTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button"
android:layout_centerHorizontal="true"
android:layout_marginBottom="34dp"
android:ems="10"
android:hint="Amount(INR)"
android:inputType="numberDecimal" />
</RelativeLayout>
And code for the main activity:
package com.example.asus.currencyconverter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
public void convert(View view){
EditText amount = (EditText) findViewById(R.id.amount);
Double amountDouble= Double.parseDouble(amount.getText().toString());
Double dollar= amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$" + dollar.toString(), Toast.LENGTH_LONG).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
}
}
What am I doing wrong?
Replace with this code:
public class MainActivity extends AppCompatActivity {
public EditText amount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main1);
amount = (EditText) findViewById(R.id.amount);
}
public void convert(View view) {
String text = amount.getText().toString();
if (!text.isEmpty()) {
Double amountDouble = Double.parseDouble(text);
Double dollar = amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$"+dollar.toString(), Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Please enter a valid value", Toast.LENGTH_LONG).show();
}
}
}
It will show a toast to enter a value if it is empty.
That's because when you have empty EditText and call getText() it returns empty string which you put to Double.parseDouble(), which raises NumberFormatException. You want to check first is your String is empty.
public void convert(View view) {
EditText amount = (EditText) findViewById(R.id.amount);
String textAmount = amount.getText().toString();
if (!TextUtils.isEmpty(textAmount)) {
Double amountDouble = Double.parseDouble(textAmount);
Double dollar= amountDouble * 0.01575;
Toast.makeText(MainActivity.this, "$" + dollar.toString(), Toast.LENGTH_LONG).show();
}
}