0

I am developing an application and whenever I use

text1.setTextSize(10 * getResources().getDisplayMetrics().density);

to set the text size in a fragment, my app crashes. Also findViewById() shows this warning.

method invocation findviewbyid may produce java.lang.nullpointerexception

I tried to suppress this warning by @SuppressWarnings("ConstantConditions") and was successful in doing so but this is not helping my app to be saved from crashing.
It still crashes.
Here's the Java code:

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class Metro extends Fragment {

    @SuppressWarnings("ConstantConditions")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        TextView text1 = getView().findViewById(R.id.textView1);
        text1.setText(R.string.route);
        text1.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text2 = getView().findViewById(R.id.textView2);
        text2.setText(R.string.map);
        text2.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text3 = getView().findViewById(R.id.textView3);
        text3.setText(R.string.location);
        text3.setTextSize(10 * getResources().getDisplayMetrics().density);
        TextView text4 = getView().findViewById(R.id.textView4);
        text4.setText(R.string.about);
        text4.setTextSize(10 * getResources().getDisplayMetrics().density);

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

And the XML's code:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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:orientation="vertical"
    android:background="@drawable/home"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="40dp"
        android:id="@+id/button1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="parent">

        <ImageView
            android:id="@+id/imageview1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:layout_constraintTop_toBottomOf="@+id/imageview1"
            app:layout_constraintLeft_toLeftOf="@+id/imageview1"
            app:layout_constraintRight_toRightOf="@+id/imageview1"
            android:id="@+id/textView1"
            android:text="@string/route"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button2"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/button1"
        android:layout_marginTop="40dp">

        <ImageView
            android:id="@+id/imageview2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView2"
            app:layout_constraintTop_toBottomOf="@+id/imageview2"
            app:layout_constraintLeft_toLeftOf="@+id/imageview2"
            app:layout_constraintRight_toRightOf="@+id/imageview2"
            android:text="@string/map"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button3"
        app:layout_constraintTop_toBottomOf="@+id/button1"
        app:layout_constraintLeft_toLeftOf="@+id/button1"
        app:layout_constraintRight_toRightOf="@+id/button1"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/imageview3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/textView3"
            app:layout_constraintTop_toBottomOf="@+id/imageview3"
            app:layout_constraintLeft_toLeftOf="@+id/imageview3"
            app:layout_constraintRight_toRightOf="@+id/imageview3"
            android:text="@string/location"/>

    </android.support.constraint.ConstraintLayout>

    <android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button4"
        app:layout_constraintTop_toBottomOf="@+id/button2"
        app:layout_constraintLeft_toLeftOf="@+id/button2"
        app:layout_constraintRight_toRightOf="@+id/button2"
        android:layout_marginTop="20dp">

        <ImageView
            android:id="@+id/imageview4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/maps"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/about"
            android:id="@+id/textView4"
            app:layout_constraintTop_toBottomOf="@+id/imageview4"
            app:layout_constraintLeft_toLeftOf="@+id/imageview4"
            app:layout_constraintRight_toRightOf="@+id/imageview4"/>

    </android.support.constraint.ConstraintLayout>

</android.support.constraint.ConstraintLayout>

Some point to note :

  • Without setting text or text size programmatically, it works fine.
  • I have tried putting all those texts in onCreateView() and onViewCreated() with that SuppressWarnings but nothing worked.
  • I have tried removing all android:text from XML but no effect.
  • I have tried to run app after removing all setTextSize in the java but it still crashed.
  • I have also tried to remove all the nullables from the Java file but the Method Invocation warning was still there and it also did't help.

What I want to achieve is that I want to set text size according to each device.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1
 public class Metro extends Fragment {
@SuppressWarnings("ConstantConditions")
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    View view=inflater.inflate(R.layout.fragment_metro, container, false);
    TextView text1 =(TextView) view.findViewById(R.id.textView1);
    text1.setText(getString(R.string.route));
    text1.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text2 = (TextView) view.findViewById(R.id.textView2);
    text2.setText(getString(R.string.route));
    text2.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text3 = (TextView) view.findViewById(R.id.textView3);
    text3.setText(getString(.string.route));
    text3.setTextSize(10 * getResources().getDisplayMetrics().density);
    TextView text4 = (TextView) view.findViewById(R.id.textView4);
    text4.setText(getString(R.string.route));
    text4.setTextSize(10 * getResources().getDisplayMetrics().density);


    return view;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

}

}

Bosco Sabin
  • 124
  • 7
  • 1
    One thing I just noticed is that I was using the same string but changing those didn't help either. –  Jul 28 '17 at 10:24
  • I tried two ways in two different tabs - one is yours and one is by using SuppressWarnings on onViewCreated and pasting those text lines in onViewCreated and happy to see that both are working in individual tabs. Thank you –  Jul 28 '17 at 10:28
  • You are welcome !! – Bosco Sabin Jul 28 '17 at 10:34
1

Try this:

public class Metro extends Fragment {

    @SuppressWarnings("ConstantConditions")
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_metro, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        TextView text1 = view.findViewById(R.id.textView1);
        text1.setText(R.string.route);
        text1.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text2 = view.findViewById(R.id.textView2);
        text2.setText(R.string.route);
        text2.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text3 = view.findViewById(R.id.textView3);
        text3.setText(R.string.route);
        text3.setTextSize(10 * getResources().getDisplayMetrics().density);

        TextView text4 = view.findViewById(R.id.textView4);
        text4.setText(R.string.route);
        text4.setTextSize(10 * getResources().getDisplayMetrics().density);
    }
}
MPhil
  • 881
  • 1
  • 12
  • 24
Akash Dubey
  • 1,508
  • 17
  • 34
  • 2
    It's finally working but I put SuppressWarnings on onViewCreated but no worries. Thank you. Both Yours and Bosco sabin's method is working in two individual menu tabs. –  Jul 28 '17 at 10:30
  • Try to do view Stuffs in onViewCreated(), hence you don't need to go for getView() – Akash Dubey Jul 28 '17 at 10:32
  • I'll remeber this. Thank you –  Jul 28 '17 at 10:33
1

Instead of this line:

TextView text1 = getView().findViewById(R.id.textView1);

Try out this:

View view = inflater.inflate(R.layout.fragment_metro, container, false);
TextView text1 = view.findViewById(R.id.textView1);
MPhil
  • 881
  • 1
  • 12
  • 24
Sathish
  • 33
  • 4