Jochen
Jochen

Reputation: 1776

Null Pointer Exception when setting new text to text view

I am trying to run an AndroidApp on my phone. I am able to show my welcome fragment, moreover I can trigger the log-message. Unfortunately I am getting a null pointer exception if I want to change the text value from 'welcome' to 'Neuer Text'. What went wrong? I am quite new to android development.

public class WelcomeFragment extends Fragment implements OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_welcome, container, false);
        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);
        return view;
    }
    @Override
    public void onClick(View v) {
        //Log.d("JobaApp", "Logtext"); // see LogCat
        TextView text1 = (TextView) v.findViewById(R.id.welcome_text);
        text1.setText("NeuerText");
    }
}

Upvotes: 0

Views: 67

Answers (3)

user2213590
user2213590

Reputation:

You cant get the TextView from the button's onClick passed parameter ("View v"), since this is the actual button's view.

You should do something like:

public class WelcomeFragment extends Fragment implements OnClickListener {
View view;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_welcome, container, false);
        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);
        return view;
    }
    @Override
    public void onClick(View v) {
        //Log.d("JobaApp", "Logtext"); // see LogCat
        TextView text1 = (TextView) view.findViewById(R.id.welcome_text);
        text1.setText("NewerText"); //Also fixed typo
    }
}

Upvotes: 1

Abhishek Shukla
Abhishek Shukla

Reputation: 1242

In OnClick(View v) , v is the button that you are clicking on. The TextView does not belong to Button, it belongs to R.layout.fragment_welcome. So you find and initialize the TextView inside onCreateView() of the fragment, and then use it inside onClick(); Some what like this:

public class WelcomeFragment extends Fragment implements OnClickListener {
TextView tv;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_welcome, container, false);
        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);
        tv = (TextView) v.findViewById(R.id.welcome_text);
        return view;
    }
    @Override
    public void onClick(View v) {
        //Log.d("JobaApp", "Logtext"); // see LogCat

        tv.setText("NeuerText");
    }
}

Upvotes: 0

TronicZomB
TronicZomB

Reputation: 8747

In the onClick(), v is the view item that was clicked, e.g. the button, not the view that is inflated in onCreateView().

You should use getView():

public class WelcomeFragment extends Fragment implements OnClickListener {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_welcome, container, false);
        Button button1 = (Button) view.findViewById(R.id.button1);
        button1.setOnClickListener((OnClickListener) this);
        return view;
    }
    @Override
    public void onClick(View v) {
        //Log.d("JobaApp", "Logtext"); // see LogCat
        switch (v.getId) {
        case R.id.button1:
            TextView text1 = (TextView) getView().findViewById(R.id.welcome_text);
            text1.setText("NeuerText");
            break;
        }
    }
}

Also, if you don't want that action taking place for every button you may want to consider using a switch statement in your onClick().

Upvotes: 2

Related Questions