How to fix sharedpreference to save my data and skip an activity? - java

I want to use "Shared preference" for saving my username and password and it saved but do not work on second time i open the app.
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.icu.text.IDNA;
import android.support.v4.media.RatingCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = (TextView)findViewById(R.id.tv_user);
tv_pass = (TextView)findViewById(R.id.tv_pass);
tx_user = (EditText)findViewById(R.id.tx_user);
tx_pass = (EditText)findViewById(R.id.tx_pass);
btn_login = (Button)findViewById(R.id.btn_login);
img_ghorme = (ImageView)findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_page2);
findall();
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String user = tx_user.getText()+"";
String pass = tx_pass.getText()+"";
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
save = getSharedPreferences("user", MODE_PRIVATE);
save = getSharedPreferences("pass", MODE_PRIVATE);
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}
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.home.seesion10.Login_page"
android:layoutDirection="rtl"
android:textDirection="rtl"
android:background="#color/textShiri">
<ImageView
android:id="#+id/img_ghorme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/cirlcleghormesabzi"
android:layout_marginTop="1500dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginLeft="100dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintVertical_bias="0.902"
android:layout_marginStart="100dp" />
<TextView
android:id="#+id/tv_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="نام کاربری"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/tx_user"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_user"
android:ems="10"
android:hint="نام کاربری"
android:inputType="textPersonName"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_user" />
<TextView
android:id="#+id/tv_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="15dp"
android:layout_marginTop="20dp"
android:text="رمز عبور"
android:textSize="15sp"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_user" />
<EditText
android:id="#+id/tx_pass"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginRight="30dp"
android:layout_marginTop="10dp"
android:background="#drawable/tx_bg"
android:drawableLeft="#drawable/ic_pass"
android:ems="15"
android:hint="........."
android:inputType="textPassword"
android:padding="10dp"
android:text=""
android:textColor="#color/textShiri"
android:textColorHint="#color/textShiri"
android:visibility="invisible"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_pass" />
<Button
android:id="#+id/btn_login"
android:layout_width="122dp"
android:layout_height="50dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="20dp"
android:background="#drawable/btn_bg"
android:text="ورود"
android:textColor="#color/textShiri"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/img_ghorme"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tx_pass" />
</android.support.constraint.ConstraintLayout>
i expected that when i write admin admin for username and password, it saved it and for the second time i open the app it skip that login page but its not work and show the login page again

You are not initialising the sharedPreference before using it and defining two sharedPreferences for each key-pair. Change your code as below.
public class Login_page extends AppCompatActivity {
TextView tv_user, tv_pass;
EditText tx_user, tx_pass;
Button btn_login;
ImageView img_ghorme;
SharedPreferences save;
public void findall()
{
tv_user = findViewById(R.id.tv_user);
tv_pass = findViewById(R.id.tv_pass);
tx_user = findViewById(R.id.tx_user);
tx_pass = findViewById(R.id.tx_pass);
btn_login = findViewById(R.id.btn_login);
img_ghorme = findViewById(R.id.img_ghorme);
}
public String save_user = "";
public String save_pass = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
findall();
save = getSharedPreferences("userInfo", MODE_PRIVATE);
save_user = save.getString("username", "");
save_pass = save.getString("password", "");
if(save_user.equals("admin") && save_pass.equals("admin"))
{
Intent skip = new Intent(Login_page.this, food_page.class);
startActivity(skip);
}
btn_login.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
String user = tx_user.getText().toString();
String pass = tx_pass.getText().toString();
SharedPreferences.Editor e = save.edit();
e.putString("username", user);
e.putString("password", pass);
e.apply();
if(user.equals("admin") && pass.equals("admin"))
{
Intent food = new Intent(Login_page.this, food_page.class);
startActivity(food);
}
if (!user.equals("admin") || !pass.equals("admin"))
{
e.putString("username", "");
e.putString("password", "");
e.apply();
tx_user.setText("");
tx_pass.setText("");
}
}
});
Typeface font_shabnam = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
Typeface font_shabnam_light = Typeface.createFromAsset(getAssets(),"fonts/Shabnam_Light.ttf");
Typeface font_shabnam_bold = Typeface.createFromAsset(getAssets(),"fonts/Shabnam.ttf");
tv_user.setTypeface(font_shabnam_bold);
tv_pass.setTypeface(font_shabnam_bold);
tx_user.setTypeface(font_shabnam_light);
tx_pass.setTypeface(font_shabnam_light);
btn_login.setTypeface(font_shabnam_bold);
Animation ani_rtl = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_ltr);
Animation ani_ltr = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_rtl);
Animation ani_fade = AnimationUtils.loadAnimation(Login_page.this, R.anim.fade);
Animation ani_dtu = AnimationUtils.loadAnimation(Login_page.this, R.anim.animation_dtu);
tv_user.setVisibility(View.VISIBLE);
tv_user.startAnimation(ani_rtl);
tv_pass.setVisibility(View.VISIBLE);
tv_pass.startAnimation(ani_rtl);
tx_user.setVisibility(View.VISIBLE);
tx_user.startAnimation(ani_ltr);
tx_pass.setVisibility(View.VISIBLE);
tx_pass.startAnimation(ani_ltr);
btn_login.setVisibility(View.VISIBLE);
btn_login.startAnimation(ani_fade);
img_ghorme.startAnimation(ani_dtu);
}
#Override
protected void onPause() {
super.onPause();
finish();
}
}

