android - App crashing when setting text size dynamically -
this question has answer here:
- what nullpointerexception, , how fix it? 12 answers
i developing application , whenever use
text1.settextsize(10 * getresources().getdisplaymetrics().density);
to set text size in fragment, app crashes. findviewbyid()
shows warning.
method invocation findviewbyid may produce java.lang.nullpointerexception
i tried suppress warning @suppresswarnings("constantconditions")
, successful in doing not helping app saved crashing.
it still crashes.
here's 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 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 note :
- without setting text or text size programmatically, works fine.
- i have tried putting texts in
oncreateview()
,onviewcreated()
suppresswarnings
nothing worked. - i have tried removing
android:text
xml no effect. - i have tried run app after removing
settextsize
in java still crashed. - i have tried remove nullables java file method invocation warning still there , did't help.
what want achieve want set text size according each device.
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); }
}
Comments
Post a Comment