2

I'm getting these errors when grabbing an image from the gallery, and setting that image as the background to a RelativeLayout. So this code will open the Gallery in OnClick(), grab an image from the gallery and it should set it as the background to a RelativeLayout.

LogCat: Gist

SettingsActivity:

MainActivity mainActivity;
RelativeLayout backgroundView;

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

    mainActivity = new MainActivity();
    backgroundView = (RelativeLayout) findViewById(R.id.wallpaperView);

    // Show the Up button in the action bar.
    setupActionBar();
}

/**
 * Set up the {@link android.app.ActionBar}, if the API is available.
 */
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void setupActionBar() {
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        getActionBar().setDisplayHomeAsUpEnabled(true);
    }
}

public void setBackgroundClick(View v)
{
    Intent intent = new Intent(Intent.ACTION_PICK,
            android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(intent, 0);
}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){
        Uri targetUri = data.getData();
        //textTargetUri.setText(targetUri.toString());
        Bitmap bitmap;
        try {
            bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
            //imageView.setImageBitmap(bitmap);
            backgroundView.setBackgroundDrawable(new BitmapDrawable(bitmap));
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            Toast.makeText(this, "Error", Toast.LENGTH_LONG).show();
            e.printStackTrace();
        }
    }
}

@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) {
    switch (item.getItemId()) {
        case android.R.id.home:
            // This ID represents the Home or Up button. In the case of this
            // activity, the Up button is shown. Use NavUtils to allow users
            // to navigate up one level in the application structure. For
            // more details, see the Navigation pattern on Android Design:
            //
            // http://developer.android.com/design/patterns/navigation.html#up-vs-back
            //
            //NavUtils.navigateUpFromSameTask(this);
            finish();
            return true;
    }
    return super.onOptionsItemSelected(item);
}

Thanks

Groot
  • 463
  • 1
  • 8
  • 20
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – BackSlash Aug 05 '14 at 07:50
  • This is not the version of the code that produces the stacktrace you linked to. Please post the relevant logcat in the question itself instead of some other site. Also, `new MainActivity()` is practically always wrong, though not related to this problem. – laalto Aug 05 '14 at 07:53
  • Please tell us which line in Your code is com.matt.cards.app.SettingsActivity.onActivityResult(SettingsActivity.java:70) (line 70) – maslan Aug 05 '14 at 07:59

1 Answers1

2

From your log it appears that the backgroundView is null. Can you check whether the findViewById(R.id.wallpaperView) finds a view?

(It looks like the log belongs to the imageView not commented as the logs says:

Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

So I think the bitmap retrieval is fine but setting it in your view is the problem.

userM1433372
  • 4,925
  • 32
  • 37
  • Thankyou! This helped me figure out my real problem, it was null because the setContentView() was set to the activity that backgroundView wasn't in. – Groot Aug 05 '14 at 08:23