Successfull Build in Android Studio still crashes on Device - android

I'm pretty much a beginner still and I've tried to make an App to ease my work in the Lab. It's just some basic calculation. I want to make an App that is compatible to the most devices, in case its useful information. Even though the App seems to Build successfully, as soon as it starts on my Device it just crashes immediatelly. Files used are listed below. I tried all the hints Android Studio gave me with no result. App still crashed.
Would be very thankful for some hints and constructive feeback!
Main Activity.java
package com.e.concalc;
import androidx.appcompat.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 {
private EditText protein_sp;
private EditText salt_sp;
private EditText protein_st;
private EditText salt_st;
private EditText volume_sp;
private TextView tv_resultH2O;
private TextView tv_resultSalt;
private TextView tv_resultProtein;
private Button button1;
public MainActivity(TextView tv_resultH2O, TextView tv_resultSalt, TextView tv_resultProtein, Button button1) {
this.tv_resultH2O = tv_resultH2O;
this.tv_resultSalt = tv_resultSalt;
this.tv_resultProtein = tv_resultProtein;
this.button1 = button1;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
protein_sp = findViewById(R.id.edit1);
protein_st = findViewById(R.id.edit2);
salt_sp = findViewById(R.id.edit3);
salt_st = findViewById(R.id.edit4);
volume_sp = findViewById(R.id.edit5);
button1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
makeCalculations();
}
});
}
private void makeCalculations() {
double p_sp = Double.valueOf(protein_sp.getText().toString());
double p_st = Double.valueOf(protein_st.getText().toString());
double s_sp = Double.valueOf(salt_sp.getText().toString());
double s_st = Double.valueOf(salt_st.getText().toString());
double v_sp = Double.valueOf(volume_sp.getText().toString());
double resultH2O;
double resultSalt;
double resultProtein;
resultProtein = p_sp * v_sp / p_st;
resultSalt = s_sp * v_sp / s_st;
resultH2O = v_sp - resultProtein - resultSalt;
tv_resultH2O.setText(Double.toString(resultH2O));
tv_resultSalt.setText(Double.toString(resultSalt));
tv_resultProtein.setText(Double.toString(resultProtein));
}
}
activity_main.xml - Layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.133"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Protein2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.77"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.070" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt1"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Salt2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.21" />
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/SampleVolume"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.35" />
<EditText
android:id="#+id/edit1"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text1"
app:layout_constraintTop_toBottomOf="#+id/text1"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit2"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text2"
app:layout_constraintTop_toBottomOf="#+id/text2"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit4"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text4"
app:layout_constraintTop_toBottomOf="#+id/text4"
android:importantForAutofill="no" />
<EditText
android:id="#id/edit3"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text3"
app:layout_constraintTop_toBottomOf="#+id/text3"
android:importantForAutofill="no" />
<EditText
android:id="#+id/edit5"
android:layout_width="100dp"
android:layout_height="40dp"
android:textSize="15sp"
android:inputType="number"
android:hint="#string/hint"
app:layout_constraintStart_toStartOf="#+id/text5"
app:layout_constraintTop_toBottomOf="#+id/text5"
android:importantForAutofill="no" />
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginBottom="128dp"
android:text="#string/button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.158"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="120dp"
android:layout_height="40dp"
android:layout_marginStart="51dp"
android:layout_marginLeft="51dp"
android:text="#string/button2"
app:layout_constraintBottom_toBottomOf="#+id/button1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.745"
app:layout_constraintStart_toEndOf="#+id/button1"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/button1" />
<TextView
android:id="#+id/tv_resultH2O"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.1"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultSalt"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintVertical_bias="0.6"/>
<TextView
android:id="#+id/tv_resultProtein"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:textAppearance="?android:attr/textAppearanceLarge"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintHorizontal_bias="0.9"
app:layout_constraintVertical_bias="0.6"/>
</androidx.constraintlayout.widget.ConstraintLayout>

You forgot to findViewById the button1.So in onCreate method, before button1.setOnClickListener.... add this line:
button1 = findViewById(R.id.button1);

Remove your custom constructor.
Activities must have a no-argument constructor that Android uses to create your class and when you define any override then the no-argument constructor is no longer automatically created for you in Java...

Related

EditText.getText().toString() is not returning current value

