Resources$NotFoundException: String resource ID [duplicate] - java

This question already has answers here:
android.content.res.Resources$NotFoundException: String resource ID #0x0
(8 answers)
Closed 5 years ago.
I try to show in MainActivity from another class by getter a value.
1st I make object and insert to him value from editText.
App crash with android.content.res.Resources$NotFoundException: String resource ID #0x28 What is wrong here ?
Check code:
public class MainActivity extends AppCompatActivity {
EditText editText, editText2;
Button button, button2;
TextView tV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button2 = findViewById(R.id.button2);
editText = findViewById(R.id.editText);
editText2 = findViewById(R.id.editText2);
tV = findViewById(R.id.textView);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int a = Integer.valueOf(editText.getText().toString());
Calculations calc = new Calculations(a){
};
tV.setText(calc.getResult());
}
});
}
2nd Class
public class Calculations {
public int getResult() {
return result;
}
public int result;
public Calculations(int a){
result = a * 10;
}
public Calculations(int a, int b){
result = a * b;
}
}
Here is my layout, maybe this will help
Here is my layout, maybe this will help
<?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="ge.ger.training.MainActivity">
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.22" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.222"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.351" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.121"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Transfer value 1 and 2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.12"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.668" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Result"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.109"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.838" />
</android.support.constraint.ConstraintLayout>

Replace your line with below in onClickListener
Calculations calc=new Calculations(a);
tV.setText(String.valueOf(calc.getResult()));

Related

How to set a textView to visible after an onClick event and Activity change?

I have three passages in my scrollview that need to each become visible after an onclick event on one of three buttons.
I have currently set them to all invisible. And since I cannot get it to work, I am only trying it out with one of the passages.
Because of this I created a private textview constant for only the first passage. But after I pass the intent to switch the activity, I also try to turn the view on that package to visible.
I have included my MainActivity.java and the xml file I used to set invisible.
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView passage1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
passage1 = findViewById(R.id.passage_1);
}
public void launchPassageOne(View view) {
passage1.setVisibility(view.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageTwo(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
public void launchPassageThree(View view) {
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
}
<?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=".PassageActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<EditText
android:id="#+id/passage_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage1"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage2"
android:visibility="invisible"/>
<EditText
android:id="#+id/passage_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:gravity="start|top"
android:inputType="textMultiLine"
android:text="#string/passage3"
android:visibility="invisible"/>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
My program just crashes. And I cannot find any error messages.
How can I make the packages visible whenever I want the activity to change?
There are three passages that I want to each become visible for the respective button, then turn invisible if the back button is pressed.
It seams the three views are in the started activity. And so you can't change their visibility because they haven't been created.
Add this before you start the activity
intent.putExtra("passageNum", 1)
Then call startActivity(intent)
In PassageAactivity onCreate do the following :
If (getIntent().hasExtra("passageNum") && getIntent().getExtras().getInt("passageNum") == 1)
passage1.setVisibility(View.VISIBLE)
And so on for the other views
passage1.setVisibility(View.VISIBLE)
read more about views and how to modify their behavior here :
https://developer.android.com/reference/android/view/View
Use View.VISIBLE, capital V, it's a integer constant from the View class. Remove the View argument from the method launchPassageOne:
public void launchPassageOne() {
passage1.setVisibility(View.VISIBLE);
Intent intent = new Intent(this, PassageActivity.class);
startActivity(intent) ;
}
Image click hereWhatever I understood with your code I got that you are not initializing your methods in On create, whatever defined outside the On create will not be used until or unless called from inside On create method.
Designed some code may help you understanding in a better way.
In below code, I made text views scrollable, but you can only scroll if text is too long to fill the entire textview.
MainActivity.java
package com.example.threebuttons;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText edit1;
EditText edit2;
EditText edit3;
Button btn1;
Button btn2;
Button btn3;
Button btnV;
Button btnI;
TextView t1;
TextView t2;
TextView t3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// defining id for edit texts
edit1=findViewById(R.id.passage_1);
edit2=findViewById(R.id.passage_2);
edit3=findViewById(R.id.passage_3);
// defining id for buttons
btn1=findViewById(R.id.button_1);
btn2=findViewById(R.id.button_2);
btn3=findViewById(R.id.button_3);
btnV=findViewById(R.id.btnvisi);
btnI=findViewById(R.id.btninvisi);
// defining id for text views
t1=findViewById(R.id.textview1);
t2=findViewById(R.id.textview2);
t3=findViewById(R.id.textview3);
// making text views scrollable
t1.setMovementMethod(new ScrollingMovementMethod());
t2.setMovementMethod(new ScrollingMovementMethod());
t3.setMovementMethod(new ScrollingMovementMethod());
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setText(edit1.getText().toString());
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t2.setText(edit2.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t3.setText(edit3.getText().toString());
}
});
btnV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Making passages Visible
t1.setVisibility(View.VISIBLE);
t2.setVisibility(View.VISIBLE);
t3.setVisibility(View.VISIBLE);
}
});
btnI.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
t1.setVisibility(View.INVISIBLE);
t2.setVisibility(View.INVISIBLE);
t3.setVisibility(View.INVISIBLE);
}
});
}
}
Set activitymain.xml as below
<?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="#2196F3"
tools:context=".MainActivity">
<EditText
android:id="#+id/passage_1"
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:ems="10"
android:hint="passage 1"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.016" />
<EditText
android:id="#+id/passage_2"
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:ems="10"
android:hint="Passage 2"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.102" />
<EditText
android:id="#+id/passage_3"
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:ems="10"
android:hint="Passage 3"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.043"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.194" />
<Button
android:id="#+id/button_1"
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="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_1"
app:layout_constraintTop_toTopOf="#+id/passage_1" />
<Button
android:id="#+id/button_2"
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="Button"
app:layout_constraintBottom_toTopOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_2"
app:layout_constraintTop_toTopOf="#+id/passage_2" />
<Button
android:id="#+id/button_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="#+id/passage_3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/passage_3"
app:layout_constraintTop_toTopOf="#+id/passage_3" />
<TextView
android:id="#+id/textview1"
android:layout_width="319dp"
android:layout_height="74dp"
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:background="#3F51B5"
android:hint="Passage 1"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.446"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview2"
android:layout_width="319dp"
android:layout_height="74dp"
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:background="#3F51B5"
android:hint="Passage 2"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.672"
tools:visibility="invisible" />
<TextView
android:id="#+id/textview3"
android:layout_width="319dp"
android:layout_height="74dp"
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:background="#3F51B5"
android:hint="Passage 3"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.486"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.895"
tools:visibility="invisible" />
<Button
android:id="#+id/btnvisi"
android:layout_width="175dp"
android:layout_height="44dp"
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="Passage Visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.036"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.299" />
<Button
android:id="#+id/btninvisi"
android:layout_width="174dp"
android:layout_height="47dp"
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="passage invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.886"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.3" />
</androidx.constraintlayout.widget.ConstraintLayout>
I hope it makes you understand in a better way,
Thanks

Button not responding when clicked

I am new to Android Programming and I wanted to make a simple app where the user would enter there name and after they click the button, it would say Hello (name of person). Also the button will change to thanks for clicking me after it is clicked. I have looked through the code but I am having a hard time finding the error. I have the code down below. I will also add the xml under the code.
code
public class MainActivity extends AppCompatActivity {
private EditText yourName;
private TextView outputName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourName = (EditText)findViewById(R.id.inputText);
outputName = (TextView)findViewById(R.id.outputText);
}
public void printHello (View view){
Button button =(Button) view;
((Button)view).setText("Thanks for Clicking Me!");
yourName =(EditText)findViewById(R.id.inputText);
outputName =(TextView)findViewById(R.id.outputText);
outputName.setText("Hello, "+ yourName.getText());
outputName.setVisibility(View.VISIBLE);
}
}
Here is the xml also
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="328dp"
android:text="Talk to Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />
<TextView
android:id="#+id/Label1"
android:layout_width="0dp"
android:layout_height="31dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text=" Please Enter Your Name"
android:textSize="18sp"
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.0" />
<TextView
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="88dp"
android:text="TextView"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputText"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="72dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Label1" />
</android.support.constraint.ConstraintLayout>
You need to have an android:onClick="printHello" inside of the button XML to link the button to the function inside of the Java class. Then any time the button is clicked, it calls printHello and does what you need to.

How to change background of a child in view

I'm trying to change the background of a particular constraint layout in a cardview but I can't figure out how to do it.
I have several cards with several layouts (all of them have id like unit1Layout,unit2Layout...)
I want to change the background of the clicked card.
I tried changing the cardview's background but I'm getting as shown in the photo.
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="256"
android:fillViewport="true">
<GridLayout
android:id="#+id/mainGrid"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="10"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="10">
<!-- Row 1 -->
<android.support.v7.widget.CardView
android:id="#+id/unit1Card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_rowWeight="1"
android:background="#drawable/cardbackground"
app:cardCornerRadius="8dp"
app:cardElevation="5dp">
<android.support.constraint.ConstraintLayout
android:id="#+id/unit1Layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#drawable/cardbackground">
<TextView
android:id="#+id/unit1Label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/myfont"
android:text="Unit 1"
android:textColor="#color/fontColor"
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/unit1Words"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:fontFamily="#font/myfont"
android:text="423 Words"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Green"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="358"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/unit1Yellow"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Yellow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="32"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
<TextView
android:id="#+id/unit1Red"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:fontFamily="#font/myfont"
android:text="8"
android:textColor="#color/fontColor"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/unit1Yellow"
app:layout_constraintTop_toBottomOf="#+id/unit1Label" />
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.CardView>
this is how Im changing the cardview's background
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
[![enter image description here][1]][1]
cardView.setBackground(ContextCompat.getDrawable(cardView.getContext(), R.drawable.cardbackground));
}
}
});
}
}
I just found the answer:
private void setSingleEvent(GridLayout mainGrid){
for(int i = 0;i<mainGrid.getChildCount();i++){
final int finalI = i;
final CardView cardView = (CardView)mainGrid.getChildAt(i);
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String layoutID = "unit"+ (finalI + 1) + "Layout";
ConstraintLayout constraintLayout = findViewById(getResources()
.getIdentifier(layoutID,"id",getPackageName()));
Log.d("unit i layout", "unit"+ (finalI + 1) + "Layout");
if(unitNumbers[finalI+1] == 0) {
unitNumbers[finalI+1] = finalI+1;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackgroundselected));
}
else {
unitNumbers[finalI+1] = 0;
constraintLayout.setBackground(ContextCompat
.getDrawable(constraintLayout.getContext(),R.drawable.cardbackground));
}
}
});
}
}

App stopped responding when accessing EditText [duplicate]

This question already has answers here:
What is a NumberFormatException and how can I fix it?
(9 answers)
Closed 5 years ago.
I'm new on Android Studio and Java. I want to get a value from an EditText and convert it to an Integer and set it to itself in onClickListener method, but it keeps stopped responding after I pressed buttons(buttons that are worked for decreasing and/or increasing a value on an EditText, as stated with btnInc and btnDec) when it debugged on my phone. I debugged on an emulator and it didn't worked either. I declared a final EditText and final Button in the protected void onCreate method after everything is created.
Here's my code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button btnCalculate = findViewById(R.id.btn_calc);
final EditText edtPrime = findViewById(R.id.EditText_numMaxPrime);
final Button btnInc = findViewById(R.id.btn_incNum);
final Button btnDec = findViewById(R.id.btn_decNum);
btnDec.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer number = Integer.parseInt(edtPrime.getText().toString());
number--;
edtPrime.setText(number, TextView.BufferType.EDITABLE);
}
});
btnInc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Integer number = Integer.parseInt(edtPrime.getText().toString());
number++;
edtPrime.setText(number, TextView.BufferType.EDITABLE);
}
});
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Sieve(Integer.parseInt(edtPrime.getText().toString()));
}
});
}
I had imported android.widget.Button and android.widget.EditText.
Here's my 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="karuntodroid.sieveoferatosthenes.MainActivity">
<TextView
android:id="#+id/TextView_askPrime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:text="Enter the prime number up to:"
app:layout_constraintBottom_toBottomOf="#+id/EditText_numMaxPrime"
app:layout_constraintHorizontal_bias="0.051"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="#+id/EditText_numMaxPrime"
app:layout_constraintVertical_bias="0.032" />
<EditText
android:id="#+id/EditText_numMaxPrime"
android:layout_width="60dp"
android:layout_height="40dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:ems="10"
android:hint="100"
android:inputType="number"
app:layout_constraintEnd_toStartOf="#+id/btn_incNum"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_incNum"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="+"
app:layout_constraintEnd_toStartOf="#+id/btn_decNum"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_decNum"
android:layout_width="40dp"
android:layout_height="0dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="-"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="CALCULATE"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TextView_askPrime" />
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="407dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent">
<TextView
android:id="#+id/TextView_numbersare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:text="Numbers are:"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/TextView_numbers"
android:layout_width="350dp"
android:layout_height="309dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="numbers"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/TextView_numbersare" />
<Button
android:id="#+id/btn_numbercopy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Copy"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/btn_numsReset"
app:layout_constraintTop_toBottomOf="#+id/TextView_numbers" />
<Button
android:id="#+id/btn_numsReset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginTop="8dp"
android:text="Reset"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent
app:layout_constraintTop_toBottomOf="#+id/TextView_numbers" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
EDIT:
Here I found an error on Logcat:
01-26 18:40:15.710 32310-32310/karuntodroid.sieveoferatosthenes E/AndroidRuntime: FATAL EXCEPTION: main
Process: karuntodroid.sieveoferatosthenes, PID: 32310
java.lang.NumberFormatException: For input string: ""
at java.lang.Integer.parseInt(Integer.java:533)
at java.lang.Integer.parseInt(Integer.java:556)
at karuntodroid.sieveoferatosthenes.MainActivity$2.onClick(MainActivity.java:38)
at android.view.View.performClick(View.java:5638)
at android.view.View$PerformClick.run(View.java:22444)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:159)
at android.app.ActivityThread.main(ActivityThread.java:6139)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
01-26 18:40:15.713 1390-2874/? W/ActivityManager: Force finishing activity karuntodroid.sieveoferatosthenes/.MainActivity
Any help is appreciated. Thank you!
You are setting the text in your EditText via edtPrime.setText(number, TextView.BufferType.EDITABLE); where your number variable is Integer.
If you look at the docs, this will try to fetch a resource with the identifier (id) of that number. (and you dont have that resource with that id...those resources are accessed for ex. via R.string.my_string)
What you need to do is convert the number to string and set it via setText(..)

Error in data Binding in Android

activity_main.xml
<data class="LoginDataBinding">
<variable
name="login"
type="com.example.itstym.logina.LoginViewModel" />
<variable
name="ModelClickListener"
type="com.example.itstym.logina.interface.ClickListener" />
</data>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textStyle="bold"
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome, Back"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
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"
android:layout_marginTop="24dp"/>
<EditText
app:error="#{login.errorEmail}"
android:hint="#{login.emailHint}"
android:text="#{login.userEmailAddress}"
android:id="#+id/user_email_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:ems="10"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginTop="24dp"
app:layout_constraintTop_toBottomOf="#+id/textView"
android:layout_marginStart="8dp"/>
<EditText
app:error="#{login.errorPassword}"
android:text="#{login.userPassword}"
android:hint="#{login.passwordHint}"
android:id="#+id/user_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_email_address"/>
<Button
android:onClick="#{(v)->ModelClickListener.onSubmitButtonClick()}"
android:text="#{login.loginButtonText}"
android:id="#+id/login_submit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:layout_constraintTop_toBottomOf="#+id/user_password"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
MainActivity.java
public class SampleActivity extends AppCompatActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final LoginDataBinding loginDataBinding= DataBindingUtil.setContentView(this,R.layout.activity_main);
LoginViewModel viewModel = new LoginViewModel("Enter Email Address","Enter password","Login");
loginDataBinding.setLogin(viewModel);
loginDataBinding.setModelClickListener(new ClickListener() {
#Override
public void onClick() {
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserEmailAddress(), Toast.LENGTH_SHORT).show();
Toast.makeText(getApplicationContext(), loginDataBinding.getLogin().getUserPassword(), Toast.LENGTH_SHORT).show();
}
});
}
}
But I am getting the error unresolved reference for setModelClickListener() but the setLogin() works perfertly normal.
One more thing why the DataBindingUtil.setContentView(this,R.layout.activity_main); return the LoginDataBinding instead of MainActivityBinding?

Categories

Resources