-1

I'm trying to make an app for my project on android studio, but when I'm trying to run this java file, this file is crashing and app stopped working on my emulator.Im simply trying to import images from my default gallery to my app.

import ....


public class tenant_profile extends Fragment {

private final int SELECT_PHOTO = 1;
private ImageView imageView;
public tenant_profile() {
    // Required empty public constructor
}


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

    // Inflate the layout for this fragment

    super.onCreateView(inflater, container, savedInstanceState);

    return inflater.inflate(R.layout.fragment_tenant_profile, container, false);
}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);



    Button pickImage = (Button) getView().findViewById(R.id.btn_pick);
    pickImage.setOnClickListener(new OnClickListener() {
    //enter code here
        @Override
        public void onClick(View view) {
            Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
            photoPickerIntent.setType("image/*");
            startActivityForResult(photoPickerIntent, SELECT_PHOTO);
        }
    });
}

    @Override
    public void onActivityResult ( int requestCode, int resultCode, Intent imageReturnedIntent){
        super.onActivityResult(requestCode, resultCode, imageReturnedIntent);

        switch (requestCode) {
            case SELECT_PHOTO:
                if (resultCode == Activity.RESULT_OK) {
                    try {
                        final Uri imageUri = imageReturnedIntent.getData();
                        final InputStream imageStream = getContext().getContentResolver().openInputStream(imageUri);
                        final Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                        imageView.setImageBitmap(selectedImage);
                    } catch (FileNotFoundException e) {
                        e.printStackTrace();
                    }


                }
        }
    }



}

My exception is following:

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: FATAL EXCEPTION: main

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime: java.lang.NullPointerException

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at com.example.sanchit.helloworld.tenant_profile.onCreate(tenant_profile.java:55)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.Fragment.performCreate(Fragment.java:1939)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:988)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.moveToState
(FragmentManager.java:1207)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:738)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1572)

01-29 08:04:13.233 1796-1796/? E/AndroidRuntime:     at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:493)

please help

logan
  • 33
  • 6
  • When *app stopped working*, then I am sure there is something more in logcat. In Red color? I am sure `pickImage` is null here. – Rohit5k2 Jan 29 '16 at 13:47
  • @Rohit5k2 I have added red lines of logcat above. please have a look – logan Jan 29 '16 at 15:38

1 Answers1

0

Please have a look at the developer section of android especially the training how to use fragments and its lifecycle.

You try to find a View by id even before the layout gets inflated.

To solve your problem, you may compare the lifecycle of a fragment regarding the order of calling onCreate and onCreateView.

ArticleFragment extends Fragment {
   @Override
   public View onCreateView(LayoutInflater inflater, ViewGroup container,
       Bundle savedInstanceState) {
        // Inflate the layout for this fragment
       View view = inflater.inflate(R.layout.article_view, container, false);
       //Get Button here
       Button pickImage = (Button) view.findViewById(R.id.btn_pick);
       //Set listener etc.
       return view;

    }
}

Additionally i encourage you to set up your pickImage as a member variable to be able to use it in more methods than simply onCreateView.

Robin Vinzenz
  • 1,247
  • 15
  • 19