I´m trying to get the text that was inserted into a EditText in the UI.
The UI looks like this. In a fragment the EditText for name has a default value "Hello".
After the user has entered a new value (for example "Hello2") I´d like to get the new value when the user clicks the Add Button.
But what I recive is still the default value "Hello".
My Code looks like this:
XML
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
JAVA Code in Fragment
public class AddIngredient extends Fragment{
//Controls
public EditText etN;
public EditText etK;
public EditText etF;
public EditText etC;
public EditText etP;
public TextView tv1;
//ViewModel
AddIngredientViewModel model;
#Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_addingredient, container, false);
// Get the values
tv1 = rootView.findViewById(R.id.textview_addingredient);
etN = rootView.findViewById(R.id.editText_name);
etK = rootView.findViewById(R.id.editText_kcal);
etF = rootView.findViewById(R.id.editText_fat);
etC = rootView.findViewById(R.id.editText_carbs);
etP = rootView.findViewById(R.id.editText_protein);
// Inflate the layout for this fragment
return rootView;
}
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.fragmentbutton_addingredient).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Get current values
String name = etN.getText().toString();
int kcal = Integer.parseInt(etK.getText().toString().trim());
int fat = Integer.parseInt(etF.getText().toString().trim());
int carbs = Integer.parseInt(etC.getText().toString().trim());
int protein = Integer.parseInt(etP.getText().toString().trim());
model = new ViewModelProvider(requireActivity()).get(AddIngredientViewModel .class);
Ingredient ingredient = new Ingredient(name, kcal, fat, carbs, protein){};
model.SaveIngredient(ingredient);
}
});
}
}
So has anyone an idea, what I have tho change to recive the current value? Thanks!
-- EDIT --
Entire XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/screen_background"
tools:context=".fragments.AddIngredient">
<TextView
android:id="#+id/textview_addingredient"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="68dp"
android:text="Add Ingredient"
android:textColor="#color/headercolor"
android:textSize="#dimen/header_fontsize"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.126"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
<!-- Name -->
<TextView
android:id="#+id/textView_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Name"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textview_addingredient"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_name"
app:layout_constraintVertical_bias="0.0" />
<!-- Kcal -->
<TextView
android:id="#+id/textView_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Kcal"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_name"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_kcal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_kcal"
app:layout_constraintVertical_bias="0.0" />
<!-- Fat -->
<TextView
android:id="#+id/textView_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Fat"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_kcal"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_fat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_fat"
app:layout_constraintVertical_bias="0.0" />
<!-- Carbs -->
<TextView
android:id="#+id/textView_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Carbs"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.075"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_fat"
app:layout_constraintVertical_bias="0.050" />
<EditText
android:id="#+id/editText_carbs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_carbs"
app:layout_constraintVertical_bias="0.0" />
<!-- Protein -->
<TextView
android:id="#+id/textView_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:text="Protein"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.078"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_carbs"
app:layout_constraintVertical_bias="0.052" />
<EditText
android:id="#+id/editText_protein"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="number"
android:text=""
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.157"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textView_protein"
app:layout_constraintVertical_bias="0.0" />
<Button
android:id="#+id/fragmentbutton_addingredient"
android:layout_width="#dimen/button_width"
android:layout_height="#dimen/button_height"
android:background="#color/button_background"
android:gravity="left|center_vertical"
android:padding="20dp"
android:text="Add"
android:textColor="#color/button_foreground"
android:textSize="#dimen/button_fontsize"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.243"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/editText_protein"
app:layout_constraintVertical_bias="0.250" />
</androidx.constraintlayout.widget.ConstraintLayout>
Remove the default static text set to the editTextName field in your xml layout file. Due to this, the value is always getting stored as Hello. You shouldnt set text in EditTextview.
<EditText
android:id="#+id/editText_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#color/backgroundtint_color"
android:ems="10"
android:inputType="textPersonName"
android:textColor="#color/ntext_foreground"
android:textSize="#dimen/ntext_fontsize"
android:text="Hello" //Remove this line.

Android Studio null exception error on a ListView with custom ListAdapter

[Edited] This is the mainScreen activity xml as requested by some of you
<?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:design="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F6F6F6">
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:fontFamily="#font/montserrat"
android:text="Pocket Budget"
android:textColor="#77dd77"
android:textSize="26dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView9"
android:layout_width="138dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:text="January"
android:textColor="#443c3c"
android:textSize="16dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/textView8"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView8"
app:layout_constraintTop_toTopOf="#+id/textView8" />
<View
android:id="#+id/view5"
android:layout_width="match_parent"
android:layout_height="56dp"
android:layout_marginTop="7dp"
android:background="#FFF"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView8" />
<View
android:id="#+id/view2"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginTop="3dp"
android:background="#77dd77"
app:layout_constraintBottom_toBottomOf="#+id/view5"
app:layout_constraintEnd_toEndOf="#+id/view5"
app:layout_constraintTop_toTopOf="#+id/view5" />
<View
android:id="#+id/view3"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="#779ecb"
app:layout_constraintBottom_toBottomOf="#+id/view5"
app:layout_constraintStart_toStartOf="#+id/view5"
app:layout_constraintTop_toTopOf="#+id/view5" />
<View
android:id="#+id/view4"
android:layout_width="116dp"
android:layout_height="50dp"
android:layout_marginBottom="3dp"
android:layout_marginEnd="3dp"
android:layout_marginStart="3dp"
android:layout_marginTop="3dp"
android:background="#ff6961"
app:layout_constraintBottom_toBottomOf="#+id/view5"
app:layout_constraintEnd_toStartOf="#+id/view2"
app:layout_constraintStart_toEndOf="#+id/view3"
app:layout_constraintTop_toTopOf="#+id/view5" />
<TextView
android:id="#+id/textView12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="#font/montserrat"
android:text="Balance"
android:textColor="#FFF"
android:textSize="9.5dp"
app:layout_constraintBaseline_toBaselineOf="#+id/textView2"
app:layout_constraintStart_toStartOf="#+id/view2" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="#font/montserrat"
android:text="Expense"
android:textColor="#FFF"
android:textSize="9.5dp"
design:layout_constraintBaseline_toBaselineOf="#+id/textView"
design:layout_constraintStart_toStartOf="#+id/view4" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="2dp"
android:fontFamily="#font/montserrat"
android:text="Income"
android:textColor="#FFF"
android:textSize="9.5dp"
design:layout_constraintStart_toStartOf="#+id/view3"
design:layout_constraintTop_toTopOf="#+id/view3" />
<TextView
android:id="#+id/textView4"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="#+id/view3"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/view3"
app:layout_constraintTop_toTopOf="#+id/textView6"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:id="#+id/textView6"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/view4"
app:layout_constraintEnd_toEndOf="#+id/view4"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="#+id/view4"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView11"
android:layout_width="116dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="12dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:fontFamily="#font/montserrat"
android:gravity="center"
android:text="₱ 0"
android:textColor="#FFF"
android:textSize="20dp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="#+id/view2"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="#+id/view2"
app:layout_constraintTop_toTopOf="#+id/textView6" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="5dp"
android:fontFamily="#font/montserrat"
android:text="Transactions"
android:textColor="#808080"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view5" />
<ListView
android:id="#+id/transacList"
android:layout_width="344dp"
android:layout_height="438dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:background="#FFF"
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/textView5"
app:layout_constraintVertical_bias="0.0"></ListView>
</android.support.constraint.ConstraintLayout>
This is the code for my custom ListView I don't know what's wrong because I'm just a beginner in android development
package com.example.admin.test2;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import org.w3c.dom.Text;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
new Timer().schedule(new TimerTask(){
#Override
public void run(){
startActivity(new Intent(MainActivity.this, ScreenOne.class));
}
}, 2500);
mListView = (ListView) findViewById(R.id.transacList);
CustomAdaptor cAdaptor = new CustomAdaptor();
mListView.setAdapter(cAdaptor);
}
class CustomAdaptor extends BaseAdapter{
#Override
public int getCount() {
return 0;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = getLayoutInflater().inflate(R.layout.customlist, null);
ImageView mImageView = (ImageView) view.findViewById(R.id.imageView2);
TextView mTextView = (TextView) view.findViewById(R.id.textView2);
TextView mTextView2 = (TextView) view.findViewById(R.id.textView3);
mImageView.setImageResource(R.drawable.car);
mTextView.setText("Transportation");
mTextView2.setText("₱ 100");
return view;
}
}
}
And this is the code for my custom listview layout that will be implemented in my mainScreen for viewing the image icon with the label and amount
<?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">
<ImageView
android:id="#+id/imageView2"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="19dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.015"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/textView3"
android:layout_width="120dp"
android:layout_height="25dp"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:autoSizeTextType="uniform"
android:text="Label"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="#+id/imageView2"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="#+id/imageView2"
app:layout_constraintVertical_bias="1.0" />
<TextView
android:layout_width="147dp"
android:layout_height="21dp"
android:layout_marginEnd="18dp"
android:layout_marginStart="8dp"
android:autoSizeTextType="uniform"
android:gravity="right"
android:text="Amount"
app:layout_constraintBottom_toBottomOf="#+id/textView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toEndOf="#+id/textView3"
app:layout_constraintTop_toTopOf="#+id/textView3"
app:layout_constraintVertical_bias="1.0" />
</android.support.constraint.ConstraintLayout>
This is the error that I encountered.
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.admin.test2, PID: 9518
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.test2/com.example.admin.test2.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
at com.example.admin.test2.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:7174)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032) 
at android.app.ActivityThread.-wrap11(Unknown Source:0) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696) 
at android.os.Handler.dispatchMessage(Handler.java:105) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6944) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374) 
someone please help solving this problem the application when run is force closing and I don't know what is wrong.
It looks like your ListView "mListView" is not initialized yet, make sure your you are properly initializing it, will be helpful if you share your activity_main.xml code.
You don't have any listviews on your layout, hence you get this exception. Try adding something like this:
<ListView
android:id="#+id/transacList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="19dp"
android:layout_marginTop="8dp" />
Margins and paddings should be designed for your usage of the listview.
You didn't initialize the list view, add this line before setting the adapter.
mListView = findViewById(R.id.*your list view id*);
ListView is null, so you can't set it's adapter.
Hence you get this error:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setAdapter(android.widget.ListAdapter)' on a null object reference
findViewById(R.id.transacList) returns null beacuse the ID is wrong or the casting is wrong or your custom listview is messed up. Can't judge that from the code you provided.

