Problem saving data and transfering to another activity (using FireBase) - java

I'm having problems while attempting to save data input (from the user) via two EditText and transferring it to another activity where it should be displayed as a ViewText.
Code from activity/data input
public class DatosBasicos extends AppCompatActivity {
EditText presion;
EditText ritmo;
Button atriaje;
String valorritmo;
String valorpresion;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_datos_basicos);
atriaje = (Button) findViewById(R.id.triaj);
presion = (EditText) findViewById(R.id.presion);
ritmo = (EditText) findViewById(R.id.ritmo);
atriaje.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
valorritmo = ritmo.getText().toString();
valorpresion = presion.getText().toString();
Intent i = new Intent(DatosBasicos.this, Triaje.class);
i.putExtra("PRESION",valorpresion);
i.putExtra("RITMO",valorritmo);
startActivity(i);
finish();
}
});
}
}
Code from activity/data output
public class Triaje extends AppCompatActivity {
TextView rit, pres;
String ritS, presS;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_triaje);
rit = (TextView) findViewById(R.id.ritmoval);
pres = (TextView) findViewById(R.id.presionval);
ritS = getIntent().getExtras().getString("PRESION");
presS = getIntent().getExtras().getString("RITMO");
rit.setText(ritS);
pres.setText(presS);
}
}
ERROR LOG
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.aplicaciontriaje, PID: 2569
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.aplicaciontriaje/com.example.aplicaciontriaje.Triaje}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.TextView
at com.example.aplicaciontriaje.Triaje.onCreate(Triaje.java:19)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
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) 
Disconnected from the target VM, address: 'localhost:8619', transport: 'socket'
WHAT SHOULD THE APP DO
I expect to get my data input into my Triaje activity and displayed in the two corresponding ViewText.
I get the following error: "App keeps stopping"

Is it possible to debug the system ? Then you clearly understand exactly where the it gives you error! Then tell exactly in which line it gives error!

Related

Android Studio Emulator Crashing On Button Click

I am very new to Android Studio and am trying to create a log in screen which then opens an inventory screen. The problem I am having is when I click Submit to the username and password, the app completely crashes. Right now, I am just working out how to organize the layout portion of things, but need to figure out how to get this transition to work. Any help would be great!
This is the main activity code:
package com.example.cs360projectone;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
EditText username, password;
Button buttonLogIn, buttonRegister;;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
username = (EditText) findViewById(R.id.username);
password = (EditText) findViewById(R.id.newPassword);
buttonLogIn = (Button) findViewById(R.id.buttonLogIn);
buttonRegister = (Button) findViewById(R.id.buttonRegister);
buttonLogIn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(username.getText().toString().trim().length() <1 || password.getText().toString().trim().length() <1){
username.setError("You must enter a valid username and password");
}
else {
openInventory();
}
}
});
buttonRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
openRegisterScreen();
}
});
}
public void openRegisterScreen() {
Intent intent = new Intent(this, RegisterScreen.class);
startActivity(intent);
}
public void openInventory() {
Intent intent = new Intent(this, InventoryScreen.class);
startActivity(intent);
}
}
This is the inventory screen code:
package com.example.cs360projectone;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class InventoryScreen extends AppCompatActivity {
Button buttonSMS, buttonSignOut;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inventory_screen);
buttonSMS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view1) {
openSMSPermission();
}
});
}
public void openSMSPermission() {
Intent intent = new Intent(this, SMSPermissionScreen.class);
startActivity(intent);
}
}
This is the error from logcat when the button is pressed:
2022-08-03 16:54:09.037 8043-8043/com.example.cs360projectone E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.cs360projectone, PID: 8043
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.cs360projectone/com.example.cs360projectone.InventoryScreen}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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 invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.cs360projectone.InventoryScreen.onCreate(InventoryScreen.java:18)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
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) 
2022-08-03 16:54:09.062 8043-8043/com.example.cs360projectone I/Process: Sending signal. PID: 8043 SIG: 9
You need to get the SMS button in the InventoryScreen onCreate method the same way you did with the other buttons in MainActivity.
This means doing a
buttonSMS = (Button) findViewById(R.id.buttonSMS);
before setting up an onClickListener.

App crash, not sure what the error is pointing me too

Whenever I run this program in Andriod studio The app just cashes on the phone.
package com.example.homework1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
double tipPercent;
SeekBar seekBar;
TextView tipAmountString;
double tipAmountCalc;
TextView totals;
EditText BaseBillTotal;
EditText TipValue;
double baseValueCalc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TipValue = findViewById(R.id.TipValue);
seekBar = findViewById(R.id.seekBar);
tipAmountString = findViewById(R.id.tipAmountString);
totals = findViewById(R.id.TotalValueScreen);
BaseBillTotal = findViewById(R.id.BillBaseValue);
baseValueCalc = Double.parseDouble(BaseBillTotal.getText().toString());
seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
tipAmountString.setText(String.valueOf(progress));
tipAmountCalc = progress*.10;
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
RadioGroup radioGroup = findViewById(R.id.tipPercentGroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
if(checkedId == R.id.radioButton){
tipPercent = .10;
}
else if (checkedId == R.id.radioButton2){
tipPercent = .15;
}
else if (checkedId == R.id.radioButton3){
tipPercent = .18;
}
else if (checkedId == R.id.radioButton4)
{
tipPercent = tipAmountCalc;
}
}
});
TipValue.setText(String.valueOf(tipPercent*100));
totals.setText(String.valueOf(tipPercent * baseValueCalc) );
}
}
This is the error in the console
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.homework1, PID: 7027
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.homework1/com.example.homework1.MainActivity}: java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText
at com.example.homework1.MainActivity.onCreate(MainActivity.java:32)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
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) 
The stack trace explains exactly what is breaking and why:
java.lang.ClassCastException: androidx.constraintlayout.widget.ConstraintLayout cannot be cast to android.widget.EditText at com.example.homework1.MainActivity.onCreate(MainActivity.java:32)
line 32 of your MainActivity is BaseBillTotal = findViewById(R.id.BillBaseValue);
You have BaseBillTotal defined in your Activity as an EditText, but according to this exception, your layout file has a ConstraintLayout with an id of BillBaseValue, and you can't cast that to an EditText. Double check your layout file and verify the type and ID there.
Check the id of your edit texts. According to the error you might have given the constraint layout the id of BillBaseValue or TipValue instead of the actual edit text in your xml layout.

Firebase NullPointerException Error While have user id

intent = getIntent();
final String userid = intent.getStringExtra("userid");
I have Checked and verified that user id is coming but can not use in this child but before 2-3 days the same code working for me, from to now i have not changed anything
I have userid from Adapter position and i can use it in this activity and its working but in the given line of code error occur
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
that can not pass java.lang.NullPointerException:
Can't pass null for argument 'pathString' in child()
Error is given as below:
2020-02-27 21:46:53.567 3248-3248/com.example.chat E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.chat, PID: 3248
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.chat/com.example.chat.MessageActivity}: java.lang.NullPointerException: Can't pass null for argument 'pathString' in child()
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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: Can't pass null for argument 'pathString' in child()
at com.google.firebase.database.DatabaseReference.child(com.google.firebase:firebase-database##19.2.0:96)
at com.example.chat.MessageActivity.onCreate(MessageActivity.java:175)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
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) 
Code part is given as below
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(userid);
reference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
Last_seen.setText(user.getStatus());
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});

How to fix 'IllegalStateException' (fatal exception) in android studio?

I am new to Android Studio and trying to build a calculator which performs addition. On executing the app on my phone there is an error which says 'IllegalStateException' and there is a message displayed on my phone saying that has app has stopped working. Some help please.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View v) throws IllegalStateException
{
EditText e1 = (EditText)findViewById(R.id.editText1);
EditText e2 = (EditText)findViewById(R.id.editText2);
TextView t1 = (TextView)findViewById(R.id.view);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int RESULT = num1+num2;
t1.setText(Integer.toString(RESULT));
}
}
The stack trace after running after entering the values
--------- beginning of crash
2019-07-16 11:02:03.122 14400-14400/com.example.calculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.calculator, PID: 14400
java.lang.IllegalStateException: Could not execute method for android:onClick
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:390)
at android.view.View.performClick(View.java:5675)
at android.view.View$PerformClick.run(View.java:22646)
at android.os.Handler.handleCallback(Handler.java:836)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:203)
at android.app.ActivityThread.main(ActivityThread.java:6251)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385)
at android.view.View.performClick(View.java:5675) 
at android.view.View$PerformClick.run(View.java:22646) 
at android.os.Handler.handleCallback(Handler.java:836) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:203) 
at android.app.ActivityThread.main(ActivityThread.java:6251) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
Caused by: java.lang.ClassCastException: android.view.View cannot be cast to android.widget.TextView
at com.example.calculator.MainActivity.onButtonClick(MainActivity.java:22)
at java.lang.reflect.Method.invoke(Native Method) 
at androidx.appcompat.app.AppCompatViewInflater$DeclaredOnClickListener.onClick(AppCompatViewInflater.java:385) 
at android.view.View.performClick(View.java:5675) 
at android.view.View$PerformClick.run(View.java:22646) 
at android.os.Handler.handleCallback(Handler.java:836) 
at android.os.Handler.dispatchMessage(Handler.java:103) 
at android.os.Looper.loop(Looper.java:203) 
at android.app.ActivityThread.main(ActivityThread.java:6251) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1075) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936) 
This is the error:
java.lang.ClassCastException: android.view.View cannot be cast to
android.widget.TextView at
com.example.calculator.MainActivity.onButtonClick(MainActivity.java:22)
in this line:
TextView t1 = (TextView)findViewById(R.id.view);
because R.id.view is the id of a View, not a TextView (check your layout xml).
You need to make that view a textview in your layout file.
try this:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onButtonClick(View view){
EditText e1 = view.findViewById(R.id.editText1);
EditText e2 = view.findViewById(R.id.editText2);
TextView t1 = view.findViewById(R.id.view);
int num1 = Integer.parseInt(e1.getText().toString());
int num2 = Integer.parseInt(e2.getText().toString());
int result = num1+num2;
t1.setText(Integer.toString(result));
}
}

Android Studio Emulator Not Running. Codes are OK

I am writing an app for my company but I cannot see my work since my emulator is not running.
Here is my code below. There are no errors. I am running this in android studio.
It runs on some apps but I tried inputting my code then it does not work.
Please Help
package com.example.tankoverfill;
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 {
Button calc;
TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);
EditText flow, areas, heights, densities, heightones, cvs;
int x1,y1,w1,a1, x2,y2,w2,a2,g;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
flow = findViewById(R.id.flow);
areas = findViewById(R.id.areas);
heights = findViewById(R.id.heights);
densities = findViewById(R.id.densities);
heightones = findViewById(R.id.heightones);
cvs = findViewById(R.id.cvs);
calc = findViewById(R.id.calc);
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
x1 = Integer.parseInt(flow.getText().toString());
y1 = Integer.parseInt(cvs.getText().toString());
w1 = Integer.parseInt(densities.getText().toString());
g = (int) 9.8;
a1 = (x1 / (y1*w1*g));
tv_heightfills.setText(a1);
}
});
calc.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
x2 = Integer.parseInt(areas.getText().toString());
y2 = Integer.parseInt(cvs.getText().toString());
w2 = Integer.parseInt(densities.getText().toString());
g = (int) 9.8;
a2 = (x1 / (y1*w1*g));
tv_timefills.setText(a2);
}
});
}
}
D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION:
main
Process: com.example.tankoverfill, PID: 10137
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.tankoverfill/com.example.tankoverfill.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:2843)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
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:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
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 invoke virtual method 'android.view.Window$Callback
android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImpl.(AppCompatDelegateImpl.java:249)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:182)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:520)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:191)
at com.example.tankoverfill.MainActivity.(MainActivity.java:15)
at java.lang.Class.newInstance(Native Method)
at android.app.AppComponentFactory.instantiateActivity(AppComponentFactory.java:69)
at android.support.v4.app.CoreComponentFactory.instantiateActivity(CoreComponentFactory.java:43)
at android.app.Instrumentation.newActivity(Instrumentation.java:1215)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2831)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048) 
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:1808) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:193) 
at android.app.ActivityThread.main(ActivityThread.java:6669) 
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) 
I/Process: Sending signal. PID: 10137 SIG: 9 Application terminated.
Hello two of the errors you have in your code are:
first error that you must link the two textview to xml inside onCreate() method
error
TextView tv_heightfills = findViewById(R.id.tv_heightfills);
TextView tv_timefills = findViewById(R.id.tv_timefills);
correct
TextView tv_heightfills;
TextView tv_timefills;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv_heightfills = findViewById(R.id.tv_heightfills);
tv_timefills = findViewById(R.id.tv_timefills);
....
}
Second error is setting int value to the textview setText() method
so it should be:
tv_heightfills.setText(String.valueOf(a1));
same for
tv_timefills.setText(String.valueOf(a2));

Categories