0

I have searched for an answer that works for me but have not come across anything that helped.

My problem is that I am linking an ImageButton to another activity, however when clicked on throws a NullPointerException

Here is the MainActivity class:

package com.example.gpp2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;

public class MainActivity extends AppCompatActivity  {

    ImageButton addButton;
    ImageButton loginButton;
    ImageButton offerButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        addButton = (ImageButton) findViewById(R.id.imageButton);

        addButton.setOnClickListener(new View.OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent intent = new Intent(MainActivity.this, AddPage.class);
                intent.putExtra("keyWord", R.drawable.add);
                startActivity(intent);
            }
        });

        loginButton = (ImageButton) findViewById(R.id.imageButton5);
        loginButton.setOnClickListener(new View.OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent intent = new Intent(MainActivity.this, loginH.class);
                intent.putExtra("keyWord", R.drawable.login);
                startActivity(intent);
            }
        });

        offerButton = (ImageButton) findViewById(R.id.imageButton4);

        offerButton.setOnClickListener(new View.OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent intent = new Intent(MainActivity.this, OfferPage.class);
                intent.putExtra("keyWord", R.drawable.moneybag);
                startActivity(intent);
            }
        });

    }

}

Here is the activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/logo"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/NCard"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:text="@string/NCard"
        android:textSize="24sp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.526"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.024" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="58dp" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="724dp" />

    <ImageButton
        android:id="@+id/imageButton"
        android:layout_width="66dp"
        android:layout_height="50dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@null"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0"
        app:srcCompat="@drawable/add" />

    <ImageButton
        android:id="@+id/imageButton2"
        android:layout_width="87dp"
        android:layout_height="69dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@null"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.025"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.998"
        app:srcCompat="@drawable/creditcardspayment" />

    <ImageButton
        android:id="@+id/imageButton3"
        android:layout_width="87dp"
        android:layout_height="69dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@null"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.353"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.998"
        app:srcCompat="@drawable/bell" />

    <ImageButton
        android:id="@+id/imageButton4"
        android:layout_width="87dp"
        android:layout_height="69dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@null"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.691"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/moneybag" />

    <ImageButton
        android:id="@+id/imageButton5"
        android:layout_width="87dp"
        android:layout_height="69dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        android:background="@null"
        android:scaleType="centerInside"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0"
        app:srcCompat="@drawable/login" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@android:color/black"
        android:orientation="horizontal"
        app:layout_constraintGuide_begin="637dp" />

</android.support.constraint.ConstraintLayout>
Here is the AddPaje class:

package com.example.gpp2;


import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;

public class AddPage extends MainActivity {

    Button addButton;
    ImageButton backButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.addpage);


        backButton = (ImageButton) findViewById(R.id.imageButton11);

        backButton.setOnClickListener(new View.OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent intent = new Intent(AddPage.this, MainActivity.class);
                startActivity(intent);
            }
        });
        addButton = (Button) findViewById(R.id.button);

        addButton.setOnClickListener(new View.OnClickListener() {
            // When the button is pressed/clicked, it will run the code below
            @Override
            public void onClick(View view) {
                // Intent is what you use to start another activity
                Intent intent = new Intent(AddPage.this, NewCard.class);
                startActivity(intent);
            }
        });



    }


}

this is all errors in Run

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.gpp2, PID: 21722
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gpp2/com.example.gpp2.AddPage}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
        at android.app.ActivityThread.-wrap11(Unknown Source:0)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageButton.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
        at com.example.gpp2.AddPage.onCreate(AddPage.java:22)
        at android.app.Activity.performCreate(Activity.java:7009)
        at android.app.Activity.performCreate(Activity.java:7000)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
  • Please post the complete stack trace; i.e., all of the red lines after the `java.lang.NullPointerException`. – Mike M. Feb 02 '19 at 05:06
  • Where is your logcat details – Pratik Butani Feb 02 '19 at 05:49
  • post your Logcat – Vignesh Feb 02 '19 at 06:35
  • i add all the red line , but the logcat is larg. – khawla abdulla Feb 02 '19 at 10:01
  • It would seem that there is no `` with ID `imageButton11` in the `addpage` layout. I would also mention that `AddPage` currently extends `MainActivity`. Is that what you meant to do? It's not using anything from the `MainActivity` class, and, as is, you end up setting up the content `View` twice, with the second time completely replacing the first, so it's kind of pointless. Perhaps you meant for that to extend just plain `Activity`. – Mike M. Feb 02 '19 at 16:44
  • Kindly share your addpage layout – Erselan Khan Feb 04 '19 at 19:32

1 Answers1

-1

you can pass byte array and get byte array and then make a bitmap :

 Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);     
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos); 
byte[] b = baos.toByteArray();

Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);

In Activity B you receive intent with byte array (decoded picture) and apply it as source to ImageView:

Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
`
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
`ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
bugfreerammohan
  • 1,471
  • 1
  • 7
  • 22
  • How does this solve the `NullPointerException`? They aren't even doing anything with the passed extra yet. Besides, passing the `R.drawable` is much better than passing a huge `byte` array on an `Intent`. – Mike M. Feb 02 '19 at 05:07