-1

i have been creating an App that uses Fragments Mostly here is how it is supposed to work, on the Home Fragment i click a button that takes me to another Fragment for filling information on this fragment when I click a button a Dialog Fragment opens and I select a City Name then Submit, it dismisses the Dialog and is supposed to SetText on a TextView.

I use an interface that calls a method on the City Selection fragment in order to set the Text. here is some Code

Declaration for the EditText

EditText editText_From;

Setting finding the View

OnCreate

   @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {

        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_trip_date, container, false);

        editText_From = view.findViewById(R.id.editText_From);

        return view;
    }


       public void setSelectedCity(String city)
{

    Log.i("<<269>>", "Setting Text on Edit Text <<269>>:" + city);

   editText_From.setText(city);

}

This Method below is supposed to set text for the my EditText or TextView

    public void setSelectedCity(String city)
{

    Log.i("<<269>>", "Setting Text on Edit Text <<269>>:" + city);
    selectedCityConfirmed = city;

   editText_From.setText(city);

}

the App Crashes on EditText with the Following Error Message,

02-16 19:37:43.928 26582-26582/com.example.bob2609.busticketingapp E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.bob2609.busticketingapp, PID: 26582
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.support.v4.app.FragmentActivity.findViewById(int)' on a null object reference
        at com.example.bob2609.busticketingapp.TripDateFragment.setSelectedCity(TripDateFragment.java:188)
        at com.example.bob2609.busticketingapp.MainActivity.selectedCity(MainActivity.java:176)
        at com.example.bob2609.busticketingapp.LocationSelector$1.onItemClick(LocationSelector.java:69)
        at android.widget.AdapterView.performItemClick(AdapterView.java:313)
        at android.widget.AbsListView.performItemClick(AbsListView.java:1201)
        at android.widget.AbsListView$PerformClick.run(AbsListView.java:3195)
        at android.widget.AbsListView$3.run(AbsListView.java:4138)
        at android.os.Handler.handleCallback(Handler.java:761)
        at android.os.Handler.dispatchMessage(Handler.java:98)
        at android.os.Looper.loop(Looper.java:156)
        at android.app.ActivityThread.main(ActivityThread.java:6523)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:942)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:832)

The Interface Method on MainActivity

  @Override
public void selectedCity(String city)
{


    tripDateFragment.setSelectedCity(city);

}

Anyone knows a way around this?

**The Image Shows the Path to the Crash, as you can see if i use the bottom navigation to do the same actions it works .. but when i use the alternative it crashes after selection**

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
bob269
  • 115
  • 1
  • 13
  • whaat is tripDateFragment? – John Joe Feb 16 '19 at 17:32
  • that is the Fragment Opened by clicking a button at the HomeFragment.. tripDateFragment is a normal fragment inside it there another button that Opens a Dialog to Select a City – bob269 Feb 16 '19 at 17:35
  • your question is unclear for me and the code is not enough I afraid – John Joe Feb 16 '19 at 17:38
  • @JohnJoe the Image I added explains some more, I can share with you the full Code, tell me how i cant do that – bob269 Feb 16 '19 at 18:07
  • so tripDateFragment is in AndroidMobile-4 ? – John Joe Feb 16 '19 at 18:11
  • And the OnCreate you post is in AndroidMobile-2? – John Joe Feb 16 '19 at 18:14
  • TripDate is on android Mobile -2 and that is also the same place where the TextView is. Android-Mobile 3 is when the Dialog is Open. The Path in Green Works Great but the path in Red Crashes all the time after selecting the Item – bob269 Feb 16 '19 at 18:36
  • @Fantômas can you give me a link to the answer please. because i have not found a solution – bob269 Feb 16 '19 at 19:03
  • Debugging is part of your developing work. Simply *find the object which is used before it's referenced* and you're done. – Phantômaxx Feb 16 '19 at 19:05
  • I did all that , apparently based on the errors i get its the Edit Text, one more strange thing is even Showing a Toast throws errors – bob269 Feb 16 '19 at 19:15
  • If there is an editText in AndroidMobile-4, you need to declare the id in the xml – John Joe Feb 17 '19 at 13:24
  • It is declared.. Only tht i cant do a set text outside the onCreate method – bob269 Feb 17 '19 at 13:32

1 Answers1

0

Why are you declaring again Textview again in onCreate Method?

Declare outside the onCreate and Find the View using "findViewByID" inside on create.

  • there is no Declaration inside the OnCreate, as you can see onCreate Starts right below the declaration. – bob269 Feb 16 '19 at 17:13