Crash app when adding multiple activities android studio - java

Hello everybody I got an issue when I trying to run my app in my physical phone and just get crashed at run. its my 5 time making this app, when this happen just close at installing the app. I'm trying to make an app in the main activity two Image buttons and then open anther activity, each one, I checked myself without creating the image buttons. but when I putted the image button the app just get crashed.
here is my code.
ActivityMain.xml
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:background="#drawable/blurred"
tools:context=".MainActivity">
<ImageView
android:id="#+id/blurbackground"
android:layout_width="match_parent"
android:layout_height="97dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="0dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/backmain" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="18dp"
android:layout_marginLeft="18dp"
android:layout_marginTop="62dp"
android:text="Welcome!"
android:textColor="#android:color/white"
android:textSize="22sp" />
<TextView
android:id="#+id/txt2"
android:layout_width="345dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="7dp"
android:layout_marginTop="114dp"
android:gravity="center"
android:text="Master all professions in the professions guide"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black"
android:textSize="18sp"
android:typeface="sans" />
<TextView
android:id="#+id/txt4"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="10dp"
android:layout_marginBottom="348dp"
android:textColorHighlight="#android:color/black"
android:text="Be part of the game and be the master of professions, these guides are collected from users who offer great help in your adventure throughout Azeroth!"
android:textColor="#android:color/white"
android:textStyle="italic"
android:typeface="sans" />
<TextView
android:id="#+id/txt7"
android:layout_width="322dp"
android:layout_height="64dp"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="20dp"
android:layout_marginBottom="75dp"
android:text="We have classroom guides available for you to master your character if you are fresh and new to the world of Warcraft, dominate your enemies and take the victory!"
android:textColor="#android:color/white"
android:textColorHighlight="#android:color/black" />
<ImageView
android:id="#+id/imageView"
android:layout_width="227dp"
android:layout_height="103dp"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="66dp"
android:layout_marginLeft="66dp"
android:layout_marginTop="223dp"
app:srcCompat="#drawable/alchem"
tools:scaleType="centerCrop" />
<ImageButton
android:id="#+id/buthorde"
android:layout_width="68dp"
android:layout_height="75dp"
android:layout_alignStart="#+id/imageView"
android:layout_alignParentBottom="true"
android:layout_marginStart="3dp"
android:layout_marginBottom="155dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/horde" />
<ImageButton
android:id="#+id/butally"
android:layout_width="70dp"
android:layout_height="73dp"
android:layout_alignEnd="#+id/imageView"
android:layout_alignBottom="#+id/buthorde"
android:layout_marginEnd="5dp"
android:layout_marginBottom="3dp"
android:background="#android:color/transparent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/ally" />
<Button
android:id="#+id/butshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="21dp"
android:text="Show me!"
android:textAllCaps="false"
android:textStyle="italic"
android:typeface="sans" />
ActivityMain.java
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private Button butonhorde;
private Button butonally;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
butonally=(Button) findViewById(R.id.butally);
butonally.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
butonhorde=(Button) findViewById(R.id.buthorde);
butonhorde.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}
Logcat
2018-11-01 23:30:05.204 1051-19358/system_process I/OpenGLRenderer: Initialized EGL, version 1.4
2018-11-01 23:30:05.204 1051-19358/system_process D/OpenGLRenderer: Swap behavior 1
2018-11-01 23:30:05.291 1051-12094/system_process D/GraphicsStats: Buffer count: 4
2018-11-01 23:30:05.291 1051-4361/system_process I/WindowManager: WIN DEATH: Window{3382c8d u0 hordemuzzle.wowprofessions/hordemuzzle.wowprofessions.SplashScreenActivity}
2018-11-01 23:30:05.291 1051-12088/system_process I/ActivityManager: Process hordemuzzle.wowprofessions (pid 24362) has died
2018-11-01 23:30:05.291 1051-12088/system_process D/ActivityManager: cleanUpApplicationRecord -- 24362
Splash screen activity
package hordemuzzle.wowprofessions;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import gr.net.maroulis.library.EasySplashScreen;
import static android.support.v4.os.LocaleListCompat.create;
public class SplashScreenActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EasySplashScreen config = new EasySplashScreen(SplashScreenActivity.this)
.withFullScreen()
.withTargetActivity(MainActivity.class)
.withSplashTimeOut(4000)
.withBackgroundResource(android.R.color.background_dark)
.withLogo(R.drawable.logo);
View easysplashscreen = config.create();
setContentView(easysplashscreen);
}
}

try this
package hordemuzzle.wowprofessions;
import android.content.Intent;
import android.graphics.Typeface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//--------------------------------------------------------------------------- -------------------------
TextView textView = (TextView) findViewById(R.id.txt1);
textView.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView.setText("Welcome!");
TextView textView1 = (TextView) findViewById(R.id.txt2);
textView1.setTypeface(Typeface.createFromAsset(getAssets(),"fonts/contm.ttf"));
textView1.setText("Master all professions in the professions guide");
//----------------------------------------------------------------------------------------------------
((ImageButton) findViewById(R.id.butally)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActally();
}
});
((ImageButton) findViewById(R.id.buthorde)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openActhorde();
}
});
}
public void openActhorde(){
Intent intenth = new Intent(this, Activityhorde.class);
startActivity(intenth);
}
public void openActally(){
Intent intenta = new Intent(this, Activityally.class);
startActivity(intenta);
}
}

Related

My activity layout isn't showing when I run the app

I am currently building an Android app in Android Studio, written in Java, a symptom checker application where upon starting the application, user will be required to choose whether they want to open a patient account or a doctor account. For the patient account option, I managed to see the layout when I run the app but when I try to pick the doctor account option, the app does not show the layout. This is weird considering the fact that the code does not show any error, even the logcat too shows the process is perfectly fine.
Here is the activity class and the xml for the doctor account option, also with the main class. I am sorry if somehow the way I post this might be confusing, this is my first time asking a question here.
DoctorRegistrationActivity.java
`package com.example.jeusain;
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.Button;
import android.widget.EditText;
import android.widget.TextView;
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 com.google.firebase.database.DatabaseReference;
public class DoctorRegistrationActivity extends AppCompatActivity {
private TextView goToLogin;
private Button doctorRegisterButton;
private EditText doctor_name, university, placeofpractice, medicalregistrationnumber, doctor_email, doctor_password;
//firebase initialisation
private FirebaseAuth mAuth;
private FirebaseAuth mUser;
private DatabaseReference reference;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_doctor_registration);
doctorRegisterButton = findViewById(R.id.doctor_registerbutton_page1);
doctor_name = findViewById(R.id.doctor_fullname);
university = findViewById(R.id.graduated_uni);
placeofpractice = findViewById(R.id.place_of_practice);
medicalregistrationnumber = findViewById(R.id.registration_number);
doctor_email = findViewById(R.id.doctor_email);
doctor_password = findViewById(R.id.doctor_password);
goToLogin = findViewById(R.id.loginFromDoctor);
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
finish();
}
doctorRegisterButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
performValidations();
Intent goToLogin = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(goToLogin);
}
});
goToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
openLogin();
}
});
}
public void openLogin(){
Intent login = new Intent(DoctorRegistrationActivity.this, LoginActivity.class);
startActivity(login);
}
private void performValidations(){
String getDoctorname = doctor_name.getText().toString();
String getUniversity = university.getText().toString();
String getPlaceofPractice = placeofpractice.getText().toString();
String getMedicalregistrationnumber = medicalregistrationnumber.getText().toString();
String getDoctoremail = doctor_email.getText().toString();
String getDoctorpassword = doctor_password.getText().toString();
if (TextUtils.isEmpty(getDoctorname)){
doctor_name.setError("Name is required!");
return;
}
if (TextUtils.isEmpty(getUniversity)){
university.setError("University is required!");
return;
}
if (TextUtils.isEmpty(getPlaceofPractice)){
placeofpractice.setError("Place of Practice is required!");
return;
}
if (TextUtils.isEmpty(getMedicalregistrationnumber)){
medicalregistrationnumber.setError("Medical registration number is required!");
return;
}
if (TextUtils.isEmpty(getDoctoremail)){
doctor_email.setError("Email is required!");
return;
}
if (TextUtils.isEmpty(getDoctorpassword)){
doctor_password.setError("Password is required!");
return;
}
if (doctor_password.length()<8){
doctor_password.setError("Password must be 8 or more characters!");
return;
}
//register the user in firebase
mAuth.createUserWithEmailAndPassword(getDoctoremail, getDoctorpassword).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
Toast.makeText(DoctorRegistrationActivity.this, "User Created!", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),DoctorProfileActivity.class));
}
else{
Toast.makeText(DoctorRegistrationActivity.this, "Error!" + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
}`
activity_doctor_registration.xml
`<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#a0db8e"
tools:context=".DoctorRegistrationActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/oval_background_shape"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title_DRP_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="You're a registered medical practitioner?"
android:textColor="#color/black"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/subtitle_DRP1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:gravity="center"
android:text="Please fill in the form with relevant information"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="#+id/title_DRP_1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/title_DRP_1" />
<EditText
android:id="#+id/doctor_fullname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/subtitle_DRP1"
app:layout_constraintStart_toStartOf="#+id/subtitle_DRP1"
app:layout_constraintTop_toBottomOf="#+id/subtitle_DRP1" />
<EditText
android:id="#+id/graduated_uni"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Graduated university"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_fullname"
app:layout_constraintStart_toStartOf="#+id/doctor_fullname"
app:layout_constraintTop_toBottomOf="#+id/doctor_fullname" />
<EditText
android:id="#+id/place_of_practice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Place of Practice"
android:inputType="textPersonName"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/graduated_uni"
app:layout_constraintStart_toStartOf="#+id/graduated_uni"
app:layout_constraintTop_toBottomOf="#+id/graduated_uni" />
<EditText
android:id="#+id/registration_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Medical Registration Number"
android:inputType="number"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/place_of_practice"
app:layout_constraintStart_toStartOf="#+id/place_of_practice"
app:layout_constraintTop_toBottomOf="#+id/place_of_practice" />
<EditText
android:id="#+id/doctor_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="E-mail address"
android:inputType="textEmailAddress"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/registration_number"
app:layout_constraintStart_toStartOf="#+id/registration_number"
app:layout_constraintTop_toBottomOf="#+id/registration_number" />
<EditText
android:id="#+id/doctor_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:minHeight="48dp"
app:layout_constraintEnd_toEndOf="#+id/doctor_email"
app:layout_constraintStart_toStartOf="#+id/doctor_email"
app:layout_constraintTop_toBottomOf="#+id/doctor_email" />
<Button
android:id="#+id/doctor_registerbutton_page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="REGISTER"
app:layout_constraintEnd_toEndOf="#+id/doctor_password"
app:layout_constraintStart_toStartOf="#+id/doctor_password"
app:layout_constraintTop_toBottomOf="#+id/doctor_password" />
<TextView
android:id="#+id/loginFromDoctor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login here"
tools:layout_editor_absoluteX="280dp"
tools:layout_editor_absoluteY="597dp"
tools:ignore="MissingConstraints" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
`
MainActivity.java
`package com.example.jeusain;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button choose_patient_account, choose_doctor_account;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start_page);
choose_patient_account = (Button) findViewById(R.id.patient_reg);
choose_doctor_account = (Button) findViewById(R.id.doctor_reg);
choose_patient_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openPatientRegister();
}
});
choose_doctor_account.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDoctorRegister();
}
});
}
public void openPatientRegister() {
Intent goToPatientRegister = new Intent(getApplicationContext(), PatientRegistrationActivity.class);
startActivity(goToPatientRegister);
}
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
}`
Use:
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(this, DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Instead of
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(getApplicationContext(), DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}
Kristy Welsh's answer doesn't solve your problem then try specifying Activity name instead of "this"
public void openDoctorRegister() {
Intent goToDoctorRegister = new Intent(MainActivity.this,DoctorRegistrationActivity.class);
startActivity(goToDoctorRegister);
}

Error in mainactivity java file before that these are separate activities but when i connect two activities i got error

1.Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
2.First I added my two activities then I got this error
Below is XML code:-
Exact what line i got this error please give the solution with code.
<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"
tools:context=".MainActivity2">
<TextView
android:id="#+id/textview"
android:layout_width="346dp"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:text="Text to Speech"
android:textColor="#F32269"
android:textSize="28dp"
android:textStyle="bold" />
<SeekBar
android:id="#+id/seek_bar_speed"
android:layout_width="389dp"
android:layout_height="28dp"
android:layout_below="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:layout_marginEnd="4dp"
android:progress="50" />
<TextView
android:layout_width="match_parent"
android:layout_height="28dp"
android:layout_alignBottom="#id/textview"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="1dp"
android:layout_marginTop="41dp"
android:layout_marginBottom="-29dp"
android:gravity="center"
android:text="Speed"
android:textColor="#color/black"
android:textSize="20dp" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="363dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerInParent="true"
android:layout_marginStart="0dp"
android:layout_marginTop="100dp"
android:layout_marginEnd="2dp">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint=" Enter Text "
android:textColor="#000000"
android:textColorHint="#51FA2B"
android:textSize="20dp"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="89dp"
tools:layout_editor_absoluteX="12dp"
tools:layout_editor_absoluteY="179dp" />
</ScrollView>
<Button
android:id="#+id/button"
android:layout_width="418dp"
android:layout_height="71dp"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="0dp"
android:layout_marginTop="74dp"
android:layout_marginEnd="-7dp"
android:layout_marginBottom="134dp"
android:text="Text to speech"
android:textSize="18dp"
android:textStyle="bold"/>
</RelativeLayout>
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.util.Locale;
import android.widget.Toast;
import android.content.Intent;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
import android.widget.SeekBar;
public class MainActivity2 extends AppCompatActivity {
TextView textView;
private SeekBar mseekbar;
TextToSpeech t1;
EditText ed1;
Button b1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed1 = findViewById(R.id.editText);
b1 = findViewById(R.id.button);
textView = findViewById(R.id.text);
mseekbar = findViewById(R.id.seek_bar_speed);
t1 = new TextToSpeech(getApplicationContext(), new TextToSpeech.OnInitListener() {
#Override
public void onInit(int status) {
if (status != TextToSpeech.ERROR) {
t1.setLanguage(Locale.US);
}
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String toSpeak = ed1.getText().toString();
float speed = (float) mseekbar.getProgress() / 50;
t1.setSpeechRate(speed);
Toast.makeText(MainActivity2.this.getApplicationContext(), toSpeak, Toast.LENGTH_SHORT).show();
t1.speak(toSpeak, TextToSpeech.QUEUE_FLUSH, null);
}
});
}
public void onPause() {
if (t1 != null) {
t1.stop();
}
super.onPause();
}
}```

Intent/Start activity starting the wrong activity?

For some reason, my start activity sends me to the wrong activity( sending me to add task class vs review task class) no error and following in log cat doesn't help me track down the issue. I tried using startActivityForResult(); but that did not help at all and had the same issue. It goes to createTask.class. I need to put more details but I am not sure what else to put, Maybe the put extra and putting two items packed to the instance, but I don't think that would be an issue
<?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=".MainActivity">
<TextView
android:id="#+id/amountOfTasksTextViewUIMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You Have 0 Tasks"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.14"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.04000002" />
<Button
android:id="#+id/viewTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/viewTaskButtonUIMainString"
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.26999998" />
<Button
android:id="#+id/createTaskButtonUIMain"
android:layout_width="209dp"
android:layout_height="46dp"
android:text="#string/createTaskButtonUIMainString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/viewTaskButtonUIMain"
app:layout_constraintTop_toBottomOf="#+id/viewTaskButtonUIMain"
app:layout_constraintVertical_bias="0.04000002" />
<TextView
android:id="#+id/upCommingTasksTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/upCommingTasksTextViewUIMainActivityString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/amountOfTasksTextViewUIMain"
app:layout_constraintVertical_bias="0.18" />
<TextView
android:id="#+id/taskNameTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintStart_toStartOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/upCommingTasksTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintStart_toStartOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintTop_toTopOf="#+id/taskNameTextViewUIMainActivity"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/priorityTextViewUIMainActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="#+id/viewTaskButtonUIMain"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.6"
app:layout_constraintStart_toEndOf="#+id/dateTextViewUIMainActivity"
app:layout_constraintTop_toBottomOf="#+id/taskNameTextViewUIMainActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
/*
HW02
MainActivity.java
*/
package com.example.hw02;
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.TextView;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collections;
import java.util.Comparator;
public class MainActivity extends AppCompatActivity {
//UI Vars
Button viewTaskButtonUIMain;
Button createTaskButtonUIMain;
TextView amountOfTasksTextViewUIMain;
TextView taskNameTextViewUIMainActivity;
TextView dateTextViewUIMainActivity;
TextView priorityTextViewUIMainActivity;
//Java Vars
ArrayList<Tasks> taskArrayList;
ArrayList<String> taskNameList;
int selectedTask;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//UI Vars inst
viewTaskButtonUIMain = findViewById(R.id.viewTaskButtonUIMain);
createTaskButtonUIMain = findViewById(R.id.createTaskButtonUIMain);
amountOfTasksTextViewUIMain = findViewById(R.id.amountOfTasksTextViewUIMain);
dateTextViewUIMainActivity = findViewById(R.id.dateTextViewUIMainActivity);
priorityTextViewUIMainActivity = findViewById(R.id.priorityTextViewUIMainActivity);
taskNameTextViewUIMainActivity = findViewById(R.id.taskNameTextViewUIMainActivity);
//Java Vars inst
taskArrayList = new ArrayList<Tasks>();
//create Tasks
createTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent sendArray1 = new Intent(MainActivity.this,createTask.class );
sendArray1.putExtra("reviewActivityArray",taskArrayList);
startActivity(sendArray1);
}
});
viewTaskButtonUIMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String[] taskNames = new String[taskArrayList.size()];
for(int i = 0; i < taskNames.length; i++) {
taskNames[i] = taskArrayList.get(i).getTaskName();
}
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Select Task")
.setItems(taskNames, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent taskDelete = new Intent(MainActivity.this, reviewActivity.class );
taskDelete.putExtra("catch1", taskArrayList);
taskDelete.putExtra("catch2",which);
startActivity(taskDelete);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.create().show();
}
});
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
//This is for the Create Task
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch")){
taskArrayList = (ArrayList<Tasks>)getIntent().getSerializableExtra("catch");
int vartempsize = taskArrayList.size();//delete
amountOfTasksTextViewUIMain.setText("You have "+ taskArrayList.size() + " tasks");
Collections.sort(taskArrayList);
//if the arrayList isn't empty
if(taskArrayList.isEmpty()==false){
//change the text
String tempName = taskArrayList.get(0).getTaskName();
taskNameTextViewUIMainActivity.setText(tempName);
dateTextViewUIMainActivity.setText(taskArrayList.get(0).returnDate().toString());
priorityTextViewUIMainActivity.setText(taskArrayList.get(0).getPrioty());
}
}
//comming from review
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("dead")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("dead");
Collections.sort(taskArrayList);
}
}
private void sortLists(){
//sorts list
Collections.sort(taskArrayList);
//organize the names
taskNameList.clear();
//adds all the names in the same order as the taskArrayList
for (Tasks var : taskArrayList){
taskNameList.add(var.getTaskName());
}
}
}
<?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=".reviewActivity">
<TextView
android:id="#+id/TaskInformationTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/taskInfromationTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.13"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.06999999" />
<TextView
android:id="#+id/nameTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/nameTextViewUICreateTextString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/TaskInformationTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.100000024" />
<TextView
android:id="#+id/dateTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/dateTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.050000012" />
<TextView
android:id="#+id/priorityTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/priorityTextViewUIactivityReviewString"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.07999998" />
<TextView
android:id="#+id/nameTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/nameTextViewUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/nameTextViewUIactivityReview" />
<TextView
android:id="#+id/dateTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/dateTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/nameTaskTextViewUIactivityReview" />
<TextView
android:id="#+id/priorityTaskTextViewUIactivityReview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintStart_toStartOf="#+id/dateTaskTextViewUIactivityReview" />
<Button
android:id="#+id/cancelButtonUIactivityReview"
android:layout_width="114dp"
android:layout_height="54dp"
android:text="#string/cancleButtonUICreateTaskString"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.09"
app:layout_constraintStart_toStartOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintTop_toBottomOf="#+id/priorityTextViewUIactivityReview"
app:layout_constraintVertical_bias="0.16000003" />
<Button
android:id="#+id/deleteButtonUIactivityReview"
android:layout_width="106dp"
android:layout_height="49dp"
android:text="#string/deleteButtonUIactivityReviewString"
app:layout_constraintBottom_toBottomOf="#+id/cancelButtonUIactivityReview"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.58"
app:layout_constraintStart_toEndOf="#+id/cancelButtonUIactivityReview" />
</androidx.constraintlayout.widget.ConstraintLayout>
package com.example.hw02;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Collections;
public class reviewActivity extends AppCompatActivity {
ArrayList<Tasks> taskArrayList;
int plank;
Button deleteButtonUIactivityReview;
Button cancelButtonUIactivityReview;
TextView nameTaskTextViewUIactivityReview;
TextView dateTaskTextViewUIactivityReview;
TextView priorityTaskTextViewUIactivityReview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_review);
deleteButtonUIactivityReview = findViewById(R.id.deleteButtonUIactivityReview);
cancelButtonUIactivityReview = findViewById(R.id.cancelButtonUIactivityReview);
deleteButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
taskArrayList.remove(plank);
Intent goHomeDelete = new Intent(reviewActivity.this,MainActivity.class);
goHomeDelete.putExtra("dead",taskArrayList);
startActivity(goHomeDelete);
}
});
cancelButtonUIactivityReview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent goHomeSafe = new Intent(reviewActivity.this,MainActivity.class);
startActivity(goHomeSafe);
}
});
if(getIntent() != null && getIntent().getExtras() != null && getIntent().hasExtra("catch1")){
taskArrayList = (ArrayList<Tasks>) getIntent().getSerializableExtra("catch1");
plank = (int)getIntent().getSerializableExtra("catch2");
}
nameTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).taskName);
dateTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).returnDate().toString());
priorityTaskTextViewUIactivityReview.setText(taskArrayList.get(plank).getPrioty());
}
}

App Crashing when using onCreate Eclipse [Beginner]

This is so simple but I have no idea why it's crashing when I click a button.... It's meant to just change the TextView on the activity.
Code is below:::::
MainActivity:
package com.example.databasetut;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
private void displayText(String message) {
TextView textView = (TextView) findViewById(R.id.textDisplay);
textView.setText(message);
}
public void onClick_AddRecord(View v) {
displayText("Clicked add record");
}
public void onClick_ClearAll(View v) {
displayText("Clicked clear all");
}
public void onClick_DisplayRecords(View v) {
displayText("Clicked display record");
}
}
activity_main.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/textDisplay"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:context="com.example.databasetut.MainActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="16dp"
android:onClick="onClick_DisplayRecords"
android:text="Display" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:onClick="onClick_ClearAll"
android:text="Clear" />
<Button
android:id="#+id/btnAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="29dp"
android:onClick="onClick_AddRecord"
android:text="Add" />
<TextView
android:id="#+id/textDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="23dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
What's the problem here?!
You have given android:id="#+id/textDisplay" to both RelativeLayout and to TextView so error occurs.
Change the id of RelativeLayout or TextView your problem will be solved.
I dont think that you're alowed to use uppercase letters in view Id's in your xml layout. So the fix will be to change android:id="#+id/textDisplay" to ex. android:id="#+id/text_display". Plus remove android:id="#+id/textDisplay" from your RelativeLayout keep this id only on your textview

Android Activity stop due to non-working toast

Hello Stack Overflow !
I'm working on an android application for children : Each activity is a question.
For each question, there is two buttons for clue and the answer.
I encounter two issues :
1/ I have to click on a button before change activity, do I need an override or something so I can change of activity without any click required ?
2/ On this activity (code is below), if I click on the button with the id "essai", the activity stop and I came to the previous activity.. Anybody know how I could fix this ?
Thank you in advance !
Here is the java code
package com.beryl.timewaster;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageButton;
import android.widget.Toast;
public class Activity1 extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
}
public void indice1(View v)
{
Toast.makeText(this, "expression", Toast.LENGTH_LONG).show();
}
public void indice2(View v)
{
Toast.makeText(this, "aguéri", Toast.LENGTH_LONG).show();
}
public void essai(View v)
{
Toast.makeText(this, "LOLILO", Toast.LENGTH_LONG).show();
final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1);
ib2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Activity1.this, Accueil.class);
startActivity(intent);
}
});
}}
And here the xml
<AbsoluteLayout
android:id="#+id/absoluteLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<com.google.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="xxx"
ads:loadAdOnCreate="true"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID" >
</com.google.ads.AdView>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="42dp"
android:layout_x="-1dp"
android:layout_y="61dp"
android:text="#string/dev2"
android:textColor="#color/blanc"
android:textSize="25sp"
android:color="#color/blanc" />
<Button
android:id="#+id/Solution2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="259dp"
android:onClick="essai"
android:text="#string/solution"
android:textColor="#color/vertpomme"
android:textSize="30sp" />
<Button
android:id="#+id/indice1de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="1dp"
android:layout_y="121dp"
android:onClick="indice1"
android:text="#string/indice1"
android:textColor="#color/orange"
android:textSize="30sp" />
<Button
android:id="#+id/indice2de2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_x="0dp"
android:layout_y="190dp"
android:onClick="indice2"
android:text="#string/indice2"
android:textColor="#color/rouge"
android:textSize="30sp" />>
<ImageButton
android:id="#+id/flechedroite2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="196dp"
android:layout_y="338dp"
android:src="#drawable/flechedroite" />
<ImageButton
android:id="#+id/flechegauche2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="33dp"
android:layout_y="339dp"
android:src="#drawable/flechegauche" />
</AbsoluteLayout>
</RelativeLayout>
Change final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche1); to final ImageButton ib2 = (ImageButton) findViewById(R.id.flechegauche2);. You have not declared android:id="#+id/flechegauche1" in your xml

Categories

Resources