ScrollView inside a fragment with TabLayout is not scrolling?

I am making a sign up and sign in form using TabLayout. The height of the signup form will be long, but due to some reasons I am not able to add ScrollView inside fragment.
Here is what I am doing: https://imgur.com/9p42i43
Here is my layout Main Activity
<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"
android:background="#color/appBackground"
tools:context=".LoginActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<View
android:id="#+id/logo_view"
android:layout_width="0dp"
android:layout_height="70dp"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/toolbar2" />
<android.support.design.widget.TabLayout
android:id="#+id/signin_signup_tab_lay"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:background="#color/secondaryColor"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/logo_view"
app:tabBackground="#drawable/curve_login"
app:tabIndicatorColor="#color/yellow_btn"
app:tabSelectedTextColor="#color/primaryColor"
app:tabTextAppearance="#style/tabstyle"
app:tabTextColor="#7d98c3">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/signin_signup_tab_lay"></android.support.v4.view.ViewPager>
</android.support.constraint.ConstraintLayout>
Java Main Activity
package com.example.mobilestyx.fantasy_arena;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.widget.TextView;
public class LoginActivity extends AppCompatActivity {
TabLayout tabLayout;
private int[] tabIcons = {
R.drawable.curve_login,
R.drawable.curve_login
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
tabLayout = (TabLayout) findViewById(R.id.signin_signup_tab_lay);
tabLayout.addTab(tabLayout.newTab().setText("Signin"));
tabLayout.addTab(tabLayout.newTab().setText("Signup"));
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
//
// setupTabIcons();
final ViewPager viewPager = (ViewPager)findViewById(R.id.pager);
final PagerAdapter adapter = new PagerAdapter(getSupportFragmentManager(),tabLayout.getTabCount());
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
private void setupTabIcons() {
TextView tabOne = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabLayout.getTabAt(0).setCustomView(tabOne);
TextView tabTwo = (TextView) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}}
Here is my fragment layout
<?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:id="#+id/constraint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context=".SignupFragment">
<!-- TODO: Update blank fragment layout -->
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:background="#drawable/login_fb_btn"
android:fontFamily="#font/mavenpro_regular_font"
android:text="Signup with Facebook"
android:textColor="#ffffff"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="24dp"
android:fontFamily="#font/mavenpro_regular_font"
android:gravity="center"
android:text="OR"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button3" />
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2">
<EditText
android:id="#+id/signup_name_et"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/startborder_input"
android:ems="10"
android:fontFamily="#font/mavenpro_regular_font"
android:hint="John Doe"
android:inputType="textPersonName"
android:paddingLeft="15dp" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="22dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Full Name"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Email"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView2" />
<android.support.v7.widget.CardView
android:id="#+id/cardView3"
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3">
<EditText
android:id="#+id/signup_email_et"
style="#style/MavenproEdittextStyle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/startborder_input"
android:ems="10"
android:hint="johndoe#mail.com"
android:inputType="textEmailAddress"
android:paddingLeft="15dp" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Phone"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView3" />
<android.support.v7.widget.CardView
android:id="#+id/cardView4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:cardCornerRadius="5dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView4">
<EditText
android:id="#+id/signup_phone_et"
style="#style/MavenproEdittextStyle"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/startborder_input"
android:ems="10"
android:hint="000-000-0000"
android:inputType="number"
android:paddingLeft="15dp"
android:textColor="#000000" />
</android.support.v7.widget.CardView>
<TextView
android:id="#+id/textView5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="Gender"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/cardView4" />
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="0dp"
android:layout_height="30dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
<RadioButton
android:id="#+id/signup_gender_m_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Male" />
<RadioButton
android:id="#+id/signup_gender_f_rb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Female" />
</RadioGroup>
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="32dp"
android:text="DOB"
android:textColor="#000000"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/radioGroup" />
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginEnd="24dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginStart="24dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="date" />
</android.support.v7.widget.CardView></android.support.constraint.ConstraintLayout>
Java Fragment
package com.example.mobilestyx.fantasy_arena;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class SignupFragment extends Fragment {
public SignupFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_signup, container, false);
}
}
While using Constraint Layout, try using NestedScrollView for scrolling.
Make sure your ScrollView has constraints to parent which is ConstraintLayout view in each edges.

