I want to store simple form data in database with Sqlite. Application gets install and also show activity layout but when we fill and click on submit button then application shows unfortunately apk has stoped.where i am getting wrong in code?
My Main.class
import android.app.Activity;
import android.content.Intent;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class Main extends Activity{
Intent i=null;
EditText tv1,tv2;
SQLiteDatabase db=null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (EditText) findViewById(R.id.name);
tv2 = (EditText) findViewById(R.id.altnumber);
db = openOrCreateDatabase("mydb", MODE_PRIVATE, null);
db.execSQL("create table if not exists login(name varchar,altnumber varchar)");}
public void action(View v) {
String name = tv1.getText().toString();
String altnumber = tv2.getText().toString();
db.execSQL("insert into login values('" + name + "','" + altnumber + "')");
i = new Intent(this, Welcome.class);
startActivityForResult(i, 500);
db.close();
finish();
}
}
activity_main.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:layout_width="match_parent"
android:layout_height="match_parent"
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.milind.splach.Main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="30dp"
android:text="Data Base" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorials Point"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:textSize="35dp"
android:textColor="#ff16ff01" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text="Name"
android:ems="10"
android:id="#+id/name"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/altnumber"
android:layout_below="#+id/name"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_below="#+id/altnumber"
android:layout_centerHorizontal="true"
android:onClick="action" />
</RelativeLayout>
Related
I am learning java and trying to register user's info in firebase realtime database when they register in the app ,but the data doesn't get stored in the database and the logcat shows no error so I can't figure out what's the problem.
P.S. - App is connected to the database & firebase database rules are:
{
"rules": {
".read": true,
".write": true
}
}
here's the SignUp.java file code
package com.example.foodapp;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.android.material.textfield.TextInputEditText;
import com.google.android.material.textfield.TextInputLayout;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUp extends AppCompatActivity {
//variables
private TextInputLayout regName,regUsername,regEmail,regPhoneNo,regPassword;
private Button regBtn,regToLogInBtn;
FirebaseDatabase rootNode;
DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
//hooks
regName = (TextInputLayout )findViewById(R.id.reg_name);
regUsername = (TextInputLayout )findViewById(R.id.reg_username);
regEmail = (TextInputLayout )findViewById(R.id.reg_email);
regPhoneNo = (TextInputLayout )findViewById(R.id.reg_phoneNo);
regPassword = (TextInputLayout )findViewById(R.id.reg_password);
regBtn = (Button)findViewById(R.id.reg_btn);
regToLogInBtn = (Button)findViewById(R.id.reg_login_btn);
//save data in firebase using button click
regBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rootNode = FirebaseDatabase.getInstance();
reference = rootNode.getReference("users");
Log.d("HELLO", "Register Button Clicked");
//get all value from text fields
String name = regName.getEditText().getText().toString();
String username = regUsername.getEditText().getText().toString();
String email = regEmail.getEditText().getText().toString();
String phoneNo = regPhoneNo.getEditText().getText().toString();
String password = regPassword.getEditText().getText().toString();
UserHelperClass helperClass = new UserHelperClass(name, username, email, phoneNo, password);
reference.child(phoneNo).push().setValue(helperClass);
}
});
}
}
the xml codes are:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<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"
tools:context=".SignUp"
android:orientation="vertical"
android:background="#fff"
android:padding="20dp">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/logo"
android:transitionName="logo_name"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome"
android:textSize="40sp"
android:fontFamily="#font/bungee"
android:layout_marginTop="-20dp"
android:textColor="#000"
android:transitionName="logo_text"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SignUp to start your new Journey"
android:textSize="18sp"
android:transitionName="logo_desc"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_name"
android:hint="Full Name"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
>
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_username"
app:counterMaxLength="15"
android:hint="Username"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="username_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
</com.google.android.material.textfield.TextInputLayout>
//--------------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_email"
android:hint="Email"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>
//-----------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_phoneNo"
android:hint="Phone Number"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number" />
</com.google.android.material.textfield.TextInputLayout>
//---------------------------------------------------------
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/reg_password"
android:hint="Password"
app:passwordToggleEnabled="true"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:transitionName="password_tran">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<Button
android:id="#+id/reg_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000"
android:text="GO"
android:textColor="#fff"
android:transitionName="button_tran" />
<Button
android:id="#+id/reg_login_btn"
style="?android:attr/borderlessButtonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:text="Already have an account? Login"
android:textColor="#000"
android:transitionName="login_signup_tran" />
</LinearLayout>
</ScrollView>
```
the database is:
[1]: https://i.stack.imgur.com/IlaDs.png
you can refer to this question, after reading those answers, the reason of the issue is either you verification id or code is null or empty SHA certificates is not available on firebase app (console)
Exception when authenticating with wrong verification code
I am making a simple app in which context view changes and then a toast message is displayed as input given by user. I don't understand why the app keeps on crashing when changing the context view.
Also Android studio gives this warning:
Method "Toaster" is missing in "FirstActivity" or has incorrect signature.
Here is my code:
activity_me_clicked.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:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/welcome"
android:textSize="36sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:paddingLeft="7dp"
android:paddingRight="7dp"
android:text="#string/intro"
android:textSize="24sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<EditText
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="7dp"
android:layout_marginEnd="7dp"
android:layout_marginTop="10dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/named"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="MainProcess"
android:text="#string/done" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
tools:ignore="UselessLeaf"></LinearLayout>
</LinearLayout>
</LinearLayout>
FirstActivity.Java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class FirstActivity extends AppCompatActivity {
public static String owner_string;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_me_clicked);
}
public void MainProcess(View view) {
final String TAG="DEBUG";
Log.d(TAG,"At least process started");
EditText owner = (EditText) findViewById(R.id.name);
owner_string = owner.getText().toString();
Log.d(TAG,"owner name stored");
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
setContentView(R.layout.activity_main_screen);
Log.d(TAG,"content shown");
}
}
activity_main_screen.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/welcome"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/what_would_you_like_me_to_do_today"
android:textSize="18sp" />
<Button
android:id="#+id/Cam"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/camera" />
<Button
android:id="#+id/Mus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/music" />
<Button
android:id="#+id/QR"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/scanQR" />
<EditText
android:id="#+id/toastText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/order"
android:inputType="textPersonName" />
<Button
android:id="#+id/toaster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:onClick="toaster"
android:padding="0dp"
android:paddingTop="15dp"
android:text="#string/toast"
android:layout_marginEnd="100dp"
android:layout_gravity="center"/>
</LinearLayout>
MainScreen.java:
package com.example.nautatvanavlakha.abcd;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class MainScreen extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_screen);
}
public void toaster(View view){
EditText toast = (EditText)findViewById(R.id.toastText);
String final_toast = toast.getText().toString();
Toast.makeText(getApplicationContext(), final_toast, Toast.LENGTH_SHORT).show();
}
}
EDIT: As suggested, I moved the toaser function to FirstActivity.Java and deleted the MainScreen.java file as it becomes pointless to keep it. But the major problem is when I press the button (id named) the app keeps stopping.
EDIT2: I found that setContentView(R.layout.activity_main_screen) in FirstActivity.Java needs to be above this code
TextView textView = (TextView) findViewById(R.id.welcome);
textView.setText("Hi " + owner_string + ".");
Log.d(TAG,"owner name is set");
so that Activity has access to all the layout components. Thanks solved :)
You replaced the content view in the first activity with a layout that include the onClick attribute, but you have no public void toaster(View view) method there.
So, either don't use setContentView a second time, or implement that method on both Activities.
The recommended way to replace the view is Fragments, by the way
This is a wrong way to change the contents of your Activity, The main problem here is that you have onClick attribute set to "toaster", And you don't have a function called "toaster" in your FirstActivity.
Beside that, The MainScreen Activity in your code will never be used.
So, Your problem is that you set the FirstActivity contents to "activity_main_screen.xml", And FirstActivity doesn't have "toaster" method in it, When you change the context, the Activity will try to find toaster method and the app will crash because the method doesn't exist in FirstActivity.
You can solve this problem by making a method called "toaster" inside FirstActivity, But
This is a very bad practice, You can use Fragments instead, Or you can use Intent to move to another activity.
Some useful links about Fragments:
https://developer.android.com/guide/components/fragments.html
https://www.tutorialspoint.com/android/android_fragments.htm
https://examples.javacodegeeks.com/android/core/app/fragment/android-fragments-example/
Why should I use fragment in Android?
I've been following this tutorial: https://www.youtube.com/watch?v=sKCdqCuFFFk
I completed the tutorial, but when I wanted to launch, it gave an error. These are my codes:
(MainActivity.java)
package com.example.admir_000.jdsfnjls;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView tvRegisterHere = (TextView) findViewById(R.id.tvRegisterHere);
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
}
}
and this is my 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_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
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.example.admir_000.jdsfnjls.MainActivity">
<TextView
android:text="Register here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bLogin"
android:layout_centerHorizontal="true"
android:id="#+id/tvRegisterHere" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/etPassword"
android:hint="Password"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/etUsername"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Username" />
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bLogin"
android:layout_below="#+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Does anyone know how to fix this?
I am sure, without read the logcat, that you got a Null Pointer Exception.... why?
registerLink.setOnClickListener....
Where did you initialized registerLink variable? Maybe you wanted to put tvRegisterHere.setOnClickListener...
P.S: I suggest you to already read the stacktrace and then search for the particular Exception
when I click on Logout button it goes to the next activity but, On the next activity when I click on the "Register here" to go to the next activity it says unfortunately , Login has stopped. Please help me out.
Main Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button blogout;
EditText uname1, name1, age1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname1 =(EditText) findViewById(R.id.uname1);
name1 = (EditText) findViewById(R.id.name1);
age1 = (EditText) findViewById(R.id.age1);
blogout = (Button) findViewById(R.id.blogout);
blogout.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.blogout:
startActivity(new Intent(this, Login.class));
break;
}
}
}
Login Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends AppCompatActivity implements View.OnClickListener {
Button blogin;
EditText uname, pass;
TextView registerLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
uname =(EditText) findViewById(R.id.uname);
pass = (EditText) findViewById(R.id.pass);
registerLink = (TextView) findViewById(R.id.registerLink);
blogin = (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.registerLink:
startActivity(new Intent(this, Register.class));
break;
case R.id.blogin:
break;
}
}
}
Register Acitvity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bregister;
EditText uname2, pass1, name2, age2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
uname2 =(EditText) findViewById(R.id.uname2);
pass1 = (EditText) findViewById(R.id.pass1);
name2 = (EditText) findViewById(R.id.name2);
age2 = (EditText) findViewById(R.id.age2);
bregister = (Button) findViewById(R.id.blogin);
bregister.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bregister:
break;
}
}
}
activity_login layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/blogin"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
activity_main layout file
<?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"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname2"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:id="#+id/blogout"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
activity_register layout file
<?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"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass1"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/bregister"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.namrata.login">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
In register activity xml you have android:id="#+id/bregister"
but in RegisterActivity you searching for login button
bregister = (Button) findViewById(R.id.blogin);
which is null that is why you are getting null pointer exception i think change it to
bregister = (Button) findViewById(R.id.bregister);
In your layout file for register activity (activity_register) you have:
android:id="#+id/bregister"
but in Register Activity.java you're doing:
bregister = (Button) findViewById(R.id.blogin);
so register is set to null. Hence it crashes on:
bregister.setOnClickListener(this);
Your onClick method in MainActivity is not the overrided one that's why it's not getting called automatically.Create it as you have create onClick method in LoginActivity
In your First Activity class MainActivity you are using this layout class
setContentView(R.layout.activity_main)
You should use
setContentView(R.layout.activity_login)
and in this activity_login layout you should
change TextView is Register here To Button
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
Remove this and use button instead of TextView.
I am building a very simple currency converter app for a course I am doing.
The App has a decimal Edit text box where you enter the amount and when you press the button it will convert the amount from £ sterling to U.S dollars and gives the user the difference in a Toast message.
I need help with the java code for the currency conversion part , if anyone can help that will be much appreciated.
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:layout_width="match_parent"
android:layout_height="match_parent"
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.example.hydraquip_10.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:src="#drawable/currency"
android:scaleType="center" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="134dp"
android:layout_below="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Get Result"
android:id="#+id/button"
android:layout_marginTop="35dp"
android:onClick="changeCurrency"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter amount £"
android:id="#+id/textView"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="56dp"
android:gravity="center" />
</RelativeLayout>
main java
package com.example.hydraquip_10.myapplication;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void changeCurrency(View view){
EditText changeNumber = (EditText) findViewById(R.id.editText);
Toast.makeText(getApplicationContext(), "Currency = $" + changeNumber.getText().toString(),Toast.LENGTH_LONG).show();
}
}