Being new to android, I’m having some issues. Currently I’m trying to make use of fragments in an app I’m attempting to develop. I’m trying to simulate movement of data from an EditText view that appears as part of a layout inside a fragment.
The layout contains some other views, one being an empty TextView and a button that is also part of the layout. I’ve implemented code programmatically for the button inside the onCreateView of the fragment. A snippet of the code is below.
What I’m trying to do is capture what I type into the EditText view and load it into a variable of type EditText and then load that into an empty TextView after the button has been clicked. I’m doing this in an initial attempt to simulate retrieving this data from a database to display it. Again, here is the code:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment and assign it to a view
View view = inflater.inflate(R.layout.care_meeting, container, false);
Button button = (Button) view.findViewById(R.id.MtgButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
resName = (EditText) v.findViewById(R.id.resNameEditText);
trsName = resName.getText().toString();
TextView text = (TextView) v.findViewById(R.id.trNameView);
text.setText(trsName);
}
});
return view;
}
The problem occurs when I check the resName value and find that it’s null, which throws a nullpointer exception when I move to the next line. I’m trying to understand why what I type is not being picked up by the variable. Also, I tried using the getText() method that introduced an entirely different problem that involved a cast exception. So I abandoned that and stuck with this.
I tried to find someone else who may have experienced this problem, but I’m drawing a blank. What am I doing wrong? Please give me your insights to help me resolve this problem.
Thanks in advance,
Doug
I found the solution: I should have used the getView() method:
The changes work just fine.