Taking an edittext field and displaying in a TextView - java

I am relatively new to coding but have some experience in Java and Android. I have a really basic problem that I have done over and over again but for some reason, this occurrence doesn't work!
I have an edittext field which is populated by the user. When an enter button is pressed the app then takes this and saves it as a String. The String is then used to populate the TextView with the user input.
I have created the findviewbyids, I have set the String to equal the edittext input and then set the text in the text view to be the String as per the code below. I have checked previous apps I have made and this has always worked before...
package com.example.android.golfhandicap;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText playerName;
TextView playerOneName;
String name;
int handicap;
EditText playerHandicap;
TextView playerOneHcp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
}
//set view by IDs
public void views() {
playerName = findViewById(R.id.inputPlayerName);
playerOneName = findViewById(R.id.playerOneName);
playerHandicap = findViewById(R.id.inputPlayerHcp);
playerOneHcp = findViewById(R.id.playerOneHcp);
}
/**
* Add the codes for the button to navigate around the app
*/
public void enterScore1(View view) {
setContentView(R.layout.front_page);
}
public void addPlayer(View view) {
setContentView(R.layout.new_player);
}
public void addScore(View view) {
setContentView(R.layout.input_page);
}
public void enterPlayer(View view) {
views();
name = playerName.getText().toString();
//handicap = Integer.parseInt(playerHandicap.getText().toString());
playerOneName.setText(name);
//playerOneHcp.setText(handicap);
Toast.makeText(this, "player name is " + name + " and handicap is
" + handicap , Toast.LENGTH_SHORT).show();
setContentView(R.layout.front_page);
}
}
The XML for the page with the text view I want to display the String in:
<?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:visibility="visible"
tools:context=".MainActivity">
<TextView
android:id="#+id/appName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
android:editable="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/playerOneName"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:editable="false"
android:layout_marginTop="32dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/playerOneName2"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName3" />
<TextView
android:id="#+id/playerOneName3"
android:editable="false"
android:layout_width="136dp"
android:layout_height="30dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:visibility="invisible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/playerOneName" />
<Button
android:id="#+id/addScore"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addScore"
android:text="Add new score"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/addPlayer" />
<Button
android:id="#+id/addPlayer"
android:layout_width="200dp"
android:layout_height="40dp"
android:layout_marginBottom="16dp"
android:background="#android:color/holo_green_light"
android:onClick="addPlayer"
android:text="Add new player"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/addScore"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/playerOneHcp"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="32dp"
android:editable="false"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="visible"
app:layout_constraintStart_toEndOf="#+id/playerOneName"
app:layout_constraintTop_toBottomOf="#+id/appName2" />
<TextView
android:id="#+id/editText2"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName2"
app:layout_constraintTop_toBottomOf="#+id/editText3" />
<TextView
android:id="#+id/editText3"
android:editable="false"
android:layout_width="43dp"
android:layout_height="30dp"
android:layout_marginStart="31dp"
android:layout_marginTop="24dp"
android:ems="10"
android:inputType="number"
android:textAlignment="center"
android:visibility="invisible"
app:layout_constraintStart_toEndOf="#+id/playerOneName3"
app:layout_constraintTop_toBottomOf="#+id/playerOneHcp" />
</android.support.constraint.ConstraintLayout>
The XML for the layout with the edittext which I get the user input from:
<?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">
<TextView
android:id="#+id/enterHcpText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Current handicap :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterAgeText" />
<TextView
android:id="#+id/enterAgeText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:editable="false"
android:text="Player age :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/enterNameText" />
<TextView
android:id="#+id/enterNameText"
android:layout_width="136dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:editable="false"
android:text="Player name :"
android:textAlignment="center"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<TextView
android:id="#+id/appName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="8dp"
android:editable="false"
android:text="Golf handicap"
android:textAlignment="center"
android:textColor="#color/colorPrimaryDark"
android:textSize="24sp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/inputPlayerName"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="33dp"
android:maxLength="25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/appName3" />
<EditText
android:id="#+id/inputPlayerAge"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerName" />
<EditText
android:id="#+id/inputPlayerHcp"
android:layout_width="130dp"
android:layout_height="50dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="33dp"
android:inputType="number"
android:maxLength="2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerAge" />
<Button
android:id="#+id/enterPlayer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="148dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="148dp"
android:onClick="enterPlayer"
android:text="Enter"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputPlayerHcp" />
</android.support.constraint.ConstraintLayout>
If I comment out the line p1Name.setText(name); then it works fine, I have a toast to check the name is taken and name is set as the input. So there seems to be something wrong with the p1Name. I have checked and triple checked all the IDs are correct.
I have spent over an hour now trying to figure it out and can't see what I am doing wrong!
This is the lines I get on logcat (These are all the lines shown in red)
2019-01-24 23:29:39.376 9545-9545/com.example.android.golfhandicap E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.golfhandicap, PID: 9545
java.lang.IllegalStateException: Could not execute method for android:onClick
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText(java.lang.CharSequence)' on a null object reference
at com.example.android.golfhandicap.MainActivity.enterPlayer(MainActivity.java:65)
at java.lang.reflect.Method.invoke(Native Method) 
at android.support.v7.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:6294) 
at android.view.View$PerformClick.run(View.java:24770) 
at android.os.Handler.handleCallback(Handler.java:790) 
at android.os.Handler.dispatchMessage(Handler.java:99) 
at android.os.Looper.loop(Looper.java:164) 
at android.app.ActivityThread.main(ActivityThread.java:6494) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
Any help would be gratefully received.

The problem is simply because of the following:
you haven't bind the view with the findViewById. This is because of the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// Here you haven't bind the view.
}
you should directly bind the view with your views() method. So, change the code to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.front_page);
// bind the view.
views();
}
You're changing the layout view each time a button is clicked with this method:
public void addScore(View view) {
setContentView(R.layout.input_page);
}
which is become one of the source for your problem. First, it's not recommended because you're redrawing entire layout when calling setContentView. Second, you need to make sure all the layout onClick is implemented in your code.
Last but not the least, you better using setOnClickListener in code instead onClick attribute in layout. It is because you should separate between logic and UI.

Related

getting a java.lang.NullPointerException: in a simple app [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 2 years ago.
i'm getting java.lang.NullPointerException. And i don't know how to solve it. looking for some help here
What i'v tried:
to look here java.lang.RuntimeException: Unable to start activity ComponentInfo But, it didn't help me
to clean and Rebuild project.
to Invalidate Cache and Restart in Android studio
and i'v discovered some problems in LayoutInflater.java
It cant resolve some imports"
sorry cant import images yet this is and image of errors in LayoutInflater.java
My XML file activity_main.XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="100">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team A" />
<TextView
android:id="#+id/team_a_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsA"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsA"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointA"
android:text="Free Throw" />
</LinearLayout>
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:gravity="center"
android:text="Team B" />
<TextView
android:id="#+id/team_b_score"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="24dp"
android:gravity="center"
android:textSize="50sp"
android:text="0" />
<Button
android:id="#+id/three_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusThreePointsB"
android:text="+3 Points" />
<Button
android:id="#+id/two_points_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusTwoPointsB"
android:text="+2 Points" />
<Button
android:id="#+id/free_throw_button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:onClick="plusOnePointB"
android:text="Free Throw" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center"
android:orientation="vertical">
<Button
android:id="#+id/reset_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="24dp"
android:onClick="reset"
android:text="Reset" />
</LinearLayout>
</LinearLayout>
My MainActivity.java
package com.example.problems;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
int scoreA = 0;
int scoreB = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void plusThreePointsA(View view){
scoreA = scoreA+3;
displayScoreA(scoreA);
}
public void plusTwoPointsA(View view){
scoreA = scoreA+2;
displayScoreA(scoreA);
}
public void plusOnePointA(View view){
scoreA = scoreA+1;
displayScoreA(scoreA);
}
public void plusThreePointsB(View view){
scoreB = scoreB+3;
displayScoreB(scoreB);
}
public void plusTwoPointsB(View view){
scoreB = scoreB+2;
displayScoreB(scoreB);
}
public void plusOnePointB(View view){
scoreB = scoreB+1;
displayScoreB(scoreB);
}
public void displayScoreA (int score){
TextView viewScore = findViewById(R.id.team_a_score);
viewScore.setText(String.valueOf(score));
}
public void displayScoreB (int score){
TextView viewScore = findViewById(R.id.team_b_score);
viewScore.setText(String.valueOf(score));
}
public void reset (View view){
scoreA = 0;
scoreB = 0;
displayScoreA(scoreA);
displayScoreB(scoreB);
}
}
Error that i get
--------- beginning of crash
05-11 08:14:25.720 4084-4084/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.problems, PID: 4084
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.problems/com.example.problems.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.lang.String.equals(java.lang.Object)' on a null object reference
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:715)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:809)
at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:555)
at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:161)
at com.example.problems.MainActivity.onCreate(MainActivity.java:17)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
at android.app.ActivityThread.access$800(ActivityThread.java:144) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:135) 
at android.app.ActivityThread.main(ActivityThread.java:5221) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
05-11 08:19:26.000 4084-4084/com.example.problems I/Process: Sending signal. PID: 4084 SIG: 9
Use :
<View android:layout_width="1dp" android:layout_height="match_parent" android:background="#android:color/darker_gray" />
instead of :
<view
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#android:color/darker_gray"
/>

