Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ImageView img= (ImageView) findViewById(R.id.image);
- img.setImageResource(R.drawable.my_image);
- Attempt to invoke virtual method 'android.view.View android.widget.ImageView.findViewById(int)' on a null object reference
- java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageDrawable(android.graphics.drawable.Drawable)'
- <ImageView
- android:id="@+id/cafe"
- android:layout_width="100dp"
- android:layout_height="90dp"
- android:layout_alignParentEnd="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_gravity="right"
- android:src="@drawable/cafe" />
- <ImageView
- android:id="@+id/baron"
- android:layout_width="100dp"
- android:layout_height="90dp"
- android:layout_alignParentEnd="true"
- android:layout_alignParentRight="true"
- android:layout_alignParentTop="true"
- android:layout_gravity="right"
- android:src="@drawable/bar" />
- public boolean onMarkerClick(final Marker marker) {
- ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
- bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
- // Image replacement
- if (...) {
- ImageView img= (ImageView) findViewById(R.id.cafe);
- img.setImageResource(R.drawable.bar);
- }
- public class ExampleBottomSheetDialog extends BottomSheetDialogFragment {
- private BottomSheetListener mListener;
- @Nullable
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
- Button button1 = v.findViewById(R.id.button1);
- button1.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- dismiss();
- }
- });
- return v;
- }
- public interface BottomSheetListener {
- void onButtonClicked(String text);
- }
- @Override
- public void onAttach(Context context) {
- super.onAttach(context);
- try {
- mListener = (BottomSheetListener) context;
- } catch (ClassCastException e) {
- throw new ClassCastException(context.toString()
- + " must implement BottomSheetListener");
- }
- }
- Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
- ByteArrayOutputStream stream = new ByteArrayOutputStream();
- bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
- byte[] byteArray = stream.toByteArray();
- Intent intent = new Intent(this, NextActivity.class);
- intent.putExtra("picture", byteArray);
- startActivity(intent);
- Bundle extras = getIntent().getExtras();
- byte[] byteArray = extras.getByteArray("picture");
- Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
- ImageView image = (ImageView) findViewById(R.id.imageView1);
- image.setImageBitmap(bmp);
- Bundle args = new Bundle();
- if (...)
- args.putInt("drawable", R.drawable.bar);
- ExampleBottomSheetDialog bottomSheet = new ExampleBottomSheetDialog();
- bottomSheet.setArguments(args);
- bottomSheet.show(getSupportFragmentManager(), "exampleBottomSheet");
- @Override
- public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
- View v = inflater.inflate(R.layout.bottom_sheet_layout, container, false);
- ...
- ImageView img = v.findViewById(R.id.cafe);
- int drawable = getArguments().getInt("drawable", R.drawable.default_drawable);
- img.setImageResource(drawable);
- ...
- return v;
- }
Add Comment
Please, Sign In to add comment