简体   繁体   中英

Getting string from EditText NullPointerException error

I made an app that make EditText fields dynamically by the user choise, and get the text from it. This is how i setup my EditText fields: The user select option from a spinner and EditText field appear with the parameter he wants. The EditText ID is the row number is the spinner. The "field" parameter im my code is already declard at the begging of the program. the code:

public void setTheEditTextFiled(View rowNumber, LinearLayout linearLayout, EditText field, LayoutParams b){
    field = new EditText(this);
    field.setLayoutParams(b);
    field.setId(parametersChoose.getPositionForView(rowNumber));
    linearLayout.addView(field);

}

This seems to work fine, i get my EditText field where i want him. Now i want to take the text that user entered and put it in the database by pressing on "save" button. this is my code:

public void onClick(View v) {

    switch(v.getId())
     {
    case R.id.addWorkOut:
     {
         String exercise;
         String weight = "";
         String reps = "";


        try{
        if(findViewById(0) != null)
            weight = myWeight.getText().toString();
        if(findViewById(1) != null)
            reps = myReps.getText().toString();
        }
        catch (Exception e){

            Toast.makeText(this, "bad", Toast.LENGTH_SHORT).show();
        }

        exercise = addNewExercise.getText().toString();
        date = getIntent().getExtras();

        if(exercise.equals("") == false)
            exercise = addNewExercise.getText().toString();
        else
            exercise = workOutChoose.getSelectedItem().toString();

        DataBaseMain data = new DataBaseMain(this);
        data.open();
        data.createEntry(date.getString("date"), exercise, weight, reps);
        data.close();
                    break;

}

I also tried to call it like this:

if( (EditText) findViewById(0) != null)

but its now working.

The strange thing here, that is used to work. and suddenly its dont, i dont know why, i got this logcat error:

 05-07 20:00:45.159: E/AndroidRuntime(2045): FATAL EXCEPTION: main
05-07 20:00:45.159: E/AndroidRuntime(2045): java.lang.NullPointerException
05-07 20:00:45.159: E/AndroidRuntime(2045):     at com.example.workoutlog.AddWorkOutPage.onClick(AddWorkOutPage.java:112)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.view.View.performClick(View.java:4204)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.view.View$PerformClick.run(View.java:17355)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.os.Handler.handleCallback(Handler.java:725)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.os.Looper.loop(Looper.java:137)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at android.app.ActivityThread.main(ActivityThread.java:5041)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at java.lang.reflect.Method.invokeNative(Native Method)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at java.lang.reflect.Method.invoke(Method.java:511)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
05-07 20:00:45.159: E/AndroidRuntime(2045):     at dalvik.system.NativeStart.main(Native Method)

this line the log cat (AddWorkOutPage.java:112):

 com.example.workoutlog.AddWorkOutPage.onClick(AddWorkOutPage.java:112)

is this line in the code:

weight = myWeight.getText().toString();

Some how he doest get is ID.

Another thing, i tought the there is problem with my ID setting of the EditText fields. but i have another method that removes the EditText field by it own ID, and this method is working. So the ID of the EditText field is seems to be fine.

i tried to project-->clean. tried to delete R.java. tried to install/unistall the app. nothing helps.

I checked my XML code i dont have duplicare IDs, here is my XML code:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android">

     <Button
         android:id="@+id/addWorkOut"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="save" />

     <Button
         android:id="@+id/update"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="update" />

     <TextView
         android:id="@+id/textView1"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         android:text="Set new Exercise type or choose from the lsit below" />


     <EditText
         android:id="@+id/newExercise"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:ems="10" >

     </EditText>

     <Spinner
         android:id="@+id/workOutChoose"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />

     <Spinner
         android:id="@+id/parametersChoose"
         android:layout_width="match_parent"
         android:layout_height="wrap_content" />

        <ScrollView
            android:id="@+id/scroll"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" >

            <LinearLayout
                android:id="@+id/test"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical" >



            </LinearLayout>

         </ScrollView>



   ></LinearLayout>

Thank for helping :)

What is findViewByID(0) ?

When you add components to your layout the Id's are generated automatically by R file.

So you need to add to your layout EditText with ID:'myWeight" and get it with:

EditText weight = (EditText)findViewByID(R.id.myWeight);

It's not recommend to get the view by some number.. it will give you an error or the ID will not be related to the correct component. See, right now, when you write findViewById(0) , 0 can be related to anything, like the button in your layout.. so this is the reason why people use R.id.someID the R file matching between the Id's name you give in your layout to some integer numbers. In this way you can prevent mistakes, as you have right now (it doesn't give you the correct component)

Add something like this:

        EditText weight = (EditText)findViewByID(R.id.myWeight);

You need to reference the id of your EditText .

Well i manage to solve it. for anyone who dealing with dynamic field in is app, herer is the solution. thanks for who helped me this is the solution: I just had to find on myWeight by is ID outsite the if statement. :)

...
...

 myWeight = (EditText) findViewById(0);
 myReps = (EditText) findViewById(1);


        if(myWeight != null)
            weight = myWeight.getText().toString();
        if(myReps != null)
            reps = myReps.getText().toString();

...
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM