Getting error while accessing textfield in Android application - android

I am getting value of EditText e1 in below example as null. So it throws null pointer exception. I wrote below in MainActivity and calling the function calculateSquare upon click on a button. Am i missing something?
public void calculateSquare(View view) {
setContentView( R.layout.activity_main);
EditText e1=(EditText) findViewById(R.id.editText1);
int number = Integer.parseInt(e1.getText().toString());
int square=number * number;
TextView e2=(TextView) findViewById(R.id.textView2);
e2.setText(square);
}
activity_main.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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp"
android:text="#string/hello_world"
tools:context=".MainActivity" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="32dp"
android:layout_marginTop="52dp"
android:layout_toLeftOf="#+id/textView1"
android:ems="10"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="29dp"
android:text="CalculateSquare"
android:onClick="calculateSquare"/>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/editText1"
android:layout_marginRight="36dp"
android:text="Square"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>

You need to pass in a String as a parameter to the setText().
Change your code to:
e2.setText(square+"");
When you call setText(int) the system will look for the String resource by the given id(int), which needs to be supplied from the R class.
e2.setText(R.string.mystring);
About the null pointer, try cleaning your project, and you should move the initiation to the onCreate() method.
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView( R.layout.activity_main);
EditText e1=(EditText) findViewById(R.id.editText1);
TextView e2=(TextView) findViewById(R.id.textView2);
}
public void calculateSquare(View view) {
int number;
if (e1.getText().toString().length()>0){
number = Integer.parseInt(e1.getText().toString());
int square=number * number;
e2.setText(square+"");
}
}
You always need to check if you have entered something in the edittext, since an empty string cannot be converted into an int.

Related

Getting error while calling Intent from fragment (User Login)

I have getting error while calling intent from Fragment.
Following are the log error of the same. Below are the pieces of code from the same activities. The application automatically gets off as i click on the Button for Login.
Please help me out.
Thank you in advance.
Log Report
Process: com.example.lenovo.skanda, PID: 7978
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.example.lenovo.skanda.AdminFragment$1.onClick(AdminFragment.java:43)
HomeFragment.java
public class AdminFragment extends Fragment implements View.OnClickListener{
View viewroot;
View v1;
private EditText Name;
private EditText Password;
static Button Login;
public static Button myLog;
private int Counter = 5;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
viewroot = inflater.inflate(R.layout.fragment_admin,container,false);
Name = (EditText) getActivity().findViewById(R.id.editText);
Password = (EditText) getActivity().findViewById(R.id.editText2);
Login = (Button) getActivity().findViewById(R.id.button2);
myLog = (Button) viewroot.findViewById(R.id.button2);
myLog.setOnClickListener(this);
myLog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
validate(Name.getText().toString(), Password.getText().toString());
}
});
return viewroot;
}
private void validate (String userName, String userPassword){
if((userName.equals("Admin")) && (userPassword.equals("123"))){
Intent intent = new Intent(AdminFragment.this.getActivity(), Login.class);
startActivity(intent); //Login is the second activity
}
else {
Counter --;
Toast.makeText(getActivity(), "No. of Attempts Left for Login" + String.valueOf(Counter), Toast.LENGTH_SHORT).show();
if (Counter == 0){
Login.setEnabled(false);
Toast.makeText(getActivity(), "For Enable the Login Please Contact..!!", Toast.LENGTH_SHORT).show();
}
}
}
#Override
public void onClick(View v) {
validate(Name.getText().toString(),Password.getText().toString());
}
}
fragment_admin.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#android:color/background_dark">
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/animation_view"
android:layout_width="185dp"
android:layout_height="155dp"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
app:lottie_autoPlay="true"
app:lottie_colorFilter="#FFBC00"
app:lottie_fileName="user.json"
app:lottie_loop="true" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:text="admin login"
android:textAllCaps="true"
android:textColor="#FFBA24"
android:textSize="25dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText"
android:layout_alignStart="#+id/editText2"
android:layout_marginBottom="-247dp"
android:text="Enter your username"
android:textAllCaps="true"
android:textColor="#B7BE5D"
android:textSize="15dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="183dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/editText2"
android:layout_alignStart="#+id/editText2"
android:text="Enter your password"
android:textAllCaps="true"
android:textColor="#B7BE5D"
android:textSize="15dp" />
<EditText
android:id="#+id/editText"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="247dp"
android:ems="10"
android:fontFamily="monospace"
android:hint="Username"
android:inputType="textPersonName"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF" />
<EditText
android:id="#+id/Editinfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="62dp"
android:text="No. of attempts left : "
android:textColor="#FFF"
android:textSize="15dp"
android:textStyle="italic" />
<Button
android:id="#+id/button2"
android:layout_width="210dp"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="111dp"
android:background="#drawable/rounded_button"
android:text="login"
android:textAllCaps="true"
android:textColor="#000"
android:visibility="visible" />
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless.Colored"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#19193E"
android:text="LOG IN PROBLEM? CONTACT US."
android:textColor="#FFF"
android:onClick="onClick"
android:visibility="visible" />
</RelativeLayout>
You can't use getActivity().findViewById() like that.
Inside onCreateView(), you haven't yet returned the Fragment's View, meaning it isn't attached to the Activity. getActivity().findViewById() returns null because the Activity currently has no View with that ID. It only will after viewroot is returned, which is after you're invoking it.
Instead, you can do one of two things:
Replace getActivity() with viewroot:
Name = (EditText) viewroot.findViewById(R.id.editText);
Password = (EditText) viewroot.findViewById(R.id.editText2);
Login = (Button) viewroot.findViewById(R.id.button2);
Move your code to onViewCreated() and keep using getActivity() (you should be using the passed view variable, however).
In general, you don't want to use getActivity().findViewById(). If you need to find a View in that Fragment, use getView().findViewById(). Neither of these methods will work until onCreateView() returns.
I also notice that both Login and myLog are the same button. You should assign to one variable and call on it when needed. There's no point in a second local variable.
You should also try following Java's syntax guidelines:
Class names use TitleCase
Variable and function names use camelCase
it should be like this
viewroot = inflater.inflate(R.layout.fragment_admin,container,false);
Name = (EditText) viewroot.findViewById(R.id.editText);
Password = (EditText) viewroot.findViewById(R.id.editText2);
Login = (Button) viewroot.findViewById(R.id.button2);
myLog = (Button) viewroot.findViewById(R.id.button2);
You need to use viewroot instead of getActivity() in onCreateView().
Use the following code:
Name = (EditText) viewroot.findViewById(R.id.editText);
Password = (EditText) viewroot.findViewById(R.id.editText2);
Login = (Button) viewroot.findViewById(R.id.button2);

