Question

I'm new to android and i'm following a tutorial here http://code.tutsplus.com/tutorials/android-sdk-create-a-drawing-app-touch-interaction--mobile-19202 in order to make a simple drawing app. Everything works fine until i add these two functions in the onCreateView method

currPaint = (ImageButton)paintLayout.getChildAt(0);



currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

Here's the faulty code :

        package com.example.tastycorpse;

    import android.support.v7.app.ActionBarActivity;
    import android.support.v7.app.ActionBar;
    import android.support.v4.app.Fragment;
    import android.annotation.SuppressLint;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.View;
    import android.view.ViewGroup;
    import android.os.Build;
    import android.widget.ImageButton;
    import android.widget.LinearLayout;

    @SuppressLint("ValidFragment")
    public class MainActivity extends ActionBarActivity {
        private DrawingView drawView;
        private ImageButton currPaint;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            if (savedInstanceState == null) {
                getSupportFragmentManager().beginTransaction()
                        .add(R.id.container, new PlaceholderFragment()).commit();
            }
        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
            if (id == R.id.action_settings) {
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        /**
         * A placeholder fragment containing a simple view.
         */
        public class PlaceholderFragment extends Fragment {

            public PlaceholderFragment() {
            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {

                View rootView = inflater.inflate(R.layout.fragment_main, container,
                        false);
                drawView = (DrawingView)findViewById(R.id.drawing);
                //get the palette and first color button
                LinearLayout paintLayout = (LinearLayout)findViewById(R.id.paint_colors);
                currPaint = (ImageButton)paintLayout.getChildAt(0);
                currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));

                return rootView;
            }





    }
    //user clicked paint
    public void paintClicked(View view){
        //use chosen color      
        if(view!=currPaint){
            ImageButton imgView = (ImageButton)view;
            String color = view.getTag().toString();
            drawView.setColor(color);
            //update ui
            imgView.setImageDrawable(getResources().getDrawable(R.drawable.paint_pressed));
            currPaint.setImageDrawable(getResources().getDrawable(R.drawable.paint));
            currPaint=(ImageButton)view;
        }
    }

}

and here's the logcat

05-04 12:40:24.224: E/AndroidRuntime(2887): Caused by: java.lang.NullPointerException
05-04 12:43:14.489: E/AndroidRuntime(3048): Caused by: java.lang.NullPointerException
05-04 12:43:47.919: E/AndroidRuntime(3187): Caused by: java.lang.NullPointerException
05-04 12:43:54.607: E/AndroidRuntime(3232): Caused by: java.lang.NullPointerException

i tried to follow and understand all the steps of the tutorial but i'm stuck here... (unfortunately -app- has stopped). I can't even find anything on older questions because the error is too general. Can someone explain to me why i get this error?

Was it helpful?

Solution

Call findViewById() on the rootView you just inflated and not Activity.findViewById(). The fragment view hierarchy is not yet a part of the activity view hierarchy and trying to find the fragment's view from the activity will return null. Invoking a method such as getChildAt() on null causes the NPE.

For example, change calls like

drawView = (DrawingView)findViewById(R.id.drawing);

to

drawView = (DrawingView)rootView.findViewById(R.id.drawing);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top