This question already has answers here:
Unfortunately MyApp has stopped. How can I solve this?
(23 answers)
Closed 5 years ago.
I'm new to android development and im making a simple calculator app... Whenever i'm running the code in the android emulator it's showing me Unfortunately, SimpleApp has Stopped.... I'm Using Android Studio to write the code Please help me...
Following is my code....
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.simpleapp.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="337dp"
android:layout_height="58dp"
android:contextClickable="false"
android:cursorVisible="false"
android:fontFamily="serif"
android:longClickable="true"
android:text="Calculator"
android:textAlignment="center"
android:textAllCaps="true"
android:textColor="#android:color/holo_red_dark"
android:textSize="36sp"
android:visibility="visible"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="31dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="194dp"
android:layout_height="43dp"
android:text="Number 1: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="140dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="194dp"
android:layout_height="44dp"
android:text="Number 2: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="217dp" />
<EditText
android:id="#+id/number2"
android:layout_width="99dp"
android:layout_height="43dp"
android:ems="10"
android:hint="Number 2"
android:inputType="number|numberSigned|numberDecimal"
android:singleLine="true"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textIsSelectable="true"
android:textSize="20sp"
tools:layout_editor_absoluteX="262dp"
tools:layout_editor_absoluteY="217dp" />
<EditText
android:id="#+id/number1"
android:layout_width="99dp"
android:layout_height="43dp"
android:ems="10"
android:hint="Number 1"
android:inputType="number|numberSigned|numberDecimal"
android:linksClickable="false"
android:longClickable="true"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="20sp"
tools:layout_editor_absoluteX="262dp"
tools:layout_editor_absoluteY="140dp" />
<TextView
android:id="#+id/result"
android:layout_width="175dp"
android:layout_height="40dp"
tools:layout_editor_absoluteX="176dp"
tools:layout_editor_absoluteY="430dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="103dp"
android:layout_height="40dp"
android:text="Result: "
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="59dp"
tools:layout_editor_absoluteY="433dp" />
<Button
android:id="#+id/btnAdd"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="+"
android:textColor="#android:color/black"
android:textSize="30sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="292dp" />
<Button
android:id="#+id/btnSub"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="-"
android:textColor="#android:color/black"
android:textIsSelectable="false"
android:textSize="30sp"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="292dp" />
<Button
android:id="#+id/btnDiv"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="/"
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="24dp"
tools:layout_editor_absoluteY="362dp" />
<Button
android:id="#+id/btnMul"
android:layout_width="152dp"
android:layout_height="49dp"
android:text="*"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="25sp"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="362dp" />
</android.support.constraint.ConstraintLayout>
MainActivity.java
package com.myapp.simpleapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
EditText number1= (EditText) findViewById(R.id.number1);
EditText number2= (EditText) findViewById(R.id.number2);
Button add= (Button) findViewById(R.id.btnAdd);
Button sub= (Button) findViewById(R.id.btnSub);
Button div= (Button) findViewById(R.id.btnDiv);
Button mul= (Button) findViewById(R.id.btnMul);
TextView txtresult= (TextView) findViewById(R.id.result);
double num1,num2,result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
num1=Double.parseDouble(number1.getText().toString());
num2=Double.parseDouble(number2.getText().toString());
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1+num2;
txtresult.setText(Double.toString(result));
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1-num2;
txtresult.setText(Double.toString(result));
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1/num2;
txtresult.setText(Double.toString(result));
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
result=num1*num2;
txtresult.setText(Double.toString(result));
}
});
}
}
LogCat
04-03 12:25:38.913 3927-3927/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:38.913 3927-3927/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:39.372 3952-3952/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:39.373 3952-3952/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.078 4097-4097/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.078 4097-4097/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:49.647 4122-4122/? E/memtrack: Couldn't load memtrack module (No such file or directory)
04-03 12:25:49.648 4122-4122/? E/android.os.Debug: failed to load memtrack module: -2
04-03 12:25:58.059 3666-3666/com.myapp.simpleapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.simpleapp, PID: 3666
java.lang.NumberFormatException: Invalid double: ""
at java.lang.StringToReal.invalidReal(StringToReal.java:63)
at java.lang.StringToReal.parseDouble(StringToReal.java:267)
at java.lang.Double.parseDouble(Double.java:301)
at com.myapp.simpleapp.MainActivity$4.onClick(MainActivity.java:70)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
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:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
04-03 12:25:58.079 1163-1163/? E/EGL_emulation: tid 1163: eglCreateSyncKHR(1299): error 0x3004 (EGL_BAD_ATTRIBUTE)
04-03 12:26:02.452 1810-2202/system_process E/InputDispatcher: channel '21dd8534 com.myapp.simpleapp/com.myapp.simpleapp.MainActivity (server)' ~ Channel is unrecoverably broken and will be disposed!
04-03 12:26:02.581 1175-1175/? E/audio_hw_generic: Error opening input stream format 1, channel_mask 0010, sample_rate 16000
The most common cause of this problem is a NullPointerException at runtime.
Steps to follow:
Observe the Stack trace on Android Studio as shown below:
You can then analyze the Stack trace, and zero in on what exactly caused the exception, and the line that caused it. Alternatively, you can post the screenshot of your stack trace so it can be easier to answer the question.
Use findViewById inside onCreate and use Double.parse only after values have been entered in the edittexts.
Try this code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText number1 = (EditText) findViewById(R.id.number1);
final EditText number2 = (EditText) findViewById(R.id.number2);
Button add = (Button) findViewById(R.id.btnAdd);
Button sub = (Button) findViewById(R.id.btnSub);
Button div = (Button) findViewById(R.id.btnDiv);
Button mul = (Button) findViewById(R.id.btnMul);
final TextView txtresult = (TextView) findViewById(R.id.result);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 + num2;
txtresult.setText(Double.toString(result));
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 - num2;
txtresult.setText(Double.toString(result));
}
});
div.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 / num2;
txtresult.setText(Double.toString(result));
}
});
mul.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
double num1, num2, result;
num1 = Double.parseDouble(number1.getText().toString());
num2 = Double.parseDouble(number2.getText().toString());
result = num1 * num2;
txtresult.setText(Double.toString(result));
}
});
}
}
Related
I'm trying to design a times table testing app, the theory is that the user is presented with a screen with 12 buttons to select from.
When he presses a button it opens a new class testing him with the number he chose. There are 4 main files:
MainActivity.java
activity_main.xml
Times.java
activity_times.java
Currently the class view that comes up to test the user (activity_times.xml), just contains three text views:
"HI" id=textView
"" (empty to be edited in the java class) id=TextOut
"Under development...." id=textView2
The first and last text views are not important, they'll be removed later on.
In the MainActivity.java class I declared a shared variable
public int numberClicked;
and then told the buttons to set the variable to a certain number:
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 2;
openTestPage();
}
});
and then on the Times.java class I told it to import it and the empty output text view:
private MainActivity MA;
private int SubjectNumber = MA.numberClicked;
private final TextView OutputText = findViewById(R.id.TextOut);
and then to set the text view to the imported variable:
OutputText.setText("You chose the number " + SubjectNumber);
The output I want is that on the screen is that the TextOut text view shows, for example if the user presses the button with the number "2" on it: "You chose the number 2".
But instead of that, the app crashes when the second class view launches :(
To stop it from crashing I have to comment out the following code:
private MainActivity MA;
private int SubjectNumber = MA.numberClicked;
private final TextView OutputText = findViewById(R.id.TextOut);
If I uncomment only one of the three lines it will still crash.
I looked in the logcat and saw something interesting:
2020-08-23 15:20:18.553 6537-6537/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.ma, PID: 6537
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ma/com.example.ma.Times}: java.lang.NullPointerException: Attempt to read from field 'int com.example.ma.MainActivity.numberClicked' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2844)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to read from field 'int com.example.ma.MainActivity.numberClicked' on a null object reference
at com.example.ma.Times.<init>**(Times.java:13)**
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at androidx.core.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:41)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2832)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3049)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1809)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6680)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Every thing that came out of the logcat you will find here.
My AndroidManefest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.ma">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Times Practice"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".Times">
</activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="preloaded_fonts"
android:resource="#array/preloaded_fonts" />
</application>
</manifest>
My MainActivity.java
package com.example.ma;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
public int numberClicked;
private Button button2;
private Button button3;
private Button button4;
private Button button5;
private Button button6;
private Button button7;
private Button button8;
private Button button9;
private Button button10;
private Button button11;
private Button button12;
private Button button13;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button2 = findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 2;
openTestPage();
}
});
button3 = findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 3;
openTestPage();
}
});
button4 = findViewById(R.id.button4);
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 4;
openTestPage();
}
});
button5 = findViewById(R.id.button5);
button5.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 5;
openTestPage();
}
});
button6 = findViewById(R.id.button6);
button6.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 6;
openTestPage();
}
});
button7 = findViewById(R.id.button7);
button7.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 7;
openTestPage();
}
});
button8 = findViewById(R.id.button8);
button8.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 8;
openTestPage();
}
});
button9 = findViewById(R.id.button9);
button9.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 9;
openTestPage();
}
});
button10 = findViewById(R.id.button10);
button10.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 10;
openTestPage();
}
});
button11 = findViewById(R.id.button11);
button11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 11;
openTestPage();
}
});
button12 = findViewById(R.id.button12);
button12.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 12;
openTestPage();
}
});
button13 = findViewById(R.id.button13);
button13.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
numberClicked = 13;
openTestPage();
}
});
}
public void openTestPage() {
Intent intent = new Intent(this, Times.class);
startActivity(intent);
}
}
My activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/Instructions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/acme"
android:gravity="center"
android:text="Choose the number you want to practice your times tables with....."
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textSize="36sp"
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.051" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="2"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button5"
app:layout_constraintEnd_toStartOf="#+id/button3"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Instructions" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="3"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Instructions" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="4"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toEndOf="#+id/button3"
app:layout_constraintTop_toBottomOf="#+id/Instructions" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="5"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button6"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="6"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="7"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.493"
app:layout_constraintStart_toEndOf="#+id/button6"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="8"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button11"
app:layout_constraintEnd_toStartOf="#+id/button9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button5" />
<Button
android:id="#+id/button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="9"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button12"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button6" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/architects_daughter"
android:text="10"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button13"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button9"
app:layout_constraintTop_toBottomOf="#+id/button7" />
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fontFamily="#font/architects_daughter"
android:text="11"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/button12"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fontFamily="#font/architects_daughter"
android:text="12"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<Button
android:id="#+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp"
android:fontFamily="#font/architects_daughter"
android:text="13"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/button12" />
</androidx.constraintlayout.widget.ConstraintLayout>
My Times.java:
package com.example.ma;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Times extends AppCompatActivity {
private MainActivity MA;
private int SubjectNumber = MA.numberClicked;
private final TextView OutputText = findViewById(R.id.TextOut);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_times);
OutputText.setText("You chose the number " + SubjectNumber);
}
}
My activity_times.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Times">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Under development...."
android:textSize="28sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.696" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HI"
android:textSize="100sp"
app:layout_constraintBottom_toTopOf="#+id/TextOut"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/TextOut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="316dp"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
You shouldn't try to directly access an Activity from another Activity. In Android, you add the data you want to share with another Activity onto an Intent. You also shouldn't try to obtain a view outside of the Activity lifecycle. Your findViewById() should be moved to the Activity's onCreate().
In your first Activity's openTestPage():
Intent intent = new Intent(this, Times.class);
// Add the value you want to pass to the other Activity
// to the Intent
intent.putExtra("NUMBER_CLICKED", numberClicked);
startActivity(intent);
Your second Activity should look something like this:
public class Times extends AppCompatActivity {
private int SubjectNumber;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_times);
// Access your views here
TextView OutputText = findViewById(R.id.TextOut);
// Retrieve the value passed into the Intent
// (The 0 is a default value to fallback on. You can use another value.)
SubjectNumber = getIntent().getIntExtra("NUMBER_CLICKED", 0);
OutputText.setText("You chose the number " + SubjectNumber);
}
}
You can't directly pass the data from one activity to another you just need to pass through intent or bundle. Here I am updating your code.
MainActivity.java
public void openTestPage(int numberClicked) {
Intent intent = new Intent(this, Times.class);
intent.putExtra("intVariableName", numberClicked);
startActivity(intent);
}
Times.java
Intent intent = getIntent();
int intValue = intent.getIntExtra("intVariableName", 0);
OutputText.setText("You chose the number " + intValue);
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
I'm very new to Android Studio and I know a little java.So I'm trying a simple calculator app with simple layout.I hope all my code is correct but I don't know what's wrong in it.When I run the app it is not at all opening and showing a FATAL exception main.Here is the xml code,java code and logcat of my app.So please help me to make corrections.Thanks!
XML Code ::
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary"
tools:context="com.example.narendra.calculator.MainActivity"
tools:layout_editor_absoluteY="81dp"
tools:layout_editor_absoluteX="0dp">
<EditText
android:id="#+id/editText"
android:layout_width="360dp"
android:layout_height="159dp"
android:background="#android:color/holo_blue_light"
android:ems="10"
android:hint="#string/num"
android:inputType="number"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
tools:ignore="MissingConstraints,RtlHardcoded"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="16dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="360dp"
android:layout_height="159dp"
android:background="#android:color/holo_green_light"
android:ems="10"
android:hint="#string/Num2"
android:inputType="number"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
tools:ignore="MissingConstraints,RtlHardcoded"
tools:layout_editor_absoluteY="180dp"
tools:layout_editor_absoluteX="26dp" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onAddButtonClick"
android:text="#string/a"
android:textSize="50sp"
app:layout_constraintLeft_toLeftOf="#+id/editText2"
app:layout_constraintTop_toTopOf="#+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintRight_toLeftOf="#+id/button3"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onSubtractionButtonClick"
android:text="#string/m"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintTop_toTopOf="#+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="#+id/button"
app:layout_constraintHorizontal_bias="0.03"
android:layout_marginRight="5dp"
app:layout_constraintRight_toRightOf="#+id/button3"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onMultiplicationButtonClick"
android:text="#string/p"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintTop_toTopOf="#+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="#+id/button2"
android:layout_marginRight="5dp"
app:layout_constraintRight_toRightOf="#+id/button4"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onDivisionButtonClick"
android:text="#string/d"
android:textSize="50sp"
android:layout_marginLeft="5dp"
app:layout_constraintRight_toRightOf="#+id/editText2"
app:layout_constraintTop_toTopOf="#+id/editText"
android:layout_marginTop="323dp"
tools:ignore="RtlHardcoded"
app:layout_constraintLeft_toRightOf="#+id/button3"
app:layout_constraintHorizontal_bias="0.0"
android:layout_marginStart="5dp"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="360dp"
android:layout_height="100dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:background="#color/colorAccent"
android:text="#string/Result"
android:textAlignment="center"
android:textAllCaps="true"
android:textSize="50sp"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.771"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.839"
tools:ignore="RtlHardcoded" />
Java Code ::
package com.example.narendra.calculator;
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 {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
EditText e1=(EditText)findViewById(R.id.editText);
EditText e2=(EditText)findViewById(R.id.editText2);
TextView t1=(TextView)findViewById(R.id.textView);
int num1=Integer.parseInt(e1.getText().toString());
int num2=Integer.parseInt(e2.getText().toString());
public void conditionCheck(){
if(e1.getText().toString().isEmpty()){
e1.setError("Please Enter a valid number");
}
else if(e2.getText().toString().isEmpty()){
e2.setError("Please Enter a valid number");
}
}
public void onAddButtonClick(View v){
conditionCheck();
int sum=num1+num2;
t1.setText(sum);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onSubtractionButtonClick(View v){
conditionCheck();
int diff=num1-num2;
t1.setText(diff);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onMultiplicationButtonClick(View v){
conditionCheck();
int product=num1*num2;
t1.setText(product);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onDivisionButtonClick(View v){
conditionCheck();
if(num2==0){
e2.setError(" Zero is not allowed here");
}
else {
int quotient = num1 / num2;
t1.setText(quotient);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
}
}
Logcat ::
06-14 12:31:23.216 4822-4822/? I/art: Not late-enabling -Xcheck:jni (already on)
06-14 12:31:23.216 4822-4822/? W/art: Unexpected CPU variant for X86 using defaults: x86
06-14 12:31:23.342 4822-4822/? W/System: ClassLoader referenced unknown path: /data/app/com.example.narendra.calculator-2/lib/x86
06-14 12:31:23.348 4822-4822/? I/InstantRun: starting instant run server: is main process
06-14 12:31:23.405 4822-4822/? D/AndroidRuntime: Shutting down VM
06-14 12:31:23.405 4822-4822/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.narendra.calculator, PID: 4822
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.narendra.calculator/com.example.narendra.calculator.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:2548)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:120)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:155)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:31)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:55)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:33)
at android.support.v7.app.AppCompatDelegateImplN.<init>(AppCompatDelegateImplN.java:33)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:201)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:185)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.narendra.calculator.MainActivity.<init>(MainActivity.java:17)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1078)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2538)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
Your onCreate() is closed early
Change it to
package com.example.narendra.calculator;
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 e1,e2;
int num1,num2;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
e1 = (EditText) findViewById(R.id.editText);
e2 = (EditText) findViewById(R.id.editText2);
t1 = (TextView) findViewById(R.id.textView);
num1 = Integer.parseInt(e1.getText().toString());
num2 = Integer.parseInt(e2.getText().toString());
} public void conditionCheck(){
if(e1.getText().toString().isEmpty()){
e1.setError("Please Enter a valid number");
}
else if(e2.getText().toString().isEmpty()){
e2.setError("Please Enter a valid number");
}
}
public void onAddButtonClick(View v){
conditionCheck();
int sum=num1+num2;
t1.setText(sum);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onSubtractionButtonClick(View v){
conditionCheck();
int diff=num1-num2;
t1.setText(diff);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onMultiplicationButtonClick(View v){
conditionCheck();
int product=num1*num2;
t1.setText(product);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
public void onDivisionButtonClick(View v){
conditionCheck();
if(num2==0){
e2.setError(" Zero is not allowed here");
}
else {
int quotient = num1 / num2;
t1.setText(quotient);
Toast.makeText(MainActivity.this, " Thank You ", Toast.LENGTH_SHORT).show();
}
}
}
Just modify the code of (view init) position,
like this:
MainActivity{
onCreate(){
View view = findViewById(id);
}
}
Note: the Activity is a component that has self life-cycle. so can't apply it as a simple Pojo
1.)lam.java
public class lam extends Activity {
//MediaPlayer ourSong;
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.end);
//View title = getWindow().findViewById(android.R.id.title);
// View titleBar = (View) title.getParent();
// titleBar.setBackgroundColor(Color.BLACK);
overridePendingTransition(R.anim.a1,R.anim.a2);
//ourSong=MediaPlayer.create(lam.this, R.raw.rollver);
addListenerOnButton();
}
#Override
public void onBackPressed() {
super.onBackPressed();
overridePendingTransition(R.anim.activity_open_translate,R.anim.activity_close_translate);
}
public void addListenerOnButton() {
final Context context = this;
Button tut1 = (Button) findViewById(R.id.button1);
Button tut2= (Button) findViewById(R.id.button2);
Button tut3 = (Button) findViewById(R.id.button3);
//Button tut4 = (Button) findViewById(R.id.button4);
Button tut6 = (Button) findViewById(R.id.button4);
Button tut5 = (Button) findViewById(R.id.button5);
tut1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
//marks
Intent i = new Intent(lam.this, MarksTask.class);
startActivity(i);
}
});
tut2.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(lam.this, timetable.class);
startActivity(intent);
}
});
tut3.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,further.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
/*tut4.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,bus.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
}); */
tut6.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,P_menu.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
tut5.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// ourSong.start();
Vibrator v = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
v.vibrate(60);
Intent intent = new Intent(context,S_lib.class);
startActivity(intent);
overridePendingTransition(R.anim.a1,R.anim.a2);
}
});
}
}
2.)end.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#FDF3E7"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="200dp"
android:layout_marginTop="10dp"
android:background="#drawable/wd" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button2"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/ef" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button3"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/qs" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button5"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/library" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:layout_marginBottom="10dp"
android:background="#3B3738" />
<Button
android:id="#+id/button4"
android:layout_width="250dp"
android:layout_height="100dp"
android:layout_marginBottom="50dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/buss" />
</LinearLayout>
</ScrollView>
i am getting the following error in log
A/libc(25871): Fatal signal 11 (SIGSEGV) at 0x0019c12c (code=1), thread 25918 (1234.xyz)
A/libc(25871): Fatal signal 11 (SIGSEGV) at 0x00001160 (code=1), thread 25871 (1234.xyz)
and my google play store error console shows following error-
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xyz.xyz/com.xyz.xyz.lam}: android.view.InflateException: Binary XML file line #49: Error inflating class <unknown>
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2190)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2239)
at android.app.ActivityThread.access$800(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5047)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:609)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #49: Error inflating class <unknown>
at android.view.LayoutInflater.createView(LayoutInflater.java:623)
at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:672)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:697)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:761)
at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
at android.view.LayoutInflater.inflate(LayoutInflater.java:400)
at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
at android.app.Activity.setContentView(Activity.java:1947)
at com.xyz.xyz.lam.onCreate(lam.java:21)
at android.app.Activity.performCreate(Activity.java:5249)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2154)
... 11 more
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:597)
... 26 more
Caused by: java.lang.OutOfMemoryError
at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:597)
at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:432)
at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:840)
at android.content.res.Resources.createFromResourceStream(Resources.java:2477)
at android.content.res.Resources.loadDrawable(Resources.java:2136)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:319)
at android.content.res.Resources.getDrawable(Resources.java:710)
at android.graphics.drawable.StateListDrawable.inflate(StateListDrawable.java:176)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:937)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:877)
at android.content.res.Resources.loadDrawable(Resources.java:2116)
at android.content.res.MiuiResources.loadDrawable(MiuiResources.java:319)
at android.content.res.TypedArray.getDrawable(TypedArray.java:602)
at android.view.View.<init>(View.java:3558)
at android.widget.TextView.<init>(TextView.java:623)
at android.widget.Button.<init>(Button.java:107)
at android.widget.Button.<init>(Button.java:103)
This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 4 years ago.
Hello I am trying to start activity from one to activity to other one but i get error
FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException
here is the code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button etprisijungti;
EditText etelpastas, etslaptazodis;
TextView reglink;
saugykla saugykla;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etprisijungti = (Button) findViewById(R.id.etprisijungti);
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
reglink = (TextView) findViewById(R.id.reglink);
reglink.setOnClickListener(this);
etprisijungti.setOnClickListener(this);
saugykla = new saugykla(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.etprisijungti:
vartotojas vartotojas = new vartotojas(null,null);
saugykla.saugoti(vartotojas);
saugykla.vartotojas_prisijunges(true);
startActivity(new Intent(this, pagrindinis.class));
break;
case R.id.reglink:
startActivity(new Intent(this, registracija.class));
break;
}
}
}
code where app crashes is
case R.id.reglink:
startActivity(new Intent(this, registracija.class));
break;
first case works fine code is exactly the same here is registracija class:
public class registracija extends AppCompatActivity implements View.OnClickListener {
Button etregistruotis;
EditText etelpastas, etslaptazodis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracija);
etregistruotis = (Button) findViewById(R.id.reglink)
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
etregistruotis.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.etregistruotis:
String elpastas = etelpastas.getText().toString();
String slaptazdodis = etslaptazodis.getText().toString();
break;
}
}
}
Logcat :
05-25 10:45:36.401 23721-23721/com.example.bsine_000.baseinas W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41241438)
05-25 10:45:36.411 23721-23721/com.example.bsine_000.baseinas E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bsine_000.baseinas/com.example.bsine_000.baseinas.registracija}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.bsine_000.baseinas.registracija.onCreate(registracija.java:23)
at android.app.Activity.performCreate(Activity.java:5203)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4960)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
activity main
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="El.pastas"/>
<EditText
android:id="#+id/etelpastas"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="slaptazodis"/>
<EditText
android:id="#+id/etslaptazodis"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/etprisijungti"
android:text="Prisijungti"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:enabled="true" />
<TextView
android:id="#+id/reglink"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20dp"
android:textStyle="bold"
android:padding="10dp"
android:layout_gravity="center_horizontal"
android:text="Registruotis"
android:clickable="true"
android:linksClickable="true" />
Activity registracija
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="El.pastas"/>
<EditText
android:id="#+id/etelpastas"
android:layout_width="match_parent"
android:layout_marginBottom="10dp"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="10dp"
android:text="slaptazodis"/>
<EditText
android:id="#+id/etslaptazodis"
android:inputType="textPassword"
android:layout_marginBottom="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/etregistruotis"
android:text="Registruotis"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Try this:
In your MainActivity replace this code:
startActivity(new Intent(this, registracija.class));
With this code:
startActivity(new Intent(MainActivity.this, registracija.class));
Hope it helps, if it didn't please post the code of activity_registracija.xml.
Edit: Reglink doesn't exist in your activity_registracija.xml.
public class registracija extends AppCompatActivity implements
View.OnClickListener {
Button etregistruotis;
EditText etelpastas, etslaptazodis;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registracija);
etregistruotis=(Button)findViewById(R.id. etregistruotis);
etelpastas = (EditText) findViewById(R.id.etelpastas);
etslaptazodis = (EditText) findViewById(R.id.etslaptazodis);
etregistruotis.setOnClickListener(this);
}
#Override
public void onClick(View v) {
String elpastas = etelpastas.getText().toString();
String slaptazdodis = etslaptazodis.getText().toString();
break;
}
}
please intialise the button on onCreate().
as you are making click on that same button no need to giva the id again in registracija.java.ithink this will resolve your error
As said in one comment, etregistruotis variable is not initialized. You need to do it like you do to the other two variables above and then only call the setOnClickListener.
If it still crashes after that please update your answer with the code you are using.
Bonus : your activity class starts with a lower case letter. By convention it should start with a capital letter.
Update: Reglink doesn't exist in the activity xml
I started coding my first app in Android Studio, what it should do at this stage is that you click a button with the digit and it outputs it into the textfield. When i run this code the application crashes right on startup and I have no idea what's wrong.
MyActivity.java
package com.example.david.calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import org.w3c.dom.Text;
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
private void BtnPressed(int i) {
result.setText(Integer.toString(i));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
activity_my.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/number"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="0"
android:background="#ffe7e7e7"
android:textSize="30sp"
android:gravity="center_vertical|right"
android:paddingRight="20dp"
android:editable="true"
android:enabled="true"
android:numeric="integer|signed|decimal" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="1"
android:id="#+id/button"
android:layout_below="#+id/number"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="2"
android:id="#+id/button2"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button"
android:layout_toRightOf="#+id/button"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="3"
android:id="#+id/button3"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="4"
android:id="#+id/button4"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="5"
android:id="#+id/button5"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button4"
android:layout_toRightOf="#+id/button4"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="6"
android:id="#+id/button6"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button5"
android:layout_toRightOf="#+id/button5"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="7"
android:id="#+id/button7"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="8"
android:id="#+id/button8"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button7"
android:layout_toRightOf="#+id/button7"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="9"
android:id="#+id/button9"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="0"
android:id="#+id/button11"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_below="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="10dp" />
<Button
android:layout_width="160dp"
android:layout_height="75dp"
android:text="="
android:id="#+id/button12"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button11"
android:layout_toRightOf="#+id/button11"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="+"
android:id="#+id/button13"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button3"
android:layout_toRightOf="#+id/button3"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="-"
android:id="#+id/button14"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button6"
android:layout_toRightOf="#+id/button6"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="*"
android:id="#+id/button15"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button9"
android:layout_toRightOf="#+id/button9"
android:layout_marginLeft="10dp" />
<Button
android:layout_width="75dp"
android:layout_height="75dp"
android:text="/"
android:id="#+id/button16"
android:textSize="50sp"
android:background="#ff0fdf22"
android:layout_alignTop="#+id/button12"
android:layout_toRightOf="#+id/button12"
android:layout_marginLeft="10dp" />
Logcat:
07-24 16:30:07.894 26564-26564/com.example.david.calculator D/AndroidRuntime﹕ Shutting down VM
07-24 16:30:07.894 26564-26564/com.example.david.calculator W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4190aba8)
07-24 16:30:07.899 26564-26564/com.example.david.calculator E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.david.calculator, PID: 26564
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.david.calculator/com.example.david.calculator.MyActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.app.Activity.findViewById(Activity.java:1884)
at com.example.david.calculator.MyActivity.<init>(MyActivity.java:18)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Thank you very much for all the replies.
You are setting your EditText in your main class and not in your classes onCreate method. You can't find a view if the activity hasn't been created yet.
public class MyActivity extends Activity {
EditText results;
private int number;
final EditText result = (EditText) findViewById(R.id.number);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Should become
public class MyActivity extends Activity {
EditText results;
private int number;
private EditText result;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
You were getting a NullPointerException in your log, this should fix it.
You should move
final EditText result = (EditText) findViewById(R.id.number);
inside onCreate(...) after setContentView(..)
Corrected Code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
EditText result = (EditText) findViewById(R.id.number);
Button one = (Button) findViewById(R.id.button);
one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
BtnPressed(1);
}
});
}
Check your code:
final EditText result = (EditText) findViewById(R.id.number);
This should come after setContentView() method just like with the button.
You should do this:
final EditText result = (EditText) findViewById(R.id.number);
in your onCreate after the setContentView.