Skip to content Skip to sidebar Skip to footer

Nullpointerexception In Ecclipse Android Sdk Unfortunately Myapp Has Stopped

I am a beginner. Stuck on this NullPointerException. This is a simple program of buttons. As soon as i run as android application, the project opens and immediately gets closed by

Solution 1:

The problem is you have created View elements in your fragment fragment_main.xml (I assume that's what it's called, you haven't posted the name) but your code expects them in your activity.

When you do this:

setContentView(R.layout.activity_buttonwa);

name = (Button) findViewById(R.id.name_button);
surname = (Button) findViewById(R.id.surname_button);
display = (TextView) findViewById(R.id.display);

You are saying, "Hey activity, check activity_buttonwa.xml for all these view elements, you'll find them there.

Judging from your error message, name is null and therefore you are getting a NullPointerException when you try and set an OnClickListener on it. It's null because it's not in your activity_buttonwa.xml file.

To fix, you can either move all that xml into activity_buttonwa.xmlor you can move the inflation code into your fragment:

/**
 * A placeholder fragment containing a simple view.
 */publicstaticclassPlaceholderFragmentextendsFragment {

    publicPlaceholderFragment() {
    }

    @Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        ViewrootView= inflater.inflate(R.layout.fragment_main, container, false);

        name = (Button) rootView.findViewById(R.id.name_button);
        surname = (Button) rootView.findViewById(R.id.surname_button);
        display = (TextView) rootView.findViewById(R.id.display);

        // Add OnClickListeners, etc.return rootView;
    }
}

I would say it's better to move the inflation code to your fragment, as above.

Post a Comment for "Nullpointerexception In Ecclipse Android Sdk Unfortunately Myapp Has Stopped"