Related

Text view is disappearing in android studio

my textview in activity configure disappears after setting it
the small text 192.168.0, is the text view appears only when i hit the save button
the textview is disappearing completely when i come back to this activity
edit: added my java code and my xml
package com.example.smartcharge;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Objects;
public class configure extends AppCompatActivity {
EditText ip, port;
public TextView tv;
Button SAVEBTN;
SharedPreferences sharedPreferences;
public static final String MyPREFERENCES = "MyPrefs";
public static final String IPSTR = "ipKey";
public static final String PORTSTR = "portKey";
public static final String TEXT1 = "textkey";
private String text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_configure);
Objects.requireNonNull(getSupportActionBar()).setDisplayShowTitleEnabled(false);
ip = findViewById(R.id.et1);
port = findViewById(R.id.et2);
SAVEBTN = findViewById(R.id.savebtn);
tv = findViewById(R.id.tv2);
SAVEBTN.setOnClickListener(v -> {
senddata();
});
loaddata();
updateviews();
}
public void senddata(){
sharedPreferences = PreferenceManager.getDefaultSharedPreferences(configure.this);
SharedPreferences.Editor editor = sharedPreferences.edit();
String ipstr = ip.getText().toString();
String portstr = port.getText().toString();
tv.setText(ipstr + "," + portstr);
editor.putString(IPSTR, ipstr);
editor.putString(PORTSTR, portstr);
editor.putString(TEXT1, tv.getText().toString());
editor.apply();
Toast.makeText(configure.this,"saved",Toast.LENGTH_LONG).show();
}
public void loaddata(){
sharedPreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
text1 = sharedPreferences.getString(TEXT1 , "");
}
public void updateviews(){
tv.setText(text1);
}
}
Below is xml code of my configure activity
<?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=".configure">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Broker Settings"
android:textSize="50sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.361"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.038" />
<TextView
android:id="#+id/tv2"
android:layout_width="327dp"
android:layout_height="49dp"
android:text="TextView"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.309"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.247" />
<Button
android:id="#+id/savebtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/btns"
android:text="save"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.08"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.572" />
<EditText
android:id="#+id/et2"
android:layout_width="141dp"
android:layout_height="wrap_content"
android:backgroundTint="#D1D1D1"
android:hint="port "
android:inputType="number"
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.096"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.444">
</EditText>
<EditText
android:id="#+id/et1"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:backgroundTint="#D1D1D1"
android:hint="ip Address"
android:inputType="number"
android:text="192.168.0."
android:textSize="25sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.163"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.339">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
any one have any clue of this behavior?, please help me if you can :)
The text disappears since the updating of your TextView takes place in onCreate.
#Override
protected void onResume() {
SAVEBTN.setOnClickListener(v -> {
senddata();
});
loaddata();
updateviews();
}
This should do the trick.
Go through the Activity lifecycle to see how this works. But basically moving methods to onResume should work.

How to stop fragments from being recreated in fragment transaction

