0

I am trying to build an android app that allows you to input text, and will then transfer the text to the main screen. It allows me to build the autocomplete with no issue, but whenever i try to invoke the getText() function, the entire program crashes. Any help would be highly appreciated, thank you

     public class TeamFragment extends DialogFragment {
TextView team1, team2, score1, score2;



    AutoCompleteTextView team1input;



public TeamFragment(View t1, View t2, View s1, View s2) {
    team1=(TextView) t1;
    team2=(TextView) t2;
    score1=(TextView) s1;
    score2=(TextView) s2;
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.team_score_layout, container);
    //mEditText = (EditText) view.findViewById(R.id.txt_your_name);
    getDialog().setTitle("Enter Team Name");
    Button b = (Button) view.findViewById(R.id.teamButton);

    b.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
            team1input=(AutoCompleteTextView) v.findViewById(R.id.autoCompleteTeam1);
            team1input.getText();         //crashes here
            team1.setText("hi");
            dismiss();
        }
    });
    return view;
}


}

here is the logCat, I appear to be getting a nullpointerexception.

04-23 13:12:33.628: E/AndroidRuntime(902): FATAL EXCEPTION: main
04-23 13:12:33.628: E/AndroidRuntime(902): java.lang.NullPointerException
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.example.ucsb.cs.cs185.dimberman.nba.TeamFragment$1.onClick(TeamFragment.java:42)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.view.View.performClick(View.java:2485)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.view.View$PerformClick.run(View.java:9080)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Handler.handleCallback(Handler.java:587)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Handler.dispatchMessage(Handler.java:92)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.os.Looper.loop(Looper.java:123)
04-23 13:12:33.628: E/AndroidRuntime(902):  at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 13:12:33.628: E/AndroidRuntime(902):  at java.lang.reflect.Method.invokeNative(Native Method)
04-23 13:12:33.628: E/AndroidRuntime(902):  at java.lang.reflect.Method.invoke(Method.java:507)
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 13:12:33.628: E/AndroidRuntime(902):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 13:12:33.628: E/AndroidRuntime(902):  at dalvik.system.NativeStart.main(Native Method)

`

codeMagic
  • 44,549
  • 13
  • 77
  • 93
Daniel Imberman
  • 618
  • 1
  • 5
  • 18
  • You should provide the log cat. btw your implementation seems weird to me why do you pass View's as param to the TeamFragment constructor? – Benoit Apr 23 '13 at 20:24
  • 1
    It's very likely your ``findViewById`` within ``onClick`` does not work since you put OnClickListener to a Button. – harism Apr 23 '13 at 20:25
  • I was running into issues because findViewByID primarily accesses the main XML file, and so whenever I tried to access those from within the fragment, I was getting strange errors. I will add the log now. – Daniel Imberman Apr 23 '13 at 20:27
  • upon further testing, it appears that for some reason, my findviewbyid is returning a nullpointer. – Daniel Imberman Apr 23 '13 at 20:31

3 Answers3

4

Use team1input = (AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1) in the onCreateView() method and delete the similar line in the onClick() method.

(Assuming you have the AutoCompleteTextView with autoCompleteTeam1 id defined in the team_score_layout.xml layout file.)

zbr
  • 6,860
  • 4
  • 31
  • 43
  • That worked! thank you! could you please explain why it work within the onclick? – Daniel Imberman Apr 24 '13 at 03:26
  • Actually StoneBird has provided a good explanation in his answer. If you have any further questions, please ask more specifically. – zbr Apr 24 '13 at 07:46
4

When you call findViewById() under a view class it finds child views of that class.

When the onClick is triggered, View v passed into onClick() is the button. If the AutoCompleteView you are trying to retrieve is not a child of that button then calling v.findViewById() will give you NullPointerException, since the view does not exist under the button's children tree.

StoneBird
  • 1,900
  • 13
  • 12
0

Just replace

        team1input=(AutoCompleteTextView) v.findViewById(R.id.autoCompleteTeam1);

with

        team1input=(AutoCompleteTextView) view.findViewById(R.id.autoCompleteTeam1);
tasomaniac
  • 10,234
  • 6
  • 52
  • 84