I’m trying to set the text of a TextView which sounds simple enough but it keeps throwing errors here’s my function:
private void getAndPutNum(){
TelephonyManager tm = (TelephonyManager)getSystemService(Context.TELEPHONY_SERVICE);
String myNum = tm.getLine1Number();
TextView numDisplay = (TextView)findViewById(R.id.myNum);
try{
numDisplay.setText(myNum);
}catch(Exception e){
Toast.makeText(getApplicationContext(), "ERROR", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
I’ve set READS_PHONE_STATE in my ‘Uses Permission’ of my Manifest. Can anyone see what I might be doing wrong?
UPDATE:
Here’s my layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="1">
<TextView android:gravity="center|center_vertical|center_horizontal" android:textAppearance="?android:attr/textAppearanceLarge" android:text="@string/number" android:textSize="65px" android:layout_gravity="center|center_vertical|center_horizontal" android:layout_height="fill_parent" android:layout_weight=".8" android:layout_width="fill_parent" android:layout_marginTop="-40dp" android:keepScreenOn="true" android:id="@+id/myNum"></TextView>
I was attempting to call my method before I
setContentView()thus when the method attempted to access current elements in the layout it resulted in aNullPointerException