I have an activity with multiple steps, each step being a fragment transaction with an edittext box, with a next/back button to navigate and initiate sending the users input data. I want to be able to move throughout the fragments with the text in each box staying static. I can move back no problem, but as soon as I click next the next fragment is recreated and everything is lost.
I tried:
using a dummy view in onCreateView
refetching the string in onCreate
setRetainInstance(true); in the constructor
Using a viewpager has other complications regarding receiving data which I was not able to solve despite many here attempting to help, so I have no choice but to use fragment transactions in my app.
main/FormActivity.java
package com.loopbreakr.formproject;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.FrameLayout;
public class FormActivity extends AppCompatActivity implements PageOne.ExitFormInteractionListener{
//DECLARE COMPONENTS
private FrameLayout fragmentContainer;
private Button backToMain;
private Button startForm;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//FIND COMPONENTS IN XML
setContentView(R.layout.activity_form);
fragmentContainer = findViewById(R.id.frame_layout);
backToMain = findViewById(R.id.return_button);
startForm = findViewById(R.id.start_button);
//SET LISTENERS/INTENTS FOR BUTTONS
backToMain.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
startForm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openFragments();
}
});
}
private void openFragments() {
PageOne fragment1 = PageOne.newInstance();
FragmentManager fragmentManager =getSupportFragmentManager();
FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
beginTransaction.addToBackStack(null);
beginTransaction.add(R.id.frame_layout,fragment1,"PageOne_tag");
beginTransaction.commit();
}
#Override
public void onFragmentInteraction(String answerOneText) {
}
}
One of my fragments PageOne.java
package com.loopbreakr.formproject;
import android.content.Context;
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
public class PageOne extends Fragment {
View restored;
private static final String SAVED = "text";
private String savedText;
private String answerOneString;
private EditText answerOne;
private ExitFormInteractionListener mListener;
public PageOne() {
setRetainInstance(true);
}
public static PageOne newInstance() {
PageOne fragment = new PageOne();
Bundle args = new Bundle();
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
savedText = getArguments().getString(SAVED);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
if(restored == null){
restored = inflater.inflate(R.layout.fragment_page_one, container, false);
}
answerOne = restored.findViewById(R.id.answer_one);
Button exitForm = restored.findViewById(R.id.exit_form);
Button goNextTwo = restored.findViewById(R.id.gonext_two);
exitForm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
returnToDesc();
}
});
goNextTwo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
answerOneString = answerOne.getText().toString();
sendToTwo(answerOneString);
openSecondFragment();
}
});
return restored;
}
private void sendToTwo(String answerOneText) {
if(mListener != null){
mListener.onFragmentInteraction(answerOneText);
}
}
private void openSecondFragment() {
PageTwo fragment2 = PageTwo.newInstance(answerOneString);
FragmentTransaction fragTransition = getFragmentManager().beginTransaction();
fragTransition.replace(R.id.frame_layout, fragment2,"PageTwo_TAG");
fragTransition.addToBackStack(null);
fragTransition.commit();
}
private void returnToDesc(){
FragmentManager fragTransBack = getFragmentManager();
fragTransBack.popBackStack();
}
public interface ExitFormInteractionListener{
void onFragmentInteraction(String answerOneText);
}
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
if (context instanceof ExitFormInteractionListener) {
mListener = (ExitFormInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
}
activity_form.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FormActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="0dp"
android:layout_height="32dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
</LinearLayout>
<TextView
android:id="#+id/activity_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="#string/activity_title"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:stateListAnimator="#null"/>
<Button
android:id="#+id/return_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:text="#string/return_text"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
android:stateListAnimator="#null"/>
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="#string/start_text"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintEnd_toEndOf="parent"
android:stateListAnimator="#null"/>
<FrameLayout
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/linearLayout"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
activity_page_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:clickable="true"
tools:context=".PageOne"
android:focusable="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:id="#+id/activity_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
android:text="#string/activity_title"
android:textSize="32sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<Button
android:id="#+id/exit_form"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginBottom="8dp"
android:text="#string/back"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
android:stateListAnimator="#null"/>
<Button
android:id="#+id/gonext_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="#string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:stateListAnimator="#null"/>
<EditText
android:id="#+id/answer_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:hint="#string/answer_one_hint"
android:inputType="textMultiLine|textAutoCorrect"
android:maxLines="4"
android:minLines="4"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/activity_name"
android:stateListAnimator="#null"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</RelativeLayout>
Thanks.

My app does not load an activity on the device

My android app links to a new activity to allow a user to create their profile and save the strings they use to a firebase database. However, the activity loads but is blank as shown below. This issue only seemed to arise after I added functionality to save user data to firebase.
blank activity
package com.example.eventfeed;
import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
#Override
public void onCreate(#Nullable Bundle savedInstanceState, #Nullable PersistableBundle persistentState) {
super.onCreate(savedInstanceState, persistentState);
this.setContentView(R.layout.create_profile_layout);
// Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" + ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
The xml files that format the page
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/create_profile_layout"
tools:context=".CreateProfileActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="?android:attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/main_page_toolbar"
layout="#layout/app_bar_layout">
</include>
<include layout="#layout/create_profile_content"></include>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer_view">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
Create profile activity xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".CreateProfileActivity"
android:id="#+id/create_profile_content"
tools:showIn="#layout/create_profile_layout">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="200dp"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/vibe_concert" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="56dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
android:scaleType="centerCrop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_goneMarginTop="100dp"
app:srcCompat="#drawable/vibe_image" />
<EditText
android:id="#+id/profile_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="158dp"
android:layout_marginLeft="158dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="156dp"
android:layout_marginRight="156dp"
android:layout_marginBottom="228dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
app:layout_constraintVertical_bias="0.553"
/>
<TextView
android:id="#+id/interest_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="224dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="#string/profile_interest"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.025"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
tools:text="Interests" />
<EditText
android:id="#+id/interests"
android:layout_width="200dp"
android:layout_height="87dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="140dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4"
app:layout_constraintVertical_bias="0.091" />
<TextView
android:id="#+id/profile_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="TextView"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.679"
app:layout_constraintStart_toEndOf="#+id/textView2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.544" />
<Button
android:id="#+id/createProfileBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.835"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/interests"
app:layout_constraintVertical_bias="0.473"
tools:text="Create Profile" />
</android.support.constraint.ConstraintLayout>
The method that is called to send the user to the create profile activity
private void LoginUser(){
setEmailStr(email.getText().toString().trim());
String Password = password.getText().toString().trim();
mAuth.signInWithEmailAndPassword(emailStr, Password)
.addOnCompleteListener(this, new
OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
finish();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
}else {
setCurrent_user_db(true);
Toast.makeText(ActivityLoginEmail.this, "couldn't login",
Toast.LENGTH_SHORT).show();
}
}
});
}
}
EDIT:After changing the xml file to a simple one with just a single button and adding a toast command to print if the activity loads nothing happens. Therefore the problem is most likely not within the xml files.
EDIT2:I found the solution by removing the PersistableBundle parameter from my oncreate method. Not exactly sure what this does to begin with if anyone would like to elaborate.
public class CreateProfileActivity extends AppCompatActivity {
public static final String INTEREST_KEY = "interest";
public static final String NAME_KEY = "name";
private DocumentReference mDocRef = RegisterActivity.getmDocRef();
private Button createBtn;
public void onCreate( Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.create_profile_layout);
Toast.makeText(this, "Create Activity", Toast.LENGTH_SHORT).show();
EditText interestsView = (EditText) findViewById(R.id.interests);
EditText nameView = (EditText) findViewById(R.id.profile_name);
String interests = interestsView.getText().toString();
String name= nameView.getText().toString();
Map<String, Object> dataToSave = new HashMap<>();
mDocRef = FirebaseFirestore.getInstance().document("users/" +
ActivityLoginEmail.getEmailStr() + "/profileInfo");
dataToSave.put(INTEREST_KEY, interests);
dataToSave.put(NAME_KEY, name);
mDocRef.set(dataToSave);
createBtn = (Button) findViewById(R.id.createProfileBtn);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(CreateProfileActivity.this, Profile.class);
startActivity(intent);
}
});
}
}
try by moveing finish() to the bottom of this condition
if (task.isSuccessful()){
currentUser = mAuth.getCurrentUser();
Toast.makeText(ActivityLoginEmail.this, "Create
Activity", Toast.LENGTH_SHORT).show();
setCurrent_user_db(false);
startActivity(new Intent(ActivityLoginEmail.this,
CreateProfileActivity.class));
finish();
}

code for image run in arraylist

i do array list with images thet i want them to run with 2 buttons thet go next pic and beck pic i dont know the code for this i thx all for the time you spend for help.
ackage com.example.hanansanag.mytourneyccreator;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.util.ArrayList;
/**
* Created by ssh on 25/12/2016.
*/
public class Players extends AppCompatActivity implements View.OnClickListener {
protected Button btnNext, btnBack;
protected String fname;
protected String Lname;
protected String team;
protected ImageView iv;
protected ArrayList array_image;
int i = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_pic);
ArrayList<Integer> array_image = new ArrayList<Integer>();
array_image.add(R.drawable.bacelona);
array_image.add(R.drawable.athlethiko);
array_image.add(R.drawable.arsenak);
array_image.add(R.drawable.chelsea);
array_image.add(R.drawable.dortmond);
array_image.add(R.drawable.city);
array_image.add(R.drawable.bayernunchen);
array_image.add(R.drawable.intermilan);
array_image.add(R.drawable.psj);
array_image.add(R.drawable.realmadrid);
array_image.add(R.drawable.leverpool);
array_image.add(R.drawable.milan);
array_image.add(R.drawable.juventus);
array_image.add(R.drawable.ashkelon);
array_image.add(R.drawable.macabiheifa);
array_image.add(R.drawable.macabitelaviv);
array_image.add(R.drawable.beitaryeroshlaim);
array_image.add(R.drawable.apoelbersheva);
iv = (ImageView) findViewById(R.id.imageView);
btnNext = (Button) findViewById(R.id.btnNextPic);
btnBack = (Button) findViewById(R.id.btnBeckPic);
btnBack.setOnClickListener(this);
btnNext.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
}
**what can i do here in this line of code thet the pic will move : **
#Override
public void onClick(View v) {
if (i <= 0 || i >= array_image.length) {
return;
}
if (btnNext == v) {
iv.setImageResource(array_image.get(i++));
} else if (btnBack == v) {
iv.setBackgroundResource(array_image.get(i--));
}
}
this is the xml;
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/teamimage">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView2" />
<ImageView
android:layout_height="80dp"
android:id="#+id/imageView"
android:layout_width="80dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="Lname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:id="#+id/Lname"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/imageView" />
<TextView
android:text="Fname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="12dp"
android:id="#+id/textView3" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true">
<Button
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnBeckPic"
android:background="#drawable/btnbeck"
android:layout_marginStart="30dp"
android:layout_alignBottom="#+id/Lname"
android:layout_toEndOf="#+id/textView2" />
<Button
android:background="#drawable/btnnext"
android:layout_width="20dp"
android:layout_height="15dp"
android:id="#+id/btnNextPic"
android:layout_below="#+id/imageView"
android:layout_toEndOf="#+id/btnBeckPic"
android:layout_marginStart="14dp" />
</GridLayout>
</RelativeLayout>
int currentImage = 0;
String[] strArr = new String[array_image.size()];
strArr = array_image.toArray(stockArr);
#Override
public void onClick(View v) {
if (btnNext == v) {
currentImage++;
currentImage = currentImage % strArr .length;
iv.setImageResource(strArr .length[currentImage]);
} else if (btnBack == v) {
currentImage--;
currentImage = (currentImage + strArr .length) % strArr .length;
iv.setImageResource(strArr[currentImage]);
}
}

Multiple Checkboxes to Run Different Methods

Here is my .java:
package com.example.clarkrubberfoam;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class PriceEstimator extends ActionBarActivity implements OnCheckedChangeListener {
double total = 0;
double md_foam25 = 0.31, md_foam50 = 0.63, md_foam75 = 0.81, md_foam100 = 1.01, md_foam125 = 1.25, md_foam150 = 1.34;
double hd_foam25 = 0.34, hd_foam50 = 0.66, hd_foam75 = 0.91, hd_foam100 = 1.19, hd_foam125 = 1.48, hd_foam150 = 1.53;
double ye_foam25 = 0.59, ye_foam50 = 0.94, ye_foam75 = 1.41, ye_foam100 = 1.67, ye_foam125 = 2.09, ye_foam150 = 2.34;
double oe_foam25 = 0.70, oe_foam50 = 1.37, oe_foam75 = 1.69, oe_foam100 = 2.20, oe_foam125 = 2.64, oe_foam150 = 3.16;
CheckBox chkMD = (CheckBox) findViewById(R.id.checkBox1);
CheckBox chkHD = (CheckBox) findViewById(R.id.checkBox2);
CheckBox chkYE = (CheckBox) findViewById(R.id.checkBox3);
CheckBox chkOE = (CheckBox) findViewById(R.id.checkBox4);
/* Called when first opening the app */
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
//Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if (buttonView == chkMD) {
if(isChecked) {
mediumDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkHD){
if(isChecked) {
highDensityPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkYE) {
if(isChecked) {
yellowEnduroPrice();
} else {
total = total + 0;
}
}
if (buttonView == chkOE) {
if(isChecked) {
orangeEnduroPrice();
} else {
total = total + 0;
}
}
}
public void mediumDensityPrice() {
Toast.makeText(getBaseContext(), "Medium Density Selected", Toast.LENGTH_LONG).show();
}
public void highDensityPrice() {
Toast.makeText(getBaseContext(), "High Density Selected", Toast.LENGTH_LONG).show();
}
public void yellowEnduroPrice() {
Toast.makeText(getBaseContext(), "Yellow Enduro Selected", Toast.LENGTH_LONG).show();;
}
public void orangeEnduroPrice() {
Toast.makeText(getBaseContext(), "Orange Enduro Selected", Toast.LENGTH_LONG).show();;
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_price_estimator,
container, false);
return rootView;
}
}
Here is my .xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.clarkrubberfoam.PriceEstimator$PlaceholderFragment" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_marginTop="2dp"
android:text="#string/price_estimator"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:text="#string/medium_density" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox1"
android:layout_below="#+id/checkBox1"
android:text="#string/high_density" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/checkBox1"
android:layout_alignBottom="#+id/checkBox1"
android:layout_marginLeft="27dp"
android:layout_toRightOf="#+id/checkBox1"
android:text="#string/enduro_yellow" />
<CheckBox
android:id="#+id/checkBox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox3"
android:layout_below="#+id/checkBox3"
android:text="#string/enduro_orange" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkBox2"
android:layout_below="#+id/checkBox2"
android:layout_marginTop="10dp"
android:text="#string/enter_width" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/numberPicker1"
android:layout_below="#+id/numberPicker1"
android:layout_marginTop="30dp"
android:onClick="totalPrice"
android:text="#string/calculate_me" />
<NumberPicker
android:id="#+id/numberPicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="16dp" />
<NumberPicker
android:id="#+id/numberPicker2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/numberPicker1"
android:layout_centerHorizontal="true" />
<NumberPicker
android:id="#+id/numberPicker3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView4"
android:layout_alignTop="#+id/numberPicker2" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/numberPicker1"
android:layout_alignRight="#+id/numberPicker2"
android:text="#string/enter_length" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/numberPicker1"
android:layout_alignParentRight="true"
android:text="#string/enter_thickness" />
My problem is that when I run the program, it crashes immediately. I'm not sure what's going wrong. I am currently trying to adapt another multiple checkbox program to work for my needs: 'http://pulse7.net/android/android-check-box-example/'.
Essentially I have four grades of foam, and multiple thicknesses, so each grade, if checked in the checkbox, will need to lead to a different method. Currently, the Toast.makeText parts are just a placeholder for what will go there eventually once this part gets figured out :)
Cheers!
You must findViewById() in onCreate() method after setContentView() call.
public class MainActivity extends Activity {
CheckBox chkMD;
CheckBox chkHD;
CheckBox chkYE;
CheckBox chkOE;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_price_estimator);
chkMD = (CheckBox) findViewById(R.id.checkBox1);
chkHD = (CheckBox) findViewById(R.id.checkBox2);
chkYE = (CheckBox) findViewById(R.id.checkBox3);
chkOE = (CheckBox) findViewById(R.id.checkBox4);
// Attach listeners to the checkboxes
chkMD.setOnCheckedChangeListener(this);
chkHD.setOnCheckedChangeListener(this);
chkYE.setOnCheckedChangeListener(this);
chkOE.setOnCheckedChangeListener(this);
}
}

Categories