I am beginner in Android Studio. I am trying to run my app on my mobile, but when i try to run my application on my mobile it shows
Unfortunately app has been stopped
Here is the error showed in Logcat android monitor.
10-23 23:19:25.882 18014-18014/com.example.dhara.codesprint E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.dhara.codesprint, PID: 18014
java.lang.RuntimeException: Unable to start activity ComponentInfo
{com.example.dhara.codesprint/com.example.dhara.codesprint.MainActivity}: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2299)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.
at android.support.v7.app.AppCompatDelegateImplV7.setSupportActionBar(AppCompatDelegateImplV7.java:198)
at android.support.v7.app.AppCompatActivity.setSupportActionBar(AppCompatActivity.java:130)
at com.example.dhara.codesprint.MainActivity.onCreate(MainActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5258)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1099)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1243)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5372)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:970)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:786)
at dalvik.system.NativeStart.main(Native Method)
Below is the code for MainActivity.java
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
TextView text1;
TextView text2;
TextView text3;
TextView text4;
TextView text5;
TextView text6;
EditText text7;
Button checkAnswer;
Button reset;
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
text1 = (TextView) findViewById(R.id.textView1);
text2 = (TextView) findViewById(R.id.textView2);
text3 = (TextView) findViewById(R.id.textView3);
text4 = (TextView) findViewById(R.id.textView4);
text5 = (TextView) findViewById(R.id.textView5);
text6 = (TextView) findViewById(R.id.textView6);
text7 = (EditText) findViewById(R.id.edittext);
checkAnswer = (Button) findViewById(R.id.button);
reset = (Button) findViewById(R.id.button2);
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
AndroidMainfest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.dhara.codesprint">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Codesprint"
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>
</application>
</manifest>
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/background"
tools:context=".MainActivity">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginLeft="13dp"
android:layout_marginStart="13dp"
android:layout_marginTop="14dp"
android:id="#+id/imageView1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView2"
android:layout_alignTop="#+id/imageView3"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView3"
android:layout_alignTop="#+id/imageView1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="24dp"
android:layout_marginEnd="24dp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:layout_marginTop="27dp"
android:id="#+id/imageView4"
android:layout_below="#+id/imageView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView5"
android:layout_alignTop="#+id/imageView4"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/circle"
android:id="#+id/imageView6"
android:layout_alignTop="#+id/imageView5"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3" />
<EditText
android:text="Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="55dp"
android:id="#+id/edittext"
android:textSize="20sp"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColorLink="?attr/actionMenuTextColor" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="31dp"
android:id="#+id/textView2"
android:textAlignment="center"
android:textStyle="normal|bold"
android:layout_alignTop="#+id/imageView2"
android:layout_alignLeft="#+id/imageView2"
android:layout_alignStart="#+id/imageView2"
android:layout_alignRight="#+id/imageView2"
android:layout_alignEnd="#+id/imageView2"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView3"
android:layout_alignEnd="#+id/imageView3"
android:id="#+id/textView3"
android:layout_alignLeft="#+id/imageView3"
android:layout_alignStart="#+id/imageView3"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/imageView4"
android:layout_alignRight="#+id/imageView4"
android:layout_alignEnd="#+id/imageView4"
android:layout_marginTop="30dp"
android:id="#+id/textView4"
android:layout_alignLeft="#+id/imageView4"
android:layout_alignStart="#+id/imageView4"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView4"
android:layout_alignRight="#+id/imageView5"
android:layout_alignEnd="#+id/imageView5"
android:id="#+id/textView5"
android:layout_alignLeft="#+id/imageView5"
android:layout_alignStart="#+id/imageView5"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView5"
android:layout_alignRight="#+id/imageView6"
android:layout_alignEnd="#+id/imageView6"
android:id="#+id/textView6"
android:layout_alignLeft="#+id/imageView6"
android:layout_alignStart="#+id/imageView6"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView2"
android:layout_alignRight="#+id/imageView1"
android:layout_alignEnd="#+id/imageView1"
android:id="#+id/textView1"
android:layout_alignLeft="#+id/imageView1"
android:layout_alignStart="#+id/imageView1"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light" />
<Button
android:text="Check Your Answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:id="#+id/button"
android:textAlignment="center"
android:textStyle="normal|bold"
android:textColor="#android:color/background_light"
android:background="#color/colorPrimary"
android:layout_below="#+id/edittext"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Reset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="27dp"
android:id="#+id/button2"
android:background="#color/colorPrimary"
android:textSize="14sp"
tools:textColor="#android:color/background_light" />
</RelativeLayout>
I have tried some solution from web but it is not working.
You layout does not have a Toolbar, and yet you are calling the following:
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Either remove these calls or add a Toolbar to your layout.
For the latter to work you also need to use (or derive from) one of the AppCompat themes that do not have an ActionBar, Theme.AppCompat.Light.NoActionBar for example.
So in your styles.xml you would have something like this:
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
You can't initiate your view elements (TextView, Buttons etc) before setting out the layout xml which for your case is R.layout.activity_main file.
package com.example.dhara.codesprint;
import android.content.res.AssetManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
//private WordDictionary dictionary;
private String currentWord;
private ArrayList<String> words;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView text1 = (TextView) findViewById(R.id.textView1);
TextView text2 = (TextView) findViewById(R.id.textView2);
TextView text3 = (TextView) findViewById(R.id.textView3);
TextView text4 = (TextView) findViewById(R.id.textView4);
TextView text5 = (TextView) findViewById(R.id.textView5);
TextView text6 = (TextView) findViewById(R.id.textView6);
TextView text7 = (TextView) findViewById(R.id.edittext);
Button checkAnswer = (Button) findViewById(R.id.button);
Button reset = (Button) findViewById(R.id.button2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
AssetManager assetManager = getAssets();
try {
InputStream inputStream = assetManager.open("words.txt");
} catch (IOException e) {
Toast toast = Toast.makeText(this, "Could not load dictionary", Toast.LENGTH_LONG);
toast.show();
}
reset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
checkAnswer.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Please initialize your layouts element in onCreate() because when your activity get start it calls onCreate() method ,but here you are calling your layout from your onCreate() method but after that you are not initializing your elements ,also check that whether you have added the name of activity in your manifest or not ,please read and implement carefully ,hope it ll help you.
Related
Problem:
I have a java/xml file in android studio. I need to play two buttons; One for "Map page" and second for "Create new account" (Signup activity). The first button work, but the second close my app. What is the problem?
What I know:
if it is empty xml file the button for to go to the xml file work.
The errors:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sampleapp, PID: 8062
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sampleapp/com.example.sampleapp.SignUpActivity}: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.ClassCastException: androidx.appcompat.widget.AppCompatImageButton cannot be cast to android.widget.Button
at com.example.sampleapp.SignUpActivity.onCreate(SignUpActivity.java:33)
at android.app.Activity.performCreate(Activity.java:7802)
at android.app.Activity.performCreate(Activity.java:7791)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1299)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
Scripts:
LoginActivity.java:
package com.example.sampleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
public EditText username;
public EditText password;
public TextView errorView;
public Button login;
public Button createNewAccount;
public void initMapPage(){
Intent mapPage = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mapPage);
finish();
}
public void initSignUpPage(){
Intent signUpPage = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(signUpPage);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
username = (EditText) findViewById(R.id.UsernameBox);
password = (EditText) findViewById(R.id.PasswordBox);
errorView = (TextView) findViewById(R.id.errorView);
login = (Button) findViewById(R.id.LoginButton);
createNewAccount = (Button) findViewById(R.id.SignupButton);
login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String errorMsg = "";
if (username.getText().toString().equals("") || password.getText().toString().equals("")) {
if (username.getText().toString().equals("")) {
errorMsg += "Username block is empty\n";
}
if (password.getText().toString().equals("")) {
errorMsg += "Password block is empty\n";
}
errorView.setText(errorMsg);
}
else {
initMapPage();
}
}
});
createNewAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
initSignUpPage();
}
});
}
}
activity_login.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity">
<TextView
android:id="#+id/WelcomeBox"
android:layout_width="210dp"
android:layout_height="101dp"
android:layout_marginTop="64dp"
android:text="Welcome \nTo Our App!"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/UsernameBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="40dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/WelcomeBox" />
<EditText
android:id="#+id/PasswordBox"
android:layout_width="300dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/UsernameBox" />
<Button
android:id="#+id/SignupButton"
android:layout_width="237dp"
android:layout_height="55dp"
android:layout_marginTop="24dp"
android:text="Create New Acount"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4" />
<Button
android:id="#+id/LoginButton"
android:layout_width="115dp"
android:layout_height="63dp"
android:layout_marginTop="12dp"
android:text="LOGIN"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/errorView" />
<TextView
android:id="#+id/textView4"
android:layout_width="60dp"
android:layout_height="29dp"
android:layout_marginTop="24dp"
android:text="OR"
android:textAlignment="center"
android:textColor="#D3000000"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LoginButton" />
<TextView
android:id="#+id/errorView"
android:layout_width="209dp"
android:layout_height="62dp"
android:layout_marginTop="16dp"
app:layout_constraintEnd_toEndOf="parent"
android:textAlignment="center"
android:textColor="#FF0000"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/PasswordBox" />
</androidx.constraintlayout.widget.ConstraintLayout>
SignUpActivity.java:
package com.example.sampleapp;
import androidx.appcompat.app.AppCompatActivity;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class SignUpActivity extends AppCompatActivity {
public Button backToLogin;
public Button SignUp;
public EditText username;
public EditText password;
public EditText email;
public void initLoginPage(){
Intent loginPage = new Intent(SignUpActivity.this, LoginActivity.class);
startActivity(loginPage);
finish();
}
#SuppressLint("WrongViewCast")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
SignUp = (Button) findViewById(R.id.suButton);
backToLogin = (Button) findViewById(R.id.back_to_login);
username = (EditText) findViewById(R.id.UsernameBoxSignUp);
password = (EditText) findViewById(R.id.PasswordBoxSignUp);
email = (EditText) findViewById(R.id.EmailBox);
backToLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
initLoginPage();
}
});
}
}
activity_sign_up.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"
tools:context=".SignUpActivity">
<EditText
android:id="#+id/UsernameBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="54dp"
android:layout_marginTop="160dp"
android:ems="10"
android:hint="Username"
android:inputType="textPersonName"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/back_to_login" />
<EditText
android:id="#+id/PasswordBoxSignUp"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="55dp"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/EmailBox" />
<Button
android:id="#+id/suButton"
android:layout_width="170dp"
android:layout_height="53dp"
android:layout_marginStart="120dp"
android:layout_marginTop="36dp"
android:text="SignUp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/PasswordBoxSignUp" />
<EditText
android:id="#+id/EmailBox"
android:layout_width="300dp"
android:layout_height="40dp"
android:layout_marginStart="44dp"
android:layout_marginTop="16dp"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/UsernameBoxSignUp" />
<ImageButton
android:id="#+id/back_to_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="54dp"
android:layout_marginTop="28dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="?attr/actionModeCloseDrawable" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem is that ImageButton (and thus also the appcompat version) is a subclass of ImageView, not of Button. You should either change the cast to that, or just remove it entirely if you're only using View methods like setOnClickListener.
Is the username or password text set up?
Android:text=""
A hint is just a hint, not the actual response. So it gets an error when it looking for something that's not even set up yet.
Your calling SignUpActivity twice
Place the intent
Intent signUpPage = new Intent(LoginActivity.this, SignUpActivity.class);
startActivity(signUpPage);
on onClick method
and don't forget to add android:onClick="OnCreateAccount" on your button
I entered some text in the text boxes on first activity.
Then, On pressing the "SAVE" button, I am moved to the second activity.
Now, On pressing the "BACK" button, I am dropped back on the first activity where i am unable to see the values/text i entered before.
I have tried using shared preferences also.
Here is my code
MainActivity.java
package com.example.madhur.intentclear;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText tv1 = findViewById(R.id.editText);
final EditText tv2 = findViewById(R.id.editText2);
final EditText tv3 = findViewById(R.id.editText3);
final Button bt1 = findViewById(R.id.button);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SharedPreferences sharedPreferences1=getSharedPreferences("value1",Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sharedPreferences1.edit();
editor.putString("tv1",tv1.getText().toString());
SharedPreferences sharedPreferences2=getSharedPreferences("value2",Context.MODE_PRIVATE);
SharedPreferences.Editor editor2=sharedPreferences2.edit();
editor2.putString("tv2",tv2.getText().toString());
SharedPreferences sharedPreferences3=getSharedPreferences("value3",Context.MODE_PRIVATE);
SharedPreferences.Editor editor3=sharedPreferences3.edit();
editor3.putString("tv3",tv3.getText().toString());
editor.apply();
editor2.apply();
editor3.apply();
tv1.setText(String.valueOf(sharedPreferences1.getString("tv1",null)));
tv2.setText(String.valueOf(sharedPreferences2.getString("tv2",null)));
tv3.setText(String.valueOf(sharedPreferences3.getString("tv3",null)));
Intent intent = new Intent(MainActivity.this, Main2Activity.class);
intent.putExtra("tv1",tv1.getText().toString());
intent.putExtra("tv2",tv2.getText().toString());
intent.putExtra("tv3",tv3.getText().toString());
startActivity(intent);
}
});
}
}
Main2Activity.java
package com.example.madhur.intentclear;
import android.content.Context;
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.TextView;
import java.io.FileOutputStream;
import java.io.IOException;
public class Main2Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
final TextView tv1=findViewById(R.id.textView);
final TextView tv2=findViewById(R.id.textView2);
final TextView tv3=findViewById(R.id.textView3);
Button button=findViewById(R.id.button);
Button button1=findViewById(R.id.button1);
Button button2=findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Bundle intent=getIntent().getExtras();
tv1.setText(intent.getString("tv1"));
tv2.setText(intent.getString("tv2"));
tv3.setText(intent.getString("tv3"));
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String clear="";
tv1.setText(clear);
tv2.setText(clear);
tv3.setText(clear);
m1();
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent1=new Intent(Main2Activity.this,MainActivity.class);
startActivity(intent1);
}
});
}
public void m1()
{String clear="";
try {
FileOutputStream fileOutputStream;
FileOutputStream fileOutputStream2;
FileOutputStream fileOutputStream3;
fileOutputStream = openFileOutput("et1.txt", Context.MODE_PRIVATE);
fileOutputStream.write(clear.getBytes());
fileOutputStream2 = openFileOutput("et2.txt", Context.MODE_PRIVATE);
fileOutputStream2.write(clear.getBytes());
fileOutputStream3 = openFileOutput("et3.txt", Context.MODE_PRIVATE);
fileOutputStream3.write(clear.getBytes());
} catch (IOException e) {
e.printStackTrace();
}
}
}
activity_main.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.madhur.intentclear.MainActivity">
<android.support.constraint.Guideline
android:id="#+id/guideline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.13" />
<android.support.constraint.Guideline
android:id="#+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.87" />
<android.support.constraint.Guideline
android:id="#+id/guideline3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.06" />
<android.support.constraint.Guideline
android:id="#+id/guideline4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.18" />
<android.support.constraint.Guideline
android:id="#+id/guideline5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.29" />
<android.support.constraint.Guideline
android:id="#+id/guideline6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.43" />
<android.support.constraint.Guideline
android:id="#+id/guideline7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.58" />
<android.support.constraint.Guideline
android:id="#+id/guideline8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.73" />
<android.support.constraint.Guideline
android:id="#+id/guideline9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.82" />
<android.support.constraint.Guideline
android:id="#+id/guideline10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.95" />
<android.support.constraint.Guideline
android:id="#+id/guideline11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.24" />
<android.support.constraint.Guideline
android:id="#+id/guideline12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.77" />
<EditText
android:id="#+id/editText"
android:layout_width="0dp"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
android:hint="Enter name"
app:layout_constraintBottom_toTopOf="#+id/guideline4"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline3" />
<EditText
android:id="#+id/editText2"
android:layout_width="0dp"
android:hint="Enter phone number"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/guideline6"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline5" />
<EditText
android:id="#+id/editText3"
android:layout_width="0dp"
android:hint="Enter Email Adress"
android:layout_height="0dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toTopOf="#+id/guideline8"
app:layout_constraintEnd_toStartOf="#+id/guideline2"
app:layout_constraintStart_toStartOf="#+id/guideline"
app:layout_constraintTop_toTopOf="#+id/guideline7" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="0dp"
android:text="SAVE"
app:layout_constraintBottom_toTopOf="#+id/guideline10"
app:layout_constraintEnd_toStartOf="#+id/guideline12"
app:layout_constraintStart_toStartOf="#+id/guideline11"
app:layout_constraintTop_toTopOf="#+id/guideline9" />
</android.support.constraint.ConstraintLayout>
activity_Main2.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.madhur.intentclear.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline16"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline15" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline19"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline20" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/guideline17"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline18" />
<android.support.constraint.Guideline
android:id="#+id/guideline13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_begin="249dp" />
<android.support.constraint.Guideline
android:id="#+id/guideline14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.33854166" />
<android.support.constraint.Guideline
android:id="#+id/guideline15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.031311154" />
<android.support.constraint.Guideline
android:id="#+id/guideline16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.13" />
<android.support.constraint.Guideline
android:id="#+id/guideline17"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.38" />
<android.support.constraint.Guideline
android:id="#+id/guideline18"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.29" />
<android.support.constraint.Guideline
android:id="#+id/guideline19"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.26" />
<android.support.constraint.Guideline
android:id="#+id/guideline20"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.16" />
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="52dp"
android:text="SHOW TEXT"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toTopOf="#+id/guideline17" />
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="44dp"
android:text="CLEAR TEXT"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:text="BACK"
app:layout_constraintEnd_toStartOf="#+id/guideline13"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/guideline14"
app:layout_constraintTop_toBottomOf="#+id/button1" />
</android.support.constraint.ConstraintLayout>
Use savedInstanceState Bundle in onCreate
The savedInstanceState is a reference to a Bundle object that is passed into the onCreate method of every Android Activity. Activities have the ability, under special circumstances, to restore themselves to a previous state using the data stored in this bundle. If there is no available instance data, the savedInstanceState will be null.
For example, the savedInstanceState will always be null the first time
an Activity is started, but may be non-null if an Activity is
destroyed during rotation.
When do I save things to the Bundle?
All activities have an onSaveInstanceState method that can be overridden. When this method is called, any state-related data should be placed into the outState Bundle. This method is called when an Activity is being backgrounded (either after onPause() or onStop(), depending on different factors).
What should be saved?
The savedInstanceState Bundle should only save information directly related to the current Activity state. Examples of this include:
A typical example is User-submitted data – If a user writes their username into a text box, they would expect the username to still be present when the Activity is resumed. This data should be saved in onSaveInstanceState then restored when the Activity is re-created.
In your onCreate function, this Bundle is handed back to the program. The best way to check if the application is being reloaded, or started for the first time is:
if (savedInstanceState != null) {
// Then the application is being reloaded
}
To get the data back out, use the get* functions just like the put* functions. The data is stored as a name-value pair. This is like a hashmap. You provide a key and the value, then when you want the value back, you give the key and the function gets the value. Here's a short example.
#Override
public void onSaveInstanceState(Bundle outState) {
outState.putString("message", "This is my message to be reloaded");
super.onSaveInstanceState(outState);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
String message = savedInstanceState.getString("message");
Toast.makeText(this, message, Toast.LENGTH_LONG).show();
}
}
// Promoting Local variable to Field
public class Main2Activity extends AppCompatActivity {
private TextView tv1;
private TextView tv2;
private TextView tv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
tv1 = (TextView) findViewById(R.id.textView);
tv2 = (TextView) findViewById(R.id.textView2);
tv3 = (TextView) findViewById(R.id.textView3);
...
}
}
Use shared Preference to store values temporally/ permanent
EDITED
Sorry i was not seen the code after seen code.
Your setting values inside onlCick again. You have set the values outside the onClick
Solution
Create one global SharedPreferences with MODE_PRIVATE, set the values onClick and read values on onCreate(outside onClick).
objectName.java
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class objectName extends AppCompatActivity {
EditText objectInput;
FloatingActionButton actionButton;
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_object_name);
// An intent received and hence been written bellow the Edit_text input to
be shown as consideration.
Intent toAgeInput = getIntent();
final String name = toAgeInput.getStringExtra("users name");
final String display = toAgeInput.getStringExtra("alphabet");
TextView show = (TextView) findViewById(R.id.disclaimer);
show.setText("Please type the name starting with the alphabet, Capital '
" + display+ " '");
objectInput = (EditText) findViewById(R.id.inputName);
actionButton = (FloatingActionButton) findViewById(R.id.outputButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
actionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
/*condition layout which allows user to dial only an alphabet
starting with random selected alphabet from baseInt activity
input.*/
if (!objectInput.getText().toString().matches(display+ "[a-zA-
Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("You have to Start with 'Capital " +
display+"'.");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if (!objectInput.getText().toString().matches("[a-zA-Z]+"))
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your name must be in words");
Toast toast = new Toast(objectName.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent openAge = new Intent(objectName.this,
ageInput.class);
String input = objectInput.getText().toString();
openAge.putExtra("alphabet", display);//alphabet to use
openAge.putExtra("users name", name);//name of the user.
openAge.putExtra("belonging", input);//the object name
provided in this class.
startActivity(openAge);//running activity.
}
}
});
}}
This file is almost working great but whenever I click on the next button it crashes the app
activity_objectName.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/object1bg">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/detail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="45dp"
app:srcCompat="#drawable/type15"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputName"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:hint="#string/Object"
android:textColor="#A52A2A"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/outputButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/appbar2"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:background="#drawable/button"
tools:ignore="UnknownIdInLayout">
<TextView
android:id="#+id/disclaimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="12dp"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
Main problem is here I'm not able to go to this file "ageInput.java",
activity_ageInput.xml
package randomexcessor.foutuneteller;
import android.content.Intent;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ageInput extends AppCompatActivity {
TextView text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_age_input);
final EditText ageFind = (EditText) findViewById(R.id.inputAge);
FloatingActionButton ageButton = (FloatingActionButton)
findViewById(R.id.ageButton);
LayoutInflater inflater = getLayoutInflater();
final View layout = inflater.inflate(R.layout.toast_layout,
(ViewGroup) findViewById(R.id.custom_toast_container));
ageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(ageFind.getText().toString().isEmpty())
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Please type your age or System error.");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else if(ageFind.getText().length() > 2)
{
text = (TextView) layout.findViewById(R.id.errorReport);
text.setText("Your age cannot be " +
ageFind.getText().toString() +"!!");
text = (TextView) layout.findViewById(R.id.errorReport);
Toast toast = new Toast(ageInput.this);
toast.setDuration(Toast.LENGTH_LONG);
toast.setGravity(Gravity.CENTER_HORIZONTAL,0,0);
toast.setView(layout);
toast.show();
}
else
{
Intent runResult = getIntent();
final String alpha = runResult.getStringExtra("alphabet");
final String name = runResult.getStringExtra("users name");
final String belong = runResult.getStringExtra("belonging");
Intent finalPage = new Intent(ageInput.this, result.class);
finalPage.putExtra("alphabet", alpha);
finalPage.putExtra("users name", name);
finalPage.putExtra("belonging", belong);
finalPage.putExtra("users age",
ageFind.getText().toString());
startActivity(finalPage);
}
}
});
}
}
activity_ageInput.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/agefact2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
app:srcCompat="#drawable/type13"
tools:ignore="ContentDescription" />
<LinearLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="6sp"
android:background="#drawable/edit_background"
android:padding="2dp"
android:weightSum="1">
<EditText
android:id="#+id/inputAge"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#drawable/edit_text"
android:ems="10"
android:hint="#string/age"
android:inputType="text"
android:padding="10dp"
android:textSize="18sp"
android:layout_margin="3dp"
android:layout_weight="1.02"
android:textColor="#color/colorAccent"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/ageButton"
android:layout_marginEnd="10dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
app:backgroundTint="#32CD32"
app:elevation="2dp"
app:fabSize="normal"
app:rippleColor="#7FFF00"
app:srcCompat="#drawable/ic_keyboard_arrow_right_white_24dp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
edit_background.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="1dp">
<stroke
android:color="#696969"
android:width="2dp"/>
<solid android:color="#FFFFFF"></solid>
<corners
android:radius="25dp" />
</shape>
edit_text.xml
<shape xmlns:android="http://schemas.android.com/apk/res/android">
</shape>
crash report
07-02 13:29:40.966 14586-14586/randomexcessor.foutuneteller E/AndroidRuntime: FATAL EXCEPTION: main
Process: randomexcessor.foutuneteller, PID: 14586
java.lang.OutOfMemoryError: Failed to allocate a 19743564 byte allocation with 15898356 free bytes and 15MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:609)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:444)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:988)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2384)
at android.content.res.Resources.getDrawable(Resources.java:787)
at android.content.Context.getDrawable(Context.java:403)
at android.support.v4.content.ContextCompatApi21.getDrawable(ContextCompatApi21.java:30)
at android.support.v4.content.ContextCompat.getDrawable(ContextCompat.java:372)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:202)
at android.support.v7.widget.AppCompatDrawableManager.getDrawable(AppCompatDrawableManager.java:190)
at android.support.v7.content.res.AppCompatResources.getDrawable(AppCompatResources.java:100)
at android.support.v7.widget.AppCompatImageHelper.loadFromAttributes(AppCompatImageHelper.java:54)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:66)
at android.support.v7.widget.AppCompatImageView.(AppCompatImageView.java:56)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImplV9.createView(AppCompatDelegateImplV9.java:1029)
at android.support.v7.app.AppCompatDelegateImplV9.onCreateView(AppCompatDelegateImplV9.java:1087)
at android.support.v4.view.LayoutInflaterCompatHC$FactoryWrapperHC.onCreateView(LayoutInflaterCompatHC.java:47)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:725)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:292)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at randomexcessor.foutuneteller.ageInput.onCreate(ageInput.java:22)
at android.app.Activity.performCreate(Activity.java:6010)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.access$800(ActivityThread.java:155)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5343)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:905)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:700)
The first level of your crash report shows an OOM, which means OutOfMemory. Any Bitmap that you put inside activity_ageInput.xml or that maybe you used in your Toast´s layout is too big (or all bitmaps). You should consider to reduce the size of your bitmaps. Read this thread about handling bitmaps:
https://developer.android.com/topic/performance/graphics/index.html
I've been following this tutorial: https://www.youtube.com/watch?v=sKCdqCuFFFk
I completed the tutorial, but when I wanted to launch, it gave an error. These are my codes:
(MainActivity.java)
package com.example.admir_000.jdsfnjls;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText etUsername = (EditText) findViewById(R.id.etUsername);
final EditText etPassword = (EditText) findViewById(R.id.etPassword);
final Button bLogin = (Button) findViewById(R.id.bLogin);
final TextView tvRegisterHere = (TextView) findViewById(R.id.tvRegisterHere);
registerLink.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
LoginActivity.this.startActivity(registerIntent);
}
});
}
}
and this is my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.admir_000.jdsfnjls.MainActivity">
<TextView
android:text="Register here"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/bLogin"
android:layout_centerHorizontal="true"
android:id="#+id/tvRegisterHere" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/etPassword"
android:hint="Password"
android:layout_marginTop="36dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/etUsername"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Username" />
<Button
android:text="Login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bLogin"
android:layout_below="#+id/etPassword"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
Does anyone know how to fix this?
I am sure, without read the logcat, that you got a Null Pointer Exception.... why?
registerLink.setOnClickListener....
Where did you initialized registerLink variable? Maybe you wanted to put tvRegisterHere.setOnClickListener...
P.S: I suggest you to already read the stacktrace and then search for the particular Exception
when I click on Logout button it goes to the next activity but, On the next activity when I click on the "Register here" to go to the next activity it says unfortunately , Login has stopped. Please help me out.
Main Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button blogout;
EditText uname1, name1, age1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname1 =(EditText) findViewById(R.id.uname1);
name1 = (EditText) findViewById(R.id.name1);
age1 = (EditText) findViewById(R.id.age1);
blogout = (Button) findViewById(R.id.blogout);
blogout.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.blogout:
startActivity(new Intent(this, Login.class));
break;
}
}
}
Login Activity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class Login extends AppCompatActivity implements View.OnClickListener {
Button blogin;
EditText uname, pass;
TextView registerLink;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
uname =(EditText) findViewById(R.id.uname);
pass = (EditText) findViewById(R.id.pass);
registerLink = (TextView) findViewById(R.id.registerLink);
blogin = (Button) findViewById(R.id.blogin);
blogin.setOnClickListener(this);
registerLink.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.registerLink:
startActivity(new Intent(this, Register.class));
break;
case R.id.blogin:
break;
}
}
}
Register Acitvity
package com.example.namrata.login;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Register extends AppCompatActivity implements View.OnClickListener {
Button bregister;
EditText uname2, pass1, name2, age2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
uname2 =(EditText) findViewById(R.id.uname2);
pass1 = (EditText) findViewById(R.id.pass1);
name2 = (EditText) findViewById(R.id.name2);
age2 = (EditText) findViewById(R.id.age2);
bregister = (Button) findViewById(R.id.blogin);
bregister.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()){
case R.id.bregister:
break;
}
}
}
activity_login layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/blogin"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"
android:layout_marginBottom="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
activity_main layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age2"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname2"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Logout"
android:id="#+id/blogout"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
activity_register layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp"
android:background="#28e317"
android:backgroundTint="#ffffff">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name"
android:id="#+id/name"
android:textSize="20dp"
android:textStyle="bold" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:ems="10"
android:id="#+id/name1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Age"
android:id="#+id/age"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/age1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username"
android:id="#+id/username1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/uname1"
android:layout_marginBottom="15dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Password"
android:id="#+id/password1"
android:textSize="20dp"
android:textStyle="bold"
android:background="#ffffff" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/pass1"
android:layout_marginBottom="15dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Register"
android:id="#+id/bregister"
android:layout_gravity="center_horizontal"
android:background="#7c82f9"
android:layout_marginTop="20dp"/>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.namrata.login">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Login"
android:label="#string/title_activity_login"
android:theme="#style/AppTheme.NoActionBar" />
<activity
android:name=".Register"
android:label="#string/title_activity_register"
android:theme="#style/AppTheme.NoActionBar"></activity>
</application>
In register activity xml you have android:id="#+id/bregister"
but in RegisterActivity you searching for login button
bregister = (Button) findViewById(R.id.blogin);
which is null that is why you are getting null pointer exception i think change it to
bregister = (Button) findViewById(R.id.bregister);
In your layout file for register activity (activity_register) you have:
android:id="#+id/bregister"
but in Register Activity.java you're doing:
bregister = (Button) findViewById(R.id.blogin);
so register is set to null. Hence it crashes on:
bregister.setOnClickListener(this);
Your onClick method in MainActivity is not the overrided one that's why it's not getting called automatically.Create it as you have create onClick method in LoginActivity
In your First Activity class MainActivity you are using this layout class
setContentView(R.layout.activity_main)
You should use
setContentView(R.layout.activity_login)
and in this activity_login layout you should
change TextView is Register here To Button
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register Here."
android:id="#+id/registerLink"
android:layout_gravity="center_horizontal"
android:textSize="20dp"
android:textStyle="italic" />
Remove this and use button instead of TextView.