getCheckedRadioButtonId just gives me nullpoint exception

Im having some issues, i have two Groups of Radiobuttons in my XML which has handling in an activity. It just crashes with nullpoint exception on line 48:
int languangeId = languageGroup.getCheckedRadioButtonId();
I have set a "default" checked button in the XML to one of the buttons in the buttongroup,
So why doesnt it get an valid value?:(
public class FirstTimeSelectMenu extends Activity{
private RadioGroup languageGroup;
private RadioGroup storageGroup;
private Button okButton;
private String getStorage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_time_run);
languageGroup = (RadioGroup)this.findViewById(R.id.languageGroup);
storageGroup = (RadioGroup)this.findViewById(R.id.storageGroup);
okButton = (Button)findViewById(R.id.okBtn);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int languangeId = languageGroup.getCheckedRadioButtonId();
int storageId = storageGroup.getCheckedRadioButtonId();
RadioButton languageb = (RadioButton)findViewById(languangeId);
RadioButton storageb = (RadioButton)findViewById(storageId);
if(languageb.equals("Norwegian")){
//Need to fix this!
}
if(languageb.equals("English")){
//Need to fix this!
}
if(storageb.equals("SD Card")){
String sdStoragePath = Environment.getExternalStorageDirectory().toString() + "/PictureTalk/";
FileInteraction fi = new FileInteraction();
fi.firstTimeFillPath(getResources(), "PictureTalk/Food", sdStoragePath +"PictureTalk/Food");
fi.firstTimeFillPath(getResources(), "PrivatePictures", Environment.getExternalStorageDirectory().toString() +"PrivatePictures");
Intent intent = new Intent(getApplicationContext(),MainMenuActivity.class);
intent.putExtra("dataStorePath",sdStoragePath);
startActivity(intent);
}
}
});
}}
My XML file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/languagetext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Choose language" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/languagetext"
android:layout_alignRight="#+id/languagetext"
android:layout_below="#+id/languageGroup">
<RadioButton
android:id="#+id/norwegianRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Norwegian" />
<RadioButton
android:id="#+id/englishRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="English" />
</RadioGroup>
<TextView
android:id="#+id/internal_sd"
android:layout_width="464dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="236dp"
android:text="Do you want to store the data to:" />
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:id="#+id/storageGroup">
<RadioButton
android:id="#+id/sdcardRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:checked="true"
android:text="SD card" />
<RadioButton
android:id="#+id/internalRadioBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="Internal storage" />
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok"
android:id="#+id/okBtn"
android:layout_gravity="bottom" />
To start off with, what element is assigned the id "#+id/languageGroup" since it doesn't appear to be declared in your XML

Android Button onClick listener error?

i Have the following code:
in onCreate method:
{
saveClick = (Button) findViewById(R.id.saveBtn);
saveClick.setOnClickListener(this);
}
#Override
public void onClick(View v) {
//------------ take input data and save in DB --------------------
String cpassword;
EditText num_1=(EditText)findViewById(R.id.num1);
EditText num_2=(EditText)findViewById(R.id.passkey1);
EditText num_3=(EditText)findViewById(R.id.passkey2);
number=num_1.getText().toString();
password=num_2.getText().toString();
cpassword=num_3.getText().toString();
DBoperation db=DBoperation.getInstance(getApplicationContext());
if(password == cpassword)
{
TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
pnumber = tMgr.getLine1Number();
db.addSIMSs(pnumber);
db.addSIMSs(number);
db.addPassKey(password);
}
}
i am using eclipse for development and it keeps giving the following errors:
at line:saveClick.setOnClickListener(this);
error:
the method setOnClickListener(View.setOnClickListener) in the type view is not applicable for the arguments.
also at line:public void onClick(View v) {
error:
the method onClick(View) of type MainActivity must override or implement a supertype method.
following is the XML file:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="113dp"
android:text="TextView" />
<EditText
android:id="#+id/num1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="38dp"
android:layout_marginTop="37dp"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/passkey1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/num1"
android:layout_below="#+id/num1"
android:layout_marginTop="86dp"
android:ems="10"
android:inputType="textPassword" />
<EditText
android:id="#+id/passkey2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/passkey1"
android:layout_below="#+id/passkey1"
android:layout_marginTop="49dp"
android:ems="10"
android:inputType="textPassword" />
<TextView
android:id="#+id/pass1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/passkey1"
android:layout_alignLeft="#+id/textView2"
android:layout_marginBottom="35dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/pass2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignTop="#+id/passkey2"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="#+id/number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pass1"
android:layout_below="#+id/textView1"
android:layout_marginTop="24dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/saveBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/passkey2"
android:layout_marginLeft="14dp"
android:layout_marginTop="41dp"
android:text="#string/save" />
</RelativeLayout>
Have you implemented OnClicklistener to your Activity?
public class MainActivity extends Activity implements OnClickListener {
In this line.-
saveClick.setOnClickListener(this);
I'm guessing this is an instance of Activity, when it should be an OnClickListener. Try something like this instead.-
saveClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do your stuff here
}
});
As for the second error, the compiler is complaining that you're trying to override method onClick in your Activity, but its parent doesn't implement that method. Just remove onClick method from the Activity, and move that logic to the listener onClick method.
You must implement the OnCLickListener in your class initializing. That is
public class MyClass extends Activity implements View.OnCLickListener
Use this , it will work.
saveClick = (Button) findViewById(R.id.saveBtn);
saveClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String cpassword;
EditText num_1=(EditText)findViewById(R.id.num1);
EditText num_2=(EditText)findViewById(R.id.passkey1);
EditText num_3=(EditText)findViewById(R.id.passkey2);
number=num_1.getText().toString();
password=num_2.getText().toString();
cpassword=num_3.getText().toString();
DBoperation db=DBoperation.getInstance(getApplicationContext());
if(password == cpassword)
{
TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
pnumber = tMgr.getLine1Number();
db.addSIMSs(pnumber);
db.addSIMSs(number);
db.addPassKey(password);
}
}
});
I think you can have problem in importing library of onclicklistner; which seems you have done
public class MainActivity extends Activity implements OnClickListener
why dont you try with
saveClick.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// Do your stuff here
}
});
rather then
saveClick.setOnClickListener(this);
which will help you to debug futher
Add this to your onClick
if (v.getId() == R.id.saveBtn) {
//your code
String cpassword;
EditText num_1=(EditText)findViewById(R.id.num1);
EditText num_2=(EditText)findViewById(R.id.passkey1);
EditText num_3=(EditText)findViewById(R.id.passkey2);
number=num_1.getText().toString();
password=num_2.getText().toString();
cpassword=num_3.getText().toString();
DBoperation db=DBoperation.getInstance(getApplicationContext());
if(password == cpassword)
{
TelephonyManager tMgr =(TelephonyManager)getApplicationContext().getSystemService(Context.TELEPHONY_SERVICE);
pnumber = tMgr.getLine1Number();
db.addSIMSs(pnumber);
db.addSIMSs(number);
db.addPassKey(password);
}
}