One of the Android Button work while the other does not

I did my best to solve the problem and I hope someone would be able to help me finding a solution.
I delcared the following layout for my activity:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#android:color/holo_orange_light"
android:visibility="visible"
tools:context="com.example.youssef.mylocation.Main2Activity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/menumessage"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText1"
android:layout_width="217dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="128dp"
android:ems="10"
android:hint="#string/phone_number"
android:inputType="phone"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="196dp"
android:ems="10"
android:hint="#string/address"
android:inputType="textPersonName"
android:textColor="#android:color/holo_orange_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="#string/place_name"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="340dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="40dp"
android:layout_marginTop="24dp"
android:textColor="#color/Color"
android:textSize="18sp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2" />
<Button
android:id="#+id/CancelBtn"
android:layout_width="88dp"
android:layout_height="48dp"
android:layout_marginBottom="40dp"
android:layout_marginStart="32dp"
android:background="#android:color/holo_red_dark"
android:text="#string/cancel"
android:onClick="Cancelonbuttonclickfunc"
android:textColor="#color/white"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="#string/city"
android:inputType="textPersonName"
android:textColor="#color/Color"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2" />
<TextView
android:id="#+id/textView6"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="144dp"
android:text="Required !"
android:textColor="#android:color/holo_red_dark"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/editText1"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/AddBtn"
android:layout_width="124dp"
android:layout_height="60dp"
android:layout_marginStart="96dp"
android:layout_marginTop="464dp"
android:text="Add"
android:background="#color/Color"
android:onClick="buttonClickFunction"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintStart_toEndOf="#+id/CancelBtn"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The related java code is the following:
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;
import android.widget.Toast;
public class Main2Activity extends AppCompatActivity {
TextView textView;
TextView laltitude;
TextView longitude;
Bundle bundle;
Double lal;
Double longt;
EditText editText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
textView=(TextView)findViewById(R.id.textView6);
editText = (EditText) findViewById(R.id.editText1);
laltitude =(TextView)findViewById(R.id.textView2);
longitude =(TextView)findViewById(R.id.textView3);
bundle = getIntent().getExtras();
lal = bundle.getDouble("laltitude");
longt = bundle.getDouble("longitude");
laltitude.setText("laltitude : " + lal.toString());
longitude.setText("longitude : " + longt.toString());
//hhhhhhhhhhhhhhhhh going back to main activity
// CancelButton.setOnClickListener(new View.OnClickListener() {
// public void onClick(View v) {
//
// }
// });
//hhhhhhhhhhhhhhh
// AddButton.setOnClickListener({});
// if (editText.getText() == null)
// textView.setVisibility(View.VISIBLE);
// else
// textView.setVisibility(View.INVISIBLE);
// });
}
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG);
if (editText.getText().toString() == "")
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
public void Cancelonbuttonclickfunc(View view) {
startActivity(new Intent(Main2Activity.this, MainActivity.class));
}
}
The problem is that only one button seems to work (cancelbutton) but the
but the add button does not seem work.
I did declare the button and use the findViewById method but no luck so far.
In your button click method you're comparing strings using the == operator which tests for reference equality and not value equality. This is why your textView's visibility never get set to VISIBLE. To check for value equality you should use equals method instead, like this:
public void buttonClickFunction(View view) {
Toast.makeText(getApplicationContext(),editText.getText().toString(),Toast.LENGTH_LONG).show();
if (editText.getText().toString().equals(""))
textView.setVisibility(View.VISIBLE);
else
textView.setVisibility(View.INVISIBLE);
}
Note that you also forgot to put .show() at the end of your Toast.
Try this
mBtnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final String text= edittext.getText().toString().trim();
Toast.makeText(getApplicationContext(),text,Toast.LENGTH_LONG).show();
if (text.length() <= 0) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.INVISIBLE);
}
}
});

