0

Can someone tell me where I am going wrong?

I'm trying to change my textview called MyText to something else, but when I try I get the error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.Window.findViewById(int)' on a null object reference at android.app.Activity.findViewById(Activity.java:2071)

public class MainScreen extends Activity {
    TextView texting = (TextView) findViewById(R.id.MyText);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);
        texting.setText("Test");

};
}

3 Answers 3

4

This is how your code should look like:

public class MainScreen extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);

        // If you're not using this view anywhere, you don't have to declare it globally. Use only a local variables
        TextView texting = (TextView) findViewById(R.id.MyText);
        texting.setText("Test");

    };
}

You should ALWAYS initialize your views after you have your root layout. In this case, right after you did setContentView(R.layout.activity_main_screen); This scenario applies to Activites.

In case you use a fragment you should do this right after you inflated your root layout in the onCreateView method. Something like:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = (LinearLayout) inflater.inflate(R.layout.my_fragment, container, false);
    // THIS is where you start initializing your views
    return view;
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you, this was doing my head in.
1

The TextView is not available until after setContentView is called. So, findViewById will return null when MainScreen is instantiated. The solution is to move the findViewById call to the onCreate method as follows:

public class MainScreen extends Activity {
    TextView texting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);

        texting = (TextView) findViewById(R.id.MyText);

        texting.setText("Test");

    }
}

Comments

1

Do the following:

public class MainScreen extends Activity {
    TextView texting;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_screen);

        texting = = (TextView) findViewById(R.id.MyText);
        texting.setText("Test");

    };
}

You have to initialize your TextView object inside your onCreate method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.