Null Pointer Exception - Getting EditText Value

This is my first question on here I will apologize in advance if I didn't ask this question very well. I have looked at my other questions similar to my problem, but I have not found a good solution to satisfy what is going on in my program.
So my problem is I am trying to assign a value to a variable called, num1 from my EditText field called, num1TextField, but I am not having any luck so far.
The segment inside my java file that throws the exception is:
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
num1 = num1Field.getText().toString();
I appreciate every contribution to this problem in advance.
Here is my fragment_compute.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="#string/compute_activity"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:text="#string/num_1"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/num1TextField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView2"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:gravity="center" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/num2TextField"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/num1TextField"
android:layout_below="#+id/textView3"
android:ems="10"
android:inputType="number"
android:layout_centerHorizontal="true"
android:gravity="center" >
</EditText>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/num2TextField"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/result"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/num1TextField"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:text="#string/num_2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
<Button
android:id="#+id/compute_multiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/num2TextField"
android:layout_below="#+id/result"
android:layout_marginTop="40dp"
android:text="#string/multiply" />
<Button
android:id="#+id/compute_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/compute_multiply"
android:layout_alignBottom="#+id/compute_multiply"
android:layout_alignLeft="#+id/num2TextField"
android:text="#string/add" />
</RelativeLayout>
Here is my ComputeFragment.java code:
public class ComputeFragment extends Fragment {
String num1;
String num2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_compute, parent, false);
// Add Button
Button addButton = (Button)v.findViewById(R.id.compute_add);
addButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// ERROR STARTS HERE!
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
num1 = num1Field.getText().toString(); // Null Pointer Exception is thrown here!
EditText num2Field = (EditText)v.findViewById(R.id.num2TextField);
num2 = num2Field.getText().toString();
// ERROR ENDS HERE!
Intent i = new Intent(getActivity(), AddActivity.class);
i.putExtra("x", num1);
i.putExtra("y", num2);
startActivityForResult(i,0);
}
});
return v;
}
}
The parameter v in EditText num1Field = (EditText)v.findViewById(R.id.num1TextField); is your button, not the whole view.
You should declare the variable in onCreateView:
final EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
And use it to get the value in onClick
num1 = num1Field.getText().toString();
The parameter view is the button. If the button is on the same parent as the editTexts you could also do:
View parentView = (View) view.getParent();
EditText num1TextField = (EditText)parentView.findViewById(R.id.num1TextField);
Try this code -
#Override
public void onClick(View view) {
EditText num1Field = (EditText)v.findViewById(R.id.num1TextField);
if(num1Field.getText().length() > 0) {
num1 = num1Field.getText().toString();
}
}

negative number input(EditText) getting unsigned

i'm doing a cable length calculator, and i'm having trouble with negative numbers.
EditTexts are like this
<EditText android:layout_height="wrap_content" android:layout_weight="1" android:inputType="numberDecimal|numberSigned" android:layout_width="40px" android:id="#+id/rxmax">
</EditText>
Then i use them like this:
final EditText rxmax = (EditText) findViewById(R.id.rxmax);
double RXmax = new Double(rxmax.getText().toString());
After i do a simple calculation:
double OPBmax = TXmax - RXmax;
Somewhere the inputted negative number turns positive. i'm guessing at the toString conversation but i don't find anything on how to prevent this.
Use
android:digits="0123456789"
EX:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:digits="0123456789"
android:gravity="center"
android:inputType="numberSigned"
android:textColor="#color/black"
android:textSize="#dimen/font_size_small" />
I've tried this, and it works...
Activity:
double RXmax;
EditText rxmax;
TextView tv;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
rxmax = (EditText) findViewById(R.id.rxmax);
tv = (TextView) findViewById(R.id.text);
}
public void click(View v) {
RXmax = new Double(rxmax.getText().toString());
tv.setText(Double.toString(RXmax));
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText android:layout_height="wrap_content"
android:layout_weight="1" android:inputType="numberDecimal|numberSigned"
android:layout_width="40px" android:id="#+id/rxmax">
</EditText>
<TextView android:id="#+id/text" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="#string/hello" />
<Button android:layout_height="wrap_content" android:id="#+id/button1"
android:layout_weight="1" android:text="Button" android:layout_width="wrap_content"
android:onClick="click"></Button>
</LinearLayout>
If I type -2, after clicking the TextView displays -2.0
If you're using Eclipse, try Project --> Clean..., and select your project. Maybe this will help.

Categories

Resources