How to loop TextView and PlainText?

I am editing the text view and plain text in the xml file always, when the new options comes in HashMAp. But, I am supposed to give the new options or more number of options in Hashmap and that should automatically change the new options in interface.
For example here there are three options in the map
gateway_ip: 162.198.23.10
net_mask: 178.465.37.34
server_ip: 243.798.76.59
I should be able to add more options or edit options in the HashMap and that should be automatically updated in the interface like this.
gateway_ip: 162.198.23.10
net_mask: 178.465.37.34
server_ip: 243.798.76.59
URL: 345.678.456.34
ip : 901.234.123.45
Basically I should change in hashmap that should update in interface automatically. Can anyone help me here ?
[Interface image][1]
/-------------------------MainActivity.java--------------------------/
package com.puchagmail.ganesh.trail2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import java.util.HashMap;
import static android.R.attr.data;
public class MainActivity extends AppCompatActivity {
TextView gateWay ;
TextView netMask;
TextView serverIp;
EditText gW;
EditText nM;
EditText sIp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gateWay = (TextView) findViewById(R.id.textView);
netMask = (TextView) findViewById(R.id.textView2);
serverIp = (TextView) findViewById(R.id.textView3);
gW = (EditText) findViewById(R.id.editText );
nM = (EditText) findViewById(R.id.editText2);
sIp = (EditText) findViewById(R.id.editText3 );
setValues(dummyValues());
}
public void setValues(HashMap<String,String>data){
gW.setText(data.get("gateway_ip"));
nM.setText(data.get("net_mask"));
sIp.setText(data.get("server_ip"));
}
public HashMap<String,String> dummyValues(){
HashMap<String,String> data = new HashMap<>();
data.put("gateway_ip","162.198.23.10");
data.put("net_mask","178.465.37.34");
data.put("server_ip","243.798.76.59");
return data;
}
}
/---------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.puchagmail.ganesh.trail2.MainActivity">
<TextView
android:id="#+id/textView2"
android:layout_width="66dp"
android:layout_height="39dp"
android:text="net_mask"
android:layout_marginLeft="24dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="34dp"
app:layout_constraintTop_toBottomOf="#+id/textView" />
<TextView
android:id="#+id/textView"
android:layout_width="77dp"
android:layout_height="39dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="96dp"
android:text="gateway_ip"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView3"
android:layout_width="67dp"
android:layout_height="37dp"
android:layout_marginLeft="24dp"
android:layout_marginTop="34dp"
android:text="server_ip"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintBottom_toBottomOf="parent"
android:layout_marginBottom="8dp"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="78dp"
android:ems="10"
android:inputType="textPersonName"
android:text="192.168.85.100"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="10dp"
android:ems="10"
android:inputType="textPersonName"
android:text="254.100.34.567"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView3"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText2"
app:layout_constraintVertical_bias="0.078" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="44dp"
android:layout_marginTop="32dp"
android:ems="10"
android:inputType="textPersonName"
android:text="465.798.86.345"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toRightOf="#+id/textView2"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/editText" />
</android.support.constraint.ConstraintLayout>
You can use android two way data binding using which you can achieve following things.
1) If you will change in hashmap manually then UI will automatically update.
2) If will enter any thing in edit text that will update in hashmap and on UI as well.
so you may learn about data binding using following link.
https://developer.android.com/topic/libraries/data-binding/index.html

Categories

Resources