My Activity comes out as a Blank even though it shouldn't? - java

I'm a complete beginner with coding and decided to try out android studios for fun without having formal training or lessons for the basics so I'm sorry if this seems like a dumb problem but whenever I try to run my code it just comes off as blank? Any idea how to solve this?
Here's a screenshot of what comes up
package com.example.trtalpha17;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import org.jetbrains.annotations.NotNull;
public class Register_Tutor extends AppCompatActivity {
private static final String[] SCHOOLS = new String[]{
"A", "B", "C"
};
private static final String[] COURSE = new String[]{
"Bachelor of Science in Social Work (BS Social Work)",
"Bachelor of Science in Marine Transportation (BSMT)",
"Bachelor of Science in Food Technology (BS Food Tech)",
"Bachelor of Science in Nutrition and Dietetics (BS Nutrition and Dietetics)"
};
private static final String[] EDUCA = new String[]{
"Grade 9", "Grade 10", "Grade 11", "Grade 12", "1st Year College", "2nd Year College", "3rd Year College", "4th Year College", "5th Year College", "Graduate"
};
private static final String[] TITLES = new String[]{
"Mr.", "Ms.", "Mrs.", "Mx."
};
EditText metEmail, metPhone, metPassword, metPasswordconfirm, metNamefirst, metNamelast;
Button mbutton;
ProgressBar progressBar2;
FirebaseAuth fAuth;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_tutor2);
final AutoCompleteTextView metTitle = (AutoCompleteTextView)findViewById(R.id.etTitle);
final AutoCompleteTextView mautoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView);
final AutoCompleteTextView meduc = (AutoCompleteTextView) findViewById(R.id.educ);
final AutoCompleteTextView metcourse = (AutoCompleteTextView) findViewById(R.id.etcourse);
metTitle.setThreshold(2);
mautoCompleteTextView.setThreshold(1);
meduc.setThreshold(1);
metcourse.setThreshold(1);
ImageView marrow = (ImageView) findViewById(R.id.arrow);
ArrayAdapter<String> adapterarrow = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, TITLES);
metTitle.setAdapter(adapterarrow);
ArrayAdapter<String> adapterschools = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, SCHOOLS);
mautoCompleteTextView.setAdapter(adapterschools);
ArrayAdapter<String> adapteredu = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, EDUCA);
meduc.setAdapter(adapteredu);
ArrayAdapter<String> adaptercourse = new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, COURSE);
metcourse.setAdapter(adaptercourse);
marrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
metTitle.showDropDown();
}
});
metNamefirst = findViewById(R.id.etNamefirst);
metNamelast = findViewById(R.id.etNamelast);
metEmail = findViewById(R.id.etEmail);
metPhone =findViewById(R.id.etPhone);
metPassword = findViewById(R.id.etPassword);
metPasswordconfirm =findViewById(R.id.etPasswordconfirm);
mbutton = findViewById(R.id.button);
progressBar2 = findViewById(R.id.progressBar2);
fAuth = FirebaseAuth.getInstance();
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
mbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String email = metEmail.getText().toString().trim();
String password = metPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
metEmail.setError("Email is required.");
return;
}
if(TextUtils.isEmpty(password)){
metPassword.setError("Password is required.");
return;
}
if(password.length()<6){
metPassword.setError("Password length must be at least 6 characters.");
return;
}
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull #NotNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(Register_Tutor.this, "Registration complete.", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}else {
Toast.makeText(Register_Tutor.this, "There was an error making your that request. Please try again later." + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
}
}
LAYOUT
<?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"
tools:context=".Register_Tutor">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:fontFamily="cursive"
android:text="Register as a Tutor"
android:textColor="#000000"
android:textSize="28sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TableRow
android:id="#+id/tableRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2">
<AutoCompleteTextView
android:id="#+id/etTitle"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:drawableLeft="#drawable/ic_baseline_person_24"
android:drawablePadding="5dp"
android:hint="Title"
android:inputType="textPersonName" />
<ImageView
android:id="#+id/arrow"
android:layout_width="16dp"
android:layout_height="45dp"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:drawablePadding="5dp"
android:src="#mipmap/outline_arrow_drop_down_black_18" />
<EditText
android:id="#+id/etNamefirst"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:hint="First name"
android:inputType="textPersonName" />
<EditText
android:id="#+id/etNamelast"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:hint="Last name"
android:inputType="textPersonName" />
</TableRow>
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="#+id/tableRow">
<EditText
android:id="#+id/etEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:drawableLeft="#drawable/ic_baseline_email_24"
android:drawablePadding="5dp"
android:hint="Email address"
android:inputType="textEmailAddress" />
<EditText
android:id="#+id/etPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:drawableLeft="#drawable/ic_baseline_local_phone_24"
android:drawablePadding="5dp"
android:hint="Phone number"
android:inputType="phone" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear1">
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:completionThreshold="1"
android:drawableLeft="#drawable/ic_baseline_school_24"
android:drawablePadding="5dp"
android:hint="School" />
<AutoCompleteTextView
android:id="#+id/educ"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:completionThreshold="1"
android:drawableLeft="#drawable/ic_baseline_article_24"
android:drawablePadding="5dp"
android:hint="Educational Attainment" />
<AutoCompleteTextView
android:id="#+id/etcourse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:completionThreshold="1"
android:drawableLeft="#drawable/ic_baseline_article_24"
android:drawablePadding="5dp"
android:hint="Course" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintTop_toBottomOf="#+id/linear2">
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:drawableLeft="#drawable/ic_baseline_lock_24"
android:drawablePadding="5dp"
android:hint="Password"
android:inputType="textPassword" />
<EditText
android:id="#+id/etPasswordconfirm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:drawableLeft="#drawable/ic_baseline_lock_open_24"
android:drawablePadding="5dp"
android:hint="Confirm Password"
android:inputType="textPassword" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="10dp"
android:background="#drawable/button"
android:gravity="center"
android:hint="SIGN UP"
android:textColorHint="#FDFCFC"
app:backgroundTint="#315E8C" />
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.trtalpha17">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/Theme.TRTAlpha17"
>
<activity android:name=".Register_Tutor">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Login" />
<activity android:name=".RegisterStudent" />
<activity android:name=".RegistrationHome" />
<activity android:name=".MainActivity" />
</application>
</manifest>

Did you clean and rebuild your project or Invalid caches/restart from IDE?You will find it in under File menu.
It seems like your code is fine , so I decided to run in IDE its compiling and running perfectly .
Firebase console-
So you can do some checks in your side -
1.
setContentView(R.layout.activity_register_tutor2);
is layout.activity_register_tutor2 your main/default layout? Or any other layout`?
2.
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
If you already sign in at least one time , this code will take you in MainActivity . Is that blank activity MainActivity? Make sure . You did not share your layout file of MainActivity , so I'm not sure .
You can Implement logout to test/debug your app easier by -
private Button mLogOutBtn;
mLogOutBtn = findViewById(R.id.log_out_btn);
mAuth = FirebaseAuth.getInstance();
mLogOutBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mAuth.signOut();
startActivity(new Intent(MainActivity.this, Register_Tutor.class));
finish();
}
});

Related

Android java, Side drawer menu is showing up whenever the app is launched and the app is getting crashed on closing the menu

java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
The terminal shows that the error is with the onOptionsItemSelected function
I have referred many StackOverflow posts regarding errors with side navigation drawer but I couldn't find the solution
Logcat
2022-05-08 17:13:15.175 867-867/com.example.books E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.books, PID: 867
java.lang.IllegalArgumentException: No drawer view found with gravity LEFT
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1736)
at androidx.drawerlayout.widget.DrawerLayout.openDrawer(DrawerLayout.java:1722)
at androidx.appcompat.app.ActionBarDrawerToggle.toggle(ActionBarDrawerToggle.java:287)
androidx.appcompat.app.ActionBarDrawerToggle.onOptionsItemSelected(MainActivity.java:245)
at com.example.books.MainActivity.onOptionsItemSelected(MainActivity.java:245)
at android.app.Activity.onMenuItemSelected(Activity.java:4324)
at androidx.fragment.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:352)
at androidx.appcompat.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:264)
at androidx.appcompat.view.WindowCallbackWrapper.onMenuItemSelected
at androidx.appcompat.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:188)
at android.view.View.performClick(View.java:8160)
at android.view.View.performClickInternal(View.java:8137)
at android.view.View.access$3700(View.java:888)
at android.view.View$PerformClick.run(View.java:30250)
at android.os.Handler.handleCallback(Handler.java:938)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:246)
at android.app.ActivityThread.main(ActivityThread.java:8443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:596)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1130)
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout
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_gravity="left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/dl">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<SearchView
android:id="#+id/searchView"
android:layout_width="277dp"
android:layout_height="34dp"
android:layout_alignParentTop="true"
android:background="#drawable/bg_white_rounded"
android:iconifiedByDefault="false"
android:queryHint="Search book,title,author, etc.."
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.119"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.035"
app:queryBackground="#drawable/bg_white_rounded"
app:submitBackground="#drawable/bg_white_rounded"
tools:ignore="MissingConstraints">
</SearchView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView2"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:contentPadding="#dimen/cardview_default_elevation"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage5"
android:layout_marginLeft="10dp"
android:layout_width="76dp"
android:layout_height="100dp"
android:src="#drawable/bookdonation" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.136"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131">
<ImageView
android:id="#+id/idIVCourseImage1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:clickable="true"
android:src="#drawable/onlinepurchase" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardView3"
android:layout_width="96dp"
android:layout_height="100dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:cardMaxElevation="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.865"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.131" >
<ImageView
android:id="#+id/idIVCourseImage2"
android:layout_marginTop="10dp"
android:layout_width="96dp"
android:layout_height="80dp"
android:src="#drawable/prouddonars" />
</androidx.cardview.widget.CardView>
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FICTION AND NON-FICTION BOOKS"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.115"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.519" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/idRVCourse"
android:layout_width="403dp"
android:layout_height="333dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.961"
tools:ignore="MissingConstraints">
</androidx.recyclerview.widget.RecyclerView>
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Purchase books"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.122"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276" />
<TextView
android:id="#+id/textView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/inter_medium"
android:text="Donate books"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.276"
android:clickable="true"/>
<TextView
android:id="#+id/textView11"
android:layout_width="89dp"
android:layout_height="17dp"
android:fontFamily="#font/inter_medium"
android:text="Proud donars"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.869"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.275" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="407dp"
android:layout_height="148dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.401"
tools:ignore="MissingConstraints">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="120dp"
android:orientation="horizontal">
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="99dp"
android:layout_height="99dp"
android:src="#drawable/medical"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="MEDICAL HEALTH AND SCIENCE BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Biochemistry| Physiotherapy | Phathalogy"></TextView>
<Button
android:id="#+id/buttonmed"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order "
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:layout_marginBottom="10dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/fictionandnonfiction"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="EXPLORE THE WIDE RANGE OF FICTION AND NON FICTION"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Science Fiction| Short stories | Teens"></TextView>
<Button
android:id="#+id/buttoneng"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/ncert"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text="NCERT BOOKS FOR CLASS 1-12"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Physics| Chemistry | Mathematics"></TextView>
<Button
android:id="#+id/buttonfiction"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="408dp"
android:layout_height="136dp"
android:elevation="10dp"
app:cardCornerRadius="8dp"
app:cardMaxElevation="10dp"
app:cardPreventCornerOverlap="true"
app:cardUseCompatPadding="true">
<ImageView
android:layout_width="96dp"
android:layout_height="wrap_content"
android:src="#drawable/competative"></ImageView>
<TextView
android:layout_width="210dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="15dp"
android:text=" WIDE RANGE OF AVAILABLE COMPITATIVE EXAM BOOKS"></TextView>
<TextView
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="110dp"
android:layout_marginTop="50dp"
android:text="Banking| UPSC| SSC"></TextView>
<Button
android:id="#+id/buttonfinance"
android:layout_width="77dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="55dp"
android:layout_marginRight="10dp"
android:fontFamily="#font/inter"
android:text="Order"
android:textAllCaps="false"
android:textSize="10sp" />
</androidx.cardview.widget.CardView>
</LinearLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.777"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_favorite" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="36dp"
android:layout_height="29dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.045"
app:srcCompat="#drawable/cart1" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="32dp"
android:layout_height="27dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/linearLayout"
app:layout_constraintHorizontal_bias="0.889"
app:layout_constraintStart_toStartOf="#+id/linearLayout"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:srcCompat="#drawable/ic_location" />
<com.google.android.material.navigation.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/idnav_bar"
android:layout_gravity="left"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu"
tools:ignore="InvalidId,MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
Mainactivity.java
package com.example.books;
import android.content.Intent;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.widget.Toolbar;
import com.google.android.material.navigation.NavigationView;
import com.synnapps.carouselview.CarouselView;
import com.synnapps.carouselview.ImageClickListener;
import com.synnapps.carouselview.ImageListener;
import androidx.annotation.NonNull;
import androidx.appcompat.app.ActionBar;
import androidx.appcompat.app.ActionBarDrawerToggle;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.view.GravityCompat;
import androidx.drawerlayout.widget.DrawerLayout;
import androidx.recyclerview.widget.DefaultItemAnimator;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import android.view.MenuItem.OnMenuItemClickListener;
import java.util.Objects;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
private Button button;
private Button medbutton;
private RecyclerView courseRV;
public DrawerLayout dl;
private Toolbar toolbar;
public ActionBarDrawerToggle abdt;
public TextView TVdonars;
public TextView TVdonatebooks;
public TextView TVpurchasebooks;
public ImageView wishlistimg;
private ArrayList<CourseModel> courseModelArrayList;
private int[] mImages = new int[]{
R.drawable.academic, R.drawable.engineering, R.drawable.medical, R.drawable.fiction
};
private String[] mImagesTitle = new String[]{
"Academic", "Engineering", "Medical", "Fiction"
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setTitle("Classic Books Donar");
medbutton = findViewById(R.id.buttonmed);
wishlistimg = findViewById(R.id.imageView5);
//button = (Button) findViewById(R.id.button2);
courseRV = findViewById(R.id.idRVCourse);
TVpurchasebooks = findViewById(R.id.textView9);
TVdonatebooks = findViewById(R.id.textView10);
TVdonars = findViewById(R.id.textView11);
TVdonatebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Register.class);
startActivity(intent);
}
});
TVdonars.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Prouddonars.class);
startActivity(intent);
}
});
TVpurchasebooks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Purchasebooks.class);
startActivity(intent);
}
});
final NavigationView nav_view = (NavigationView) findViewById(R.id.idnav_bar);
nav_view.setNavigationItemSelectedListener(new
NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.select_catogery) {
Toast.makeText(MainActivity.this, "Select Catogery",
Toast.LENGTH_SHORT).show();
}
if (id == R.id.engineering) {
Toast.makeText(MainActivity.this, "Engineering", Toast.LENGTH_SHORT).show();
}
if (id == R.id.medical) {
Toast.makeText(MainActivity.this, "Medical", Toast.LENGTH_SHORT).show();
}
return true;
}
});
dl = (DrawerLayout) findViewById(R.id.dl);
abdt = new ActionBarDrawerToggle(this, dl, toolbar, R.string.open, R.string.close);
abdt.setDrawerIndicatorEnabled(true);
//Toolbar toolbar = findViewById(R.id.toolbar);
//setSupportActionBar(toolbar);
dl.addDrawerListener(abdt);
abdt.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
courseModelArrayList = new ArrayList<>();
courseModelArrayList.add(new CourseModel("Design of Machine Elements, Fourth Edition", "V
B Bhandari", R.drawable.designofmachineelements));
courseModelArrayList.add(new CourseModel("Fluid Mechanics, Sixth Edition", "Munsun,
Okilshi,Huebsch & Rothmayer", R.drawable.fluidmechanics));
courseModelArrayList.add(new CourseModel("Fundamentals of Thermodynamics", "Claus
Borgnakke & Richard E Sonntag", R.drawable.thermodynamics));
courseModelArrayList.add(new CourseModel("Shigley's Mechanical Engineering Design",
"Richard G Budynas & JJ Keith Nisbett", R.drawable.mechanicalengineeringdesign));
courseModelArrayList.add(new CourseModel("Atlas of Human Anatomy, Seventh Edition", "Frank
H. Netter, MD ", R.drawable.atlasofhumananatomy));
courseModelArrayList.add(new CourseModel("Data structures and algorithms", " Alfred V.Aho,
John E.Hopcroft, Jeffrey D.Ullman", R.drawable.datastructures));
courseModelArrayList.add(new CourseModel("Textbook of Clinical Neuroanatomy, Third
Edition", "Vishram Singh ", R.drawable.clinicalneuroanatomy));
// we are initializing our adapter class and passing our arraylist to it.
CourseAdapter courseAdapter = new CourseAdapter(this, courseModelArrayList);
// below line is for setting a layout manager for our recycler view.
// here we are creating vertical list so we will provide orientation as vertical
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this,
LinearLayoutManager.VERTICAL, false);
// in below two lines we are setting layoutmanager and adapter to our recycler view.
courseRV.setLayoutManager(linearLayoutManager);
courseRV.setAdapter(courseAdapter);
medbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Medical.class);
startActivity(intent);
}
});
wishlistimg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MainActivity.this, Wishlist.class);
startActivity(intent);
}
});
#Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();
if (abdt.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

App Crash when i click button to change activity [duplicate]

This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Null pointer Exception - findViewById()
(12 answers)
Closed 4 years ago.
I'm sorry if i'm not so accurate but this is my first app.
The problem is: when i press a button to change activity the app crash.
Can someone help me? Thanks for all!!
If you wanna have more information tell me and I try to explain better.
CODE:
MAIN ACTIVITY
package com.example.principale.designazioni;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button invia = (Button) findViewById(R.id.buttInvia);
invia.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivityPage1();
}
});
}
public void openActivityPage1() {
Intent intent = new Intent (getApplicationContext(), Page1.class);
startActivity(intent);
}
}
XML MAIN ACTIVITY
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".MainActivity"
tools:layout_editor_absoluteY="73dp">
<ImageView
android:id="#+id/imageView4"
android:layout_width="231dp"
android:layout_height="229dp"
android:layout_marginEnd="8dp"
android:adjustViewBounds="false"
android:contentDescription='#string tools:ignore="ContentDescription"'
android:cropToPadding="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.146"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04"
app:srcCompat="#mipmap/logo_grigio" />
<TextView
android:id="#+id/textView2"
android:layout_width="272dp"
android:layout_height="51dp"
android:fontFamily="monospace"
android:text="Disponibilità"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.818"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4"
app:layout_constraintVertical_bias="0.093" />
<Button
android:id="#+id/buttInvia"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Invia"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#android:color/holo_orange_dark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.482" />
</android.support.constraint.ConstraintLayout>
ACTIVITY PAGE1
package com.example.principale.designazioni;
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;
public class Page1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page1);
EditText mail = (EditText) findViewById(R.id.editTextMail);
EditText nome = (EditText) findViewById(R.id.editTextNome);
EditText cognome = (EditText) findViewById(R.id.editTextCognome);
EditText tessera = (EditText) findViewById(R.id.editTextTessera);
Button avanti = (Button) findViewById(R.id.buttInvia);
Bundle bundle = new Bundle();
bundle.putString("mail", mail.getText().toString());
bundle.putString("nome", nome.getText().toString());
bundle.putString("cognome", cognome.getText().toString());
bundle.putString("tessera", tessera.getText().toString());
avanti.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActivityPage2();
}
});
}
public void openActivityPage2() {
Intent intent = new Intent (getApplicationContext(), Page2.class);
startActivity(intent);
}
}
XML ACTIVITY PAGE1
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".Page1">
<TextView
android:id="#+id/textView3"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="E-Mail"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06" />
<TextView
android:id="#+id/textView4"
android:layout_width="96dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Cognome"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.102"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.44" />
<TextView
android:id="#+id/textView6"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Numero di Tessera"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.629" />
<TextView
android:id="#+id/textView5"
android:layout_width="71dp"
android:layout_height="27dp"
android:fontFamily="monospace"
android:text="Nome"
android:textAlignment="textStart"
android:textColor="#android:color/holo_orange_dark"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.093"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.251" />
<EditText
android:id="#+id/editTextMail"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:fontFamily="monospace"
android:inputType="textEmailAddress"
android:textColor="#android:color/background_light"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextCognome"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextNome"
app:layout_constraintVertical_bias="0.17" />
<EditText
android:id="#+id/editTextNome"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/textView4"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editTextTessera"
android:layout_width="306dp"
android:layout_height="50dp"
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="monospace"
android:text="Avanti"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editTextTessera"
app:layout_constraintVertical_bias="0.487" />
</android.support.constraint.ConstraintLayout>
ACTIVITY PAGE2
package com.example.principale.designazioni;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Page2 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_page2);
final TextView nome = (TextView) findViewById(R.id.textViewPage2);
Bundle bundle = this.getIntent().getExtras();
nome.setText(bundle.getString("nome"));
}
}
XML ACTIVITY PAGE2
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".Page2">
<TextView
android:id="#+id/textViewPage2"
android:layout_width="180dp"
android:layout_height="47dp"
android:fontFamily="monospace"
android:text="TextView"
android:textAlignment="center"
android:textColor="#android:color/holo_orange_dark"
android:textSize="30sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
ANDROID MANIFEST
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.principale.designazioni">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Page1" />
<activity android:name=".Page2"></activity>
</application>
</manifest>
STACK TRACE
08-24 10:21:07.370 6819-6819/com.example.principale.designazioni E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.principale.designazioni, PID: 6819
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.principale.designazioni/com.example.principale.designazioni.Page1}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302)
at android.app.ActivityThread.-wrap12(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891)
at android.os.Handler.dispatchMessage(Handler.java:108)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7425)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.principale.designazioni.Page1.onCreate(Page1.java:30)
at android.app.Activity.performCreate(Activity.java:7372)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1218)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3147)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3302) 
at android.app.ActivityThread.-wrap12(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1891) 
at android.os.Handler.dispatchMessage(Handler.java:108) 
at android.os.Looper.loop(Looper.java:166) 
at android.app.ActivityThread.main(ActivityThread.java:7425) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921) 
Change this line in Page1.java
from
Button avanti = (Button) findViewById(R.id.buttInvia);
to
Button avanti = (Button) findViewById(R.id.button);

unable to get value from edittext into string

I'm trying to make an android app which will show a Toast on button click. That Toast contains the number entered by the user in edittext field. The problem is that i am entering text to edittext(Numeric) field and on button click, Toast isn't showing the text entered by me. Toast is completely blank.
Here is my code:-
Activity.java
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.EditText;
import android.widget.Toast;
public class login extends AppCompatActivity {
String username, password;
Button payNGO, payGO;
EditText usernameField, passwordField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS, WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
setContentView(R.layout.activity_login);
payNGO = (Button) findViewById(R.id.payngo);
payGO = (Button) findViewById(R.id.paygo);
usernameField = (EditText) findViewById(R.id.forno);
passwordField = (EditText) findViewById(R.id.dob);
username = usernameField.getText().toString();
password = passwordField.getText().toString();
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
}
}
Activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context=".login"
android:background="#drawable/back">
<ImageView
android:id="#+id/imageView"
android:layout_width="150dip"
android:layout_height="150dip"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/forno"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.438"
app:srcCompat="#drawable/ico" />
<EditText
android:id="#+id/forno"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="20dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Number"
android:inputType="number"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/dob"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/dob"
android:layout_width="match_parent"
android:layout_height="40dip"
android:layout_marginBottom="124dp"
android:layout_marginEnd="30dip"
android:layout_marginStart="30dip"
android:background="#drawable/rect_back"
android:ems="10"
android:hint="Date of Birth"
android:inputType="numberPassword"
android:paddingEnd="10dp"
android:paddingStart="10dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.562"
app:layout_constraintStart_toStartOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="92dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/payngo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="20dip"
android:layout_marginStart="15dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (NGO)"
tools:layout_editor_absoluteX="204dp"
tools:layout_editor_absoluteY="440dp" />
<Button
android:id="#+id/paygo"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginEnd="15dip"
android:layout_marginStart="20dip"
android:layout_weight="1"
android:background="#drawable/rect_back_button"
android:text="Pay (GO)"
tools:layout_editor_absoluteX="92dp"
tools:layout_editor_absoluteY="440dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:background="#drawable/rect_back_text"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="50dip"
android:layout_weight="1"
android:text="#string/linef"
android:textAlignment="textEnd"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="30dp"
tools:layout_editor_absoluteY="505dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
app:srcCompat="#drawable/heart"
tools:ignore="VectorDrawableCompat" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="50dip"
android:layout_weight="1"
android:text="#string/linee"
android:textColor="#fff"
android:textSize="20sp"
tools:layout_editor_absoluteX="171dp"
tools:layout_editor_absoluteY="505dp" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Since you are getting the value in onCreate which will be executed even before you enter anything, your Toast shows blank data.
Move the getText() methods to onClick to achieve expected result as follows:
payNGO.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
username = usernameField.getText().toString();
password = passwordField.getText().toString();
Toast.makeText(login.this, username, Toast.LENGTH_LONG).show();
}
});
Toast.makeText(login.this, usernameField.getText().toString(), Toast.LENGTH_LONG).show();
you can use this code it definitely work..

Crashes probably due to bad activity layout

I've been developing an application and it has been working good until I added a LinearLayout (id = 'toolbar') and some functions in that activity's corresponding Java file. Here are my codes:
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com.myapplication">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".EditPhotoActivity"
android:parentActivityName=".MainActivity">
<meta-data android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
</application>
</manifest>
MainActivty is working good and I don't think the problem is in it. Please let me know if you want me add its code.
EditPhotoActivity.java
package com.example.com.myapplication;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;
import java.io.IOException;
public class EditPhotoActivity extends AppCompatActivity {
public ImageView image_view = findViewById(R.id.image_display) ;
public boolean toolbar_is_open = false ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_photo);
ImageView imageView = findViewById(R.id.image_display);
Intent intent = getIntent();
Uri content;
if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Uri")) {
Bundle bundle = intent.getExtras();
if(bundle != null) {
content = (Uri) bundle.get(MainActivity.IMAGE_KEY);
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), content);
imageView.setImageBitmap(bitmap);
} catch(IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed to fetch image data!", Toast.LENGTH_LONG).show() ;
}
}
} else if(intent.getStringExtra(MainActivity.DATA_TYPE).equals("Bundle")) {
Bundle bundle = intent.getBundleExtra(MainActivity.IMAGE_KEY);
Bitmap bitmap = (Bitmap)bundle.get("data");
imageView.setImageBitmap(bitmap);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.publish_menu, menu);
return true ;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.share_button:
// Save the photo
break;
case R.id.save_button:
// Open Share Photo Activity
break;
}
return super.onOptionsItemSelected(item);
}
private void setToolbarState(boolean open) {
LinearLayout toolbar = findViewById(R.id.toolbar);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)toolbar.getLayoutParams();
params.height = open? 50 : 0;
toolbar.setLayoutParams(params);
toolbar_is_open = open ;
}
public void openRotateToolbar(View view) {
Button right = new Button(this), left = new Button(this);
right.setText(R.string.right); left.setText(R.string.left);
right.setBackgroundResource(R.drawable.custom_button);
left.setBackgroundResource(R.drawable.custom_button);
right.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
image_view.setRotation(90f);
}
});
left.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View view) {
image_view.setRotation(-90f);
}
});
if(!toolbar_is_open)
setToolbarState(true);
}
public void closeToolbar(View view) {
if(toolbar_is_open)
setToolbarState(false);
}
}
activity_edit_photo.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
tools:context="com.example.com.myapplication.EditPhotoActivity"
tools:layout_editor_absoluteY="81dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:weightSum="2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_display"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:background="#color/black"
android:padding="0dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toTopOf="#+id/toolbar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/ic_launcher_background"
tools:ignore="contentDescription" />
<LinearLayout
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="0dp"
android:background="#111"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/scrollView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent">
<Button
android:id="#+id/button4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/close"
android:background="#drawable/custom_button"
android:textColor="#fff"
android:onClick="closeToolbar" />
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#color/darkGrey"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:text="#string/add_sticker"
android:textColor="#fff"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="-15dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/create_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/cut_sticker"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/apply_filter"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/rotate"
android:textSize="12sp"
tools:ignore="ButtonStyle"
android:onClick="openRotateToolbar" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/flip"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
<Button
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#drawable/custom_button"
android:gravity="center"
android:text="#string/crop"
android:textSize="12sp"
tools:ignore="ButtonStyle" />
</LinearLayout>
</HorizontalScrollView>
</android.support.constraint.ConstraintLayout>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
I am not getting any errors but when the app is transitioning to EditPhoto Activity it crashes. Please let me know if you need me to provide you with any other information.
Note: I can not post the logcat because I am not using ADB and I am not using a computer that is all mine (it's my family's) so I don't want to enable x-vt in BIOS and so I am not able to use an Emulator also. To test my app I build the apk then I install it on my phone.
I have found my mistake. I will let this answer here for anyone that comes into the same problem.
The line:
public ImageView image_view = findViewById(R.id.image_display) ;
causes NullPointerException because at the time the app is opening the activity it does not yet have all the view and id's. It gets them only when it calls onCreate so I replaced the line to
public ImageView image_view ;
and inside onCreate after the super.onCreate(savedInstanceState) and the setContentView(R.layout.activity_edit_photo) calls I added
image_view = findViewById(R.id.image_display) ;

when I click on Register here, it does not go to the next activity

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.

Categories

Resources