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()
andonViewCreated()
with thatSuppressWarnings
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.