Blake Loizides
Blake Loizides

Reputation: 985

Show TextView after clicking a button

I want to show a TextView once I click the button.

My Android project runs, but when I click the button, it closes my app.

Obviously I'm doing something wrong.

This is my code:

public class AFragment extends Fragment {

Button button;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = (View) inflater.inflate(R.layout.afragment, container, false);
button = (Button) mainView.findViewById(R.id.button1);
addListenerOnButton();
return mainView;
}

public void addListenerOnButton() {
    button.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View mainView) {

            TextView view = (TextView) mainView.findViewById(R.id.textView1);
            view.setText("Do whatever");

        }


    });

}

}

Logcat

02-14 21:51:35.896: E/AndroidRuntime(1536): FATAL EXCEPTION: main
02-14 21:51:35.896: E/AndroidRuntime(1536): java.lang.NullPointerException
02-14 21:51:35.896: E/AndroidRuntime(1536):     at    com.test.capitalpostouch.AFragment$1.onClick(AFragment.java:35)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.view.View.performClick(View.java:4084)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.view.View$PerformClick.run(View.java:16966)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.os.Handler.handleCallback(Handler.java:615)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.os.Looper.loop(Looper.java:137)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at android.app.ActivityThread.main(ActivityThread.java:4745)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at java.lang.reflect.Method.invokeNative(Native Method)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at java.lang.reflect.Method.invoke(Method.java:511)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-14 21:51:35.896: E/AndroidRuntime(1536):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 862

Answers (3)

John O'Connor
John O'Connor

Reputation: 5274

The problem is that, in setOnClickListener(View mainView), the View being passed in as a parameter actually refers to the button to which the listener is attached rather than the view you were attempting to inflate with the LayoutInflater. Remember that all widgets, including Buttons, are also Views. Try something like this.

public class AFragment extends Fragment {
     View mainView;
     Button button;
     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
          mainView = (View) inflater.inflate(R.layout.afragment, container, false);
          button = (Button) mainView.findViewById(R.id.button1);
          button.setOnClickListener(buttonListener);
          return mainView;
     }

     private View.OnClickListener buttonListener = new View.OnClickListener() {
          @Override
          public void onClick(View button) {
               TextView textView = AFragment.this.mainView.findViewById(R.id.textView1);
               textView.setText("Do whatever");
          }
     }
}

Upvotes: 0

Sababado
Sababado

Reputation: 2532

In your onClick method the View passed in is not the "mainView" you are expecting. The View passed in is the view that was clicked on; so in this case you're getting the view for the button R.id.button.

Change the line from

TextView view = (TextView) mainView.findViewById(R.id.textView1);

to

TextView view = (TextView) getView().findViewById(R.id.textView1);

Upvotes: 1

SimonSays
SimonSays

Reputation: 10977

onClick() does not give you the mainView as parameter, but the button that got clicked. either store your mainView on class level, or use getActivity().findViewById()

and yes, some log messages would have been nice!

Upvotes: 2

Related Questions