+ 1

Compile successfull but app keeps crashing.

Been developing a simple android calculator app. This app have a single editText and when the user press the equal button the text on editText is saved into an array using getText () method. Used split() method to seperate the numbers from plus sign in the middle of a calculation. Right now i just want to see whether 1 st element in my array is the first number i entered into calculator. Like i said the compiling is successful but app keeps crashin . Can someone help? Below is the code that causes the trouble , without it app works fine(only the ui). String [] x = screen.getText().toString().split("+"); equals.setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v){screen.setText(x[0]);}});;

9th Nov 2020, 3:00 PM
Mons Joseph
Mons Joseph - avatar
6 Respuestas
+ 5
That's a (;) semicolon for terminating an statement, you app is crashing because of NullpointerExeption, Here you're probably getting null error, because there's no text in the EditText to be fetched from getText() at first and do those all operation, so you need Make sure you call this code when only EditText is not null. 👉 String [] x = screen.getText().toString().split("+"); This should work perfectly, If(screen !=null){ String [] x = screen.getText().toString().split("+"); equals.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // TODO: Implement this method screen.setText(x[0]); } }); }
10th Nov 2020, 3:04 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 5
import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import java.util.regex.Pattern; public class MainActivity extends Activity { String [] x; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); try{ Button one,plus,two,equals; final EditText screen; //initializing one,two etc... one=(Button)findViewById(R.id.one); two=(Button)findViewById(R.id.two); plus=(Button)findViewById(R.id.plus); equals=(Button)findViewById(R.id.equals); screen=(EditText)findViewById(R.id.screen); //the hard way of showing clicks on screen class MyListener implements View.OnClickListener { @Override public void onClick(View v) { screen.setText(screen.getText() + "1"); } } MyListener listener = new MyListener(); one.setOnClickListener(listener); //Part-1
11th Nov 2020, 2:42 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 5
//simplified way of showing clicks on screen two.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText() + "2");} }); plus.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) {screen.setText(screen.getText() + "+");} }); if(screen != null){ equals.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { // TODO: Implement this method try{ x=screen.getText().toString().split("\\+"); screen.setText(String.valueOf(Integer.parseInt(x[0])+Integer.parseInt(x[1]))); }catch(Exception e){ Toast.makeText(MainActivity.this,e.toString(),1000).show(); } } }); } }catch(Exception e){ Toast.makeText(MainActivity.this,e.toString(),1000).show(); } } } //Part-2
11th Nov 2020, 2:43 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 4
I think you have to try something like this, If(screen !=null){ String [] x = screen.getText().toString().split("+"); equals.setOnClickListener(new View.OnClickListener(){@Override public void onClick(View v){screen.setText(x[0]);}});; }
9th Nov 2020, 3:19 PM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
+ 4
Sorry, DM is not working, so I'm posting in this thread . There is no error or warning shuch as missing semicolon (;) when I tested your code, but app is crashing because of splitting text by '+' Charecter which is not supported, you have to use pattern.quote() method or add a two forward slah "\\+" to make it legal, here's your code with some little improvement.
11th Nov 2020, 2:40 AM
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~
0_O-[Mägár_Sám_Äkà_Nüllpøïntêr_Èxëcéptïön]~~ - avatar
0
I tried the code but ";" is missing..i don't know where to put it.i even tried possible places.
10th Nov 2020, 4:07 AM
Mons Joseph
Mons Joseph - avatar