How to fix crash in Android Studio [duplicate]

This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 3 years ago.
I'm creating an app for a sensor and when I start it the app crashes.
I tried the app on 2 different phones and 2 emulators but crashes un every device.
Here is the MainActivity.java file:::
package com.example.ir_sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openMenu = findViewById(R.id.Open_menu);
closeMenu = findViewById(R.id.Close_menu);
}
View openMenu = findViewById(R.id.Open_menu);
View closeMenu = findViewById(R.id.Close_menu);
public void menu() {
if (openMenu.getVisibility() == View.VISIBLE) {
closeMenu.setVisibility(View.GONE);
} else if (closeMenu.getVisibility() == View.GONE){
openMenu.setVisibility(View.VISIBLE);
}
}
}
Here is the activity_main.xml file:::
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="#+id/LOGO"
android:layout_width="0dp"
android:layout_height="248dp"
android:layout_marginStart="8dp"
android:layout_marginTop="52dp"
android:layout_marginEnd="8dp"
android:layout_weight="1"
android:contentDescription="#string/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo_int_lukka" />
<ImageView
android:id="#+id/Close_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="49dp"
android:visibility="gone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LOGO"
app:srcCompat="#drawable/ic_menu_less" />
<ImageView
android:id="#+id/Open_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="49dp"
android:contentDescription="#string/openmenu"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/LOGO"
app:srcCompat="#android:drawable/ic_menu_more" />
<LinearLayout
android:id="#+id/menuLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:layout_marginTop="380dp"
android:baselineAligned="false"
android:orientation="horizontal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/houseMenu"
android:layout_width="4dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/housemenu"
app:srcCompat="#drawable/house_menu" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/IR_LED"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/ir_led"
app:srcCompat="#drawable/ir_led" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<ImageView
android:id="#+id/alarmSign"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:contentDescription="#string/alarmsign"
app:srcCompat="#drawable/on_off__alert" />
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
</FrameLayout>
</LinearLayout>
</LinearLayout>
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gridline380FromTop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="380dp"
android:orientation="horizontal"
app:layout_constraintGuide_begin="380dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/gridline48FromLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="48dp"
android:orientation="vertical"
app:layout_constraintGuide_begin="48dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the Logcat:::
2019-08-12 17:14:39.966 9648-9648/com.example.ir_sensor E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ir_sensor, PID: 9648
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ir_sensor/com.example.ir_sensor.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3194)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at androidx.appcompat.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249)
at androidx.appcompat.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at androidx.appcompat.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at androidx.appcompat.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.ir_sensor.MainActivity.(MainActivity.java:18)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:95)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1243)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3182)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016) 
at android.os.Handler.dispatchMessage(Handler.java:107) 
at android.os.Looper.loop(Looper.java:214) 
at android.app.ActivityThread.main(ActivityThread.java:7356) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930) 
What can I do?
The error is that you are defining the openMenu and closeMenu variables, outside the onCreate() method. The thing is however that before onCreate you haven't defined your view, so getting the view by id before the view has been defined results in a NullPointerException (the program can't find the variable you were asking for and crashes). So do this:
package com.example.ir_sensor;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
openMenu = findViewById(R.id.Open_menu);
closeMenu = findViewById(R.id.Close_menu);
}
View openMenu;
View closeMenu;
public void menu() {
if (openMenu.getVisibility() == View.VISIBLE) {
closeMenu.setVisibility(View.GONE);
} else if (closeMenu.getVisibility() == View.GONE){
openMenu.setVisibility(View.VISIBLE);
}
}
}

how the second activity crash?

i just want to fill some infos in the activitymain and then pass an intent to a second activity, at the first it works perfect but when i add some xml layout, when running the app on device and pass the intent it crashes
the main activity
public void move(View view) {
Intent i = new Intent(MainActivity.this, FormActivity.class);
EditText name = findViewById(R.id.name);
EditText animal = findViewById(R.id.animal);
String n = name.getText().toString();
String a = animal.getText().toString();
if (n.isEmpty() || a.isEmpty()) {
Toast.makeText(this, "nom ou animal est vide !", Toast.LENGTH_SHORT).show();
} else {
i.putExtra("KEY_Name", name.getText().toString());
i.putExtra("KEY_Animal", animal.getText().toString());
startActivity(i);
}
}
// and the formactivity or the second
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_form);
Intent i = getIntent();
TextView Text1 = findViewById(R.id.t1);
TextView Text2 = findViewById(R.id.t2);
String name = i.getStringExtra("KEY_Name");
String animal = i.getStringExtra("KEY_Animal");
Text1.setText(name);
Text2.setText(animal);
}
// XML file for activity_main it works perfect
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/dog" />
<ImageView
android:id="#+id/imageView"
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="40dp"
android:src="#drawable/logo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginTop="8dp"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/hello"
android:textColor="#ffffff"
android:textSize="26sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:layout_constraintVertical_bias="0.075" />
<LinearLayout
android:id="#+id/linear1"
style="#style/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/name"
android:textColor="#ffffff" />
<View
android:layout_width="3dip"
android:layout_height="match_parent"
android:background="#ffffff" />
<EditText
android:id="#+id/name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/ex_name"
android:inputType="textCapWords"
android:maxLines="1" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#ffffff"
app:layout_constraintBottom_toTopOf="#+id/linear2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/linear2" />
<LinearLayout
android:id="#+id/linear2"
style="#style/linear"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear1">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
android:layout_weight="1"
android:shadowColor="#000000"
android:shadowDx="-2"
android:shadowDy="-2"
android:shadowRadius="1"
android:text="#string/pet"
android:textColor="#ffffff" />
<View
android:layout_width="3dip"
android:layout_height="match_parent"
android:background="#ffffff" />
<EditText
android:id="#+id/animal"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/ex_animal"
android:inputType="textCapWords"
android:maxLines="1" />
</LinearLayout>
<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="4dp"
android:onClick="move"
android:text="Suivant"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.096"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear2"
app:layout_constraintVertical_bias="0.344" />
// the activity_form or second that crashes
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/sheep" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:foregroundGravity="center_horizontal"
android:src="#drawable/title" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="16dp"
android:background="#drawable/list_fluid"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:id="#+id/t1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:gravity="center_horizontal"
android:text="Hello freind!"
android:textColor="#color/green"
android:textSize="24sp" />
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:layout_marginBottom="12dp"
android:layout_marginTop="12dp"
android:background="#ffffff" />
<TextView
android:id="#+id/t2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="oh, puppy!"
android:textColor="#color/green"
android:textSize="18sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginTop="12dp"
android:text="comment Aidez-Vous ?" />
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="mon Chat souffre de fièvre"
android:inputType="text"
android:lines="6"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:textColor="#FFFFFF"
android:textColorHint="#cccccc"
android:textSize="12sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="12dp"
android:background="#color/logo"
android:text="envoyer"
android:textColor="#ffffff"
android:textSize="16sp" />
</LinearLayout>
the logcat
10-09 18:03:08.327 29724-29724/com.example.android.workingproject W/ResourceType: Failure getting entry for 0x01080946 (t=7 e=2374) (error -75)
10-09 18:04:20.737 29724-29724/com.example.android.workingproject W/ResourceType: Failure getting entry for 0x01080946 (t=7 e=2374) (error -75)
10-09 18:04:21.538 29724-29724/com.example.android.workingproject E/art: Throwing OutOfMemoryError "Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM"
10-09 18:04:21.608 29724-29724/com.example.android.workingproject E/art: Throwing OutOfMemoryError "Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM"
10-09 18:04:21.628 29724-29724/com.example.android.workingproject E/art: li.han, Prepare to dump hprof for OOM Error !
li.han, Directory [/data/log] could not be write !
10-09 18:04:21.638 29724-29724/com.example.android.workingproject E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.workingproject, PID: 29724
java.lang.OutOfMemoryError: Failed to allocate a 343944636 byte allocation with 16777120 free bytes and 52MB until OOM
at dalvik.system.VMRuntime.newNonMovableArray(Native Method)
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:837)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:656)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:1037)
at android.content.res.Resources.loadDrawableForCookie(Resources.java:4056)
at android.content.res.Resources.loadDrawable(Resources.java:3929)
at android.content.res.Resources.loadDrawable(Resources.java:3779)
at android.content.res.TypedArray.getDrawable(TypedArray.java:776)
at android.widget.ImageView.<init>(ImageView.java:151)
at android.widget.ImageView.<init>(ImageView.java:140)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:72)
at android.support.v7.widget.AppCompatImageView.<init>(AppCompatImageView.java:68)
at android.support.v7.app.AppCompatViewInflater.createImageView(AppCompatViewInflater.java:182)
at android.support.v7.app.AppCompatViewInflater.createView(AppCompatViewInflater.java:106)
at android.support.v7.app.AppCompatDelegateImpl.createView(AppCompatDelegateImpl.java:1266)
at android.support.v7.app.AppCompatDelegateImpl.onCreateView(AppCompatDelegateImpl.java:1316)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:732)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:813)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.android.workingproject.FormActivity.onCreate(FormActivity.java:13)
at android.app.Activity.performCreate(Activity.java:6609)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3103)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3260)
at android.app.ActivityThread.access$1000(ActivityThread.java:218)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1734)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
At least one of the drawables referenced in your second XML file is too big.
Check the following drawables and resize them:
sheep
title
list_fluid
PNGs generally don't need to be very large. Keep them below 1600x1600 if you can, preferably 800x800.

My first app isn't working and it crashes: UnsupportedOperationException

I am new to Android and I started making my first application following tutorials and such. However, when I click the run button it gives me following error on the logcat from which I count not identify where the error is. Hence, here is my code in hope of some advice. Thanks in advance.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
});
}
}
LogCat after fixing the render problem :
7-16 01:27:14.359 10870-10870/com.example.abarimess.myfirstapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.abarimess.myfirstapp, PID: 10870
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.abarimess.myfirstapp/com.example.abarimess.myfirstapp.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3184)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294)
at android.app.ActivityThread.access$1000(ActivityThread.java:210)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6938)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x4
at android.content.res.TypedArray.getDimensionPixelOffset(TypedArray.java:546)
at android.support.constraint.ConstraintLayout$LayoutParams.<init>(ConstraintLayout.java:2685)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:1915)
at android.support.constraint.ConstraintLayout.generateLayoutParams(ConstraintLayout.java:476)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:820)
at android.view.LayoutInflater.inflate(LayoutInflater.java:511)
at android.view.LayoutInflater.inflate(LayoutInflater.java:415)
at android.view.LayoutInflater.inflate(LayoutInflater.java:366)
at android.support.v7.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:467)
at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
at com.example.abarimess.myfirstapp.MainActivity.onCreate(MainActivity.java:11)
at android.app.Activity.performCreate(Activity.java:6575)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1134)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3137)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3294) 
at android.app.ActivityThread.access$1000(ActivityThread.java:210) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1704) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:145) 
at android.app.ActivityThread.main(ActivityThread.java:6938) 
at java.lang.reflect.Method.invoke(Native Method) 
at java.lang.reflect.Method.invoke(Method.java:372) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
And the layout 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=".MainActivity"
tools:layout_editor_absoluteY="73dp">
<TextView
android:id="#+id/textView3"
android:layout_width="360dp"
android:layout_height="119dp"
android:layout_marginTop="50dp"
android:text="#string/square_of_the_number_is"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#android:color/holo_purple"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.49"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/numberinput" />
<TextView
android:id="#+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Resulttextview" />
<Button
android:id="#+id/btn"
android:layout_width="370dp"
android:layout_height="80dp"
android:layout_marginTop="50dp"
android:background="?android:attr/colorActivatedHighlight"
android:text="#string/butan"
android:textColor="#android:color/holo_blue_bright"
android:textSize="20sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView3" />
<EditText
android:id="#+id/numberinput"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="#string/number_in_here"
android:inputType="numberDecimal"
android:singleLine="false"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resulttextview" />
</android.support.constraint.ConstraintLayout>
the manifest.Xml as requested in the comments :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.abarimess.myfirstapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
In your layout xml file, you have two separate TextView which are the following.
<TextView
android:id="#+id/Resulttextview"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Resulttextview" />
Name of both text views are identical except the first one has started with a capital R. You might consider the name of the first TextView to something else like resulttextview1 like the following.
<TextView
android:id="#+id/resulttextview1"
android:layout_width="390dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#android:color/holo_red_dark"
android:text="#string/the_result_is"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff"
android:textSize="50sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/resulttextview"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginTop="30dp"
android:background="#android:color/darker_gray"
android:text="#string/_0"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/Optional"
android:textSize="40sp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.51"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/resulttextview1" />
Please avoid naming layout identifiers with capital letters. Use underscores instead (e.g. result_text_view).
Another potential problem in your code is, if there is nothing entered in your EditText the app will crash again with a NullPointerException as the value which will be returned from firstnum.getText().toString() will have the null value.
In this case, you might consider adding a null checking here.
#Override
public void onClick(View v) {
EditText firstnum = (EditText) findViewById(R.id.numberinput);
TextView resultTextView = (TextView) findViewById(R.id.resulttextview);
// Add a null check here for safety
if(firstnum.getText().toString() == null) return;
int num1 = Integer.parseInt(firstnum.getText().toString()) ;
int result = num1 * num1;
resultTextView.setText(result + "");
}
You will get NumberFormatException in case of entering a string in your EditText other than a number. So you might need to handle that case as well. However, this can be ignored in case of handling only numbers in your EditText.
this Is because you should to instance
EditText firstnum = (EditText) findViewById(R.id.numberinput);
Out of method on click listener.
The layout_editor_absoluteX attribute for the resulttextview in your layout XML is referencing spotShadowAlpha, which is an integer which can't be explicitly converted to a dimension, as it doesn't contain any units.
You should update this value to a dimension. For example:
app:layout_editor_absoluteX="30dp"
Obviously, you'll need to change this example up to fit the rest of your layout.

I'm not getting EditText Value in another class

MainActivity.java (First Class)
package com.example.we.reportcardjavaclass;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public EditText one, two, three, four, five, six;
public String bang1, bang2, bang3, bang4, bang5, bang6;
public Button results;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
}
public void getResult(View view){
results = (Button) findViewById(R.id.result);
Intent numbersIntent = new Intent(MainActivity.this, result.class);
startActivity(numbersIntent);
one = (EditText) findViewById(R.id.name);
bang1 = one.getText().toString();
two = (EditText) findViewById(R.id.Class);
bang2 = two.getText().toString();
three = (EditText) findViewById(R.id.English);
bang3 = three.getText().toString();
four = (EditText) findViewById(R.id.Hindi);
bang4 = four.getText().toString();
five = (EditText) findViewById(R.id.Maths);
bang5 = five.getText().toString();
six = (EditText) findViewById(R.id.Science);
bang6 = six.getText().toString();
}
}
activity_main.xml (First Layout File)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.we.reportcardjavaclass.MainActivity"
android:padding="16dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Student's Name"
android:id="#+id/name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Class"
android:id="#+id/Class"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="64dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in English"
android:id="#+id/English"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="128dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Hindi"
android:id="#+id/Hindi"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="192dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Maths"
android:id="#+id/Maths"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Science"
android:id="#+id/Science"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="320dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:text="Get result"
android:id="#+id/result"
android:onClick="getResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="384dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
## result.xml (Second Layout File) ##
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="32dp">
<android.support.v7.widget.CardView
android:id="#+id/resultCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
android:layout_gravity="center_vertical"
card_view:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Student's Name"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Class"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:background="#color/cardview_dark_background"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="English"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="40dp"
android:text="Hindi"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:text="Maths"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="120dp"
android:text="Science"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="120dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="BACK TO HOME"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout></ScrollView>
Second Class
package com.example.we.reportcardjavaclass;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by we on 08-07-2017.
*/
public class result extends MainActivity{
public TextView one, two, three, four, five, six;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle(bang1+"s Result");
one = (TextView) findViewById(R.id.one);
one.setText(bang1);
two = (TextView) findViewById(R.id.two);
two.setText(bang2);
three = (TextView) findViewById(R.id.three);
three.setText(bang3);
four = (TextView) findViewById(R.id.four);
four.setText(bang4);
five = (TextView) findViewById(R.id.five);
five.setText(bang5);
six = (TextView) findViewById(R.id.six);
six.setText(bang6);
}
}
EditText values are getting stored MainActivity.java in variables bang - 123456. I added public modifier while declaring them so I can access these in second class too. In second class I want to use these variable values to set in into Textviews but while running it is not showing the value.
I have also attached the screenshots of both layouts after running in my emulator.
Screenshot 1
Screenshot 2
In second layout it is supposed to show name and other values.
I would suggest that you take a few steps back and get a better understanding of Android's fundamentals, including Activities in general. Perhaps a good place to start, would be understanding the Activity Lifecycle.
An answer to your question isn't very simple, because the way in which Android works is likely very different from your understanding.
In short, your second activity is a completely different instance of a class than the first. Extending the second from the first does not mean that the assigned variables are available to the second, it merely means that the class inherits the structure of its parent in the form of methods and fields.
You should instead be designing your activities in such a way that they relay those strings. When launching your second activity, include those strings in the intent when starting it.
For instance:
public class MainActivity extends AppCompatActivity {
// ...
/** Your method where the second activity is launched */
public void onButtonClicked() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putString(SecondActivity.EXTRA_BANG1, bang1);
// ...
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_BANG1 = "bang1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// ...
TextView tvOne = (TextView) findViewById(R.id.one);
// ...
String bang1 = getIntent().getString(EXTRA_BANG1);
tvOne.setText(bang1);
}
}
I thoroughly recommend reading up in the Android documentation (it's excellently written!) to better understand how (and why) it works through this mechanism. In particular, this article on Sending data between activities is very relevant to you.
If you want to use these values in another activity then simply pass these values along with the intent that is used to start a new activity.
MainActivity.java
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("Bang1",bang1);
i.putExtra("Bang2",bang2);
i.putExtra("Bang3",bang3);
...
And you can get them in the SecondActivity.java file as:
Intent i =getIntent();
int bang1 = i.getIntExtra("Bang1");
...
But instead so choosing the string names as shown i would suggest to create a new class to store the the string names as final string constants in order to avoid any kind of mistake.
Or you can directly declare these variables as static and access them in the SecondActivity by referring to their names as :
int bang1 = MainActivity.bang1;

Categories