0

im currently working on putting to projects together and i started getting this error:

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setVisibility(int)' on a null object reference

im using a guide from this link, so i dont understand why it doesnt work.

the first activity.java

public class home extends AppCompatActivity  {


    private DrawerLayout drawer;
    TextView fadetxt;

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

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presentActivity(view);
            }
        });

    }

    public void presentActivity(View view) {
        ActivityOptionsCompat options = ActivityOptionsCompat.
                makeSceneTransitionAnimation(this, view, "transition");
        int revealX = (int) (view.getX() + view.getWidth() / 2);
        int revealY = (int) (view.getY() + view.getHeight() / 2);

        Intent intent = new Intent(this, MainActivity.class);
        intent.putExtra(MainActivity.EXTRA_CIRCULAR_REVEAL_X, revealX);
        intent.putExtra(MainActivity.EXTRA_CIRCULAR_REVEAL_Y, revealY);

        ActivityCompat.startActivity(this, intent, options.toBundle());
    }
}

The second activity.java

public class MainActivity extends Activity implements View.OnClickListener  {

    public static final String EXTRA_CIRCULAR_REVEAL_X = "EXTRA_CIRCULAR_REVEAL_X";
    public static final String EXTRA_CIRCULAR_REVEAL_Y = "EXTRA_CIRCULAR_REVEAL_Y";

    View rootLayout;

    private int revealX;
    private int revealY;

    // use a compound button so either checkbox or switch widgets work.
    private CompoundButton autoFocus;
    private CompoundButton useFlash;
    private TextView statusMessage;
    private TextView barcodeValue;

    private static final int RC_BARCODE_CAPTURE = 9001;
    private static final String TAG = "BarcodeMain";

    public static final String DATA = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
    public static Random RANDOM = new Random();



        public static String randomString(int len) {
            StringBuilder sb = new StringBuilder(len);

            for (int i = 0; i < len; i++) {
                sb.append(DATA.charAt(RANDOM.nextInt(DATA.length())));
            }

            return sb.toString();
        }

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

        statusMessage = (TextView)findViewById(R.id.status_message);
        barcodeValue = (TextView)findViewById(R.id.barcode_value);

        autoFocus = (CompoundButton) findViewById(R.id.auto_focus);
        useFlash = (CompoundButton) findViewById(R.id.use_flash);
        autoFocus.setVisibility(View.INVISIBLE);
        useFlash.setVisibility(View.INVISIBLE);

        findViewById(R.id.read_barcode).setOnClickListener(this);
        TextView alpha = (TextView) findViewById(R.id.auto_focus);
        alpha.setText(randomString(6));

        final Intent intent = getIntent();

        rootLayout = findViewById(R.id.root_layout);

        if (savedInstanceState == null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP &&
                intent.hasExtra(EXTRA_CIRCULAR_REVEAL_X) &&
                intent.hasExtra(EXTRA_CIRCULAR_REVEAL_Y)) {
            rootLayout.setVisibility(View.INVISIBLE);

            revealX = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_X, 0);
            revealY = intent.getIntExtra(EXTRA_CIRCULAR_REVEAL_Y, 0);


            ViewTreeObserver viewTreeObserver = rootLayout.getViewTreeObserver();
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
                    @Override
                    public void onGlobalLayout() {
                        revealActivity(revealX, revealY);
                        rootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                    }
                });
            }
        } else {
            rootLayout.setVisibility(View.VISIBLE);
        }

My floating button

<?xml version="1.0" encoding="utf-8"?>
<!-- Use DrawerLayout as root container for activity -->
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">

    <!-- Layout to contain contents of main body of screen (drawer will slide over this) -->
    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#F7F7F7">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="#00FFFFFF"
            android:theme="@style/ThemeOverlay.AppCompat.ActionBar"
            android:elevation="4dp"/>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            >


            <RelativeLayout
                android:id="@+id/upper_section"
                android:layout_width="match_parent"
                android:layout_height="181dp"
                android:background="#2699FB">

                <TextView
                    android:id="@+id/navn"
                    android:layout_width="wrap_content"
                    android:layout_height="69dp"
                    android:layout_alignParentBottom="true"
                    android:layout_alignParentStart="true"
                    android:layout_marginBottom="44dp"
                    android:layout_marginStart="54dp"
                    android:text="OreoList"
                    android:textColor="#ffffff"
                    android:textSize="40sp" />


                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:layout_alignStart="@+id/navn"
                    android:layout_marginBottom="37dp"
                    android:text="Family Milch"
                    android:textSize="20sp" />

            </RelativeLayout>

            <ListView
                android:id="@+id/listview"
                android:layout_width="match_parent"
                android:layout_height="448dp"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/textView3"
                android:layout_marginStart="0dp" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentStart="true"
                android:layout_below="@+id/upper_section"
                android:layout_marginBottom="10dp"
                android:layout_marginStart="20dp"
                android:layout_marginTop="10dp"
                android:text="Indkøbsliste"
                android:textColor="#707070"
                android:textSize="20sp"
                />

            <android.support.design.widget.FloatingActionButton
                android:id="@+id/fab"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignBottom="@+id/textView"
                android:layout_alignParentEnd="true"
                android:layout_gravity="end|bottom"
                android:layout_marginEnd="22dp"
                app:backgroundTint="#114F84"
                app:fabSize="mini"
                android:src="@drawable/ic_add"/>

            <TextView
                android:id="@+id/textView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentBottom="true"
                android:layout_alignStart="@+id/textView3"
                android:layout_marginBottom="34dp"
                android:text="Completed"
                android:textColor="#707070"
                android:textSize="20sp" />


        </RelativeLayout>

    </FrameLayout>




    <!-- Container for contents of drawer - use NavigationView to make configuration easier -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        />

</android.support.v4.widget.DrawerLayout>
Kaushik
  • 6,150
  • 5
  • 39
  • 54
  • Your error is in 'void android.view.View.setVisibility(int)' means in setVisibility.. check all setVisibility is done after finding the references – Vidhi Dave Jun 18 '18 at 10:10
  • Post your activity layout xml files. Most probably you are missing controls auto_focus or use_flash within activity_main that your second activity is referencing. – CodeChimp Jun 18 '18 at 10:11
  • The view you are trying to make invisible is null...please check whether the id names are given exactly as that of the xml – Sonu Sanjeev Jun 18 '18 at 10:11
  • Post your complete stacktrace – Marc Estrada Jun 18 '18 at 10:12
  • @CodeChimp I have changed the code so you can see my Xml now – AsgerThePandeklasker Jun 18 '18 at 10:40
  • get ride of `autoFocus = (CompoundButton) findViewById(R.id.auto_focus); useFlash = (CompoundButton) findViewById(R.id.use_flash); autoFocus.setVisibility(View.INVISIBLE); useFlash.setVisibility(View.INVISIBLE);` (not present in your xml) and add in java `FloatingActionButton floatingActionButton = findViewById(R.id.fab);` – E.Abdel Jun 18 '18 at 11:21
  • @E.Abdel its still making the same error, but now it doesnt shutdown now it just stop. – AsgerThePandeklasker Jun 18 '18 at 11:29
  • You need to include a stack trace when getting an exception to help narrow down where the problem is occurring, otherwise everyone has to read all of your code to find it and it's very time consuming. Use android device monitor for this. Setting breakpoints is also helpful, then just step through your code until it goes off, or try straddling them over suspected areas, and continue to narrow it down over successive runs. – samus Jun 18 '18 at 20:46

0 Answers0