+ 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]);}});;
6 Answers
+ 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]);
}
});
}
+ 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
+ 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
+ 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]);}});;
}
+ 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.
0
I tried the code but ";" is missing..i don't know where to put it.